2012-10-01 22:59:06 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Base WordPress Image Editor
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage Image_Editor
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-11-22 10:52:16 +01:00
|
|
|
* Base image editor class from which implementations extend
|
2012-10-01 22:59:06 +02:00
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
Code Modernization: Add `AllowDynamicProperties` attribute to all (parent) classes.
Dynamic (non-explicitly declared) properties are deprecated as of PHP 8.2 and are expected to become a fatal error in PHP 9.0.
There are a number of ways to mitigate this:
* If it is an accidental typo for a declared property: fix the typo.
* For known properties: declare them on the class.
* For unknown properties: add the magic `__get()`, `__set()`, et al. methods to the class or let the class extend `stdClass` which has highly optimized versions of these magic methods built in.
* For unknown ''use'' of dynamic properties, the `#[AllowDynamicProperties]` attribute can be added to the class. The attribute will automatically be inherited by child classes.
Trac ticket #56034 is open to investigate and handle the third and fourth type of situations, however it has become clear this will need more time and will not be ready in time for WP 6.1.
To reduce “noise” in the meantime, both in the error logs of WP users moving onto PHP 8.2, in the test run logs of WP itself, in test runs of plugins and themes, as well as to prevent duplicate tickets from being opened for the same issue, this commit adds the `#[AllowDynamicProperties]` attribute to all “parent” classes in WP.
The logic used for this commit is as follows:
* If a class already has the attribute: no action needed.
* If a class does not `extend`: add the attribute.
* If a class does `extend`:
- If it extends `stdClass`: no action needed (as `stdClass` supports dynamic properties).
- If it extends a PHP native class: add the attribute.
- If it extends a class from one of WP's external dependencies: add the attribute.
* In all other cases: no action — the attribute should not be needed as child classes inherit from the parent.
Whether or not a class contains magic methods has not been taken into account, as a review of the currently existing magic methods has shown that those are generally not sturdy enough and often even set dynamic properties (which they should not). See the [https://www.youtube.com/watch?v=vDZWepDQQVE live stream from August 16, 2022] for more details.
This commit only affects classes in the `src` directory of WordPress core.
* Tests should not get this attribute, but should be fixed to not use dynamic properties instead. Patches for this are already being committed under ticket #56033.
* While a number bundled themes (2014, 2019, 2020, 2021) contain classes, they are not a part of this commit and may be updated separately.
Reference: [https://wiki.php.net/rfc/deprecate_dynamic_properties PHP RFC: Deprecate dynamic properties].
Follow-up to [53922].
Props jrf, hellofromTonya, markjaquith, peterwilsoncc, costdev, knutsp, aristath.
See #56513, #56034.
Built from https://develop.svn.wordpress.org/trunk@54133
git-svn-id: http://core.svn.wordpress.org/trunk@53692 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-12 17:47:14 +02:00
|
|
|
#[AllowDynamicProperties]
|
2012-10-01 22:59:06 +02:00
|
|
|
abstract class WP_Image_Editor {
|
2017-12-01 00:11:00 +01:00
|
|
|
protected $file = null;
|
|
|
|
protected $size = null;
|
|
|
|
protected $mime_type = null;
|
2021-08-31 20:59:03 +02:00
|
|
|
protected $output_mime_type = null;
|
2012-10-01 22:59:06 +02:00
|
|
|
protected $default_mime_type = 'image/jpeg';
|
2017-12-01 00:11:00 +01:00
|
|
|
protected $quality = false;
|
2021-08-31 20:59:03 +02:00
|
|
|
|
|
|
|
// Deprecated since 5.8.1. See get_default_quality() below.
|
|
|
|
protected $default_quality = 82;
|
2012-10-01 22:59:06 +02:00
|
|
|
|
2012-11-22 10:52:16 +01:00
|
|
|
/**
|
|
|
|
* Each instance handles a single file.
|
2016-01-10 02:26:25 +01:00
|
|
|
*
|
|
|
|
* @param string $file Path to the file to load.
|
2012-11-22 10:52:16 +01:00
|
|
|
*/
|
|
|
|
public function __construct( $file ) {
|
|
|
|
$this->file = $file;
|
2012-10-01 22:59:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-11-22 10:52:16 +01:00
|
|
|
* Checks to see if current environment supports the editor chosen.
|
2019-11-17 08:43:01 +01:00
|
|
|
* Must be overridden in a subclass.
|
2012-10-01 22:59:06 +02:00
|
|
|
*
|
|
|
|
* @since 3.5.0
|
2015-05-29 17:43:29 +02:00
|
|
|
*
|
2012-11-22 10:52:16 +01:00
|
|
|
* @abstract
|
2012-10-01 22:59:06 +02:00
|
|
|
*
|
2012-11-22 10:52:16 +01:00
|
|
|
* @param array $args
|
2015-06-27 03:03:25 +02:00
|
|
|
* @return bool
|
2012-10-01 22:59:06 +02:00
|
|
|
*/
|
2012-11-22 10:52:16 +01:00
|
|
|
public static function test( $args = array() ) {
|
|
|
|
return false;
|
2012-10-01 22:59:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-11-22 10:52:16 +01:00
|
|
|
* Checks to see if editor supports the mime-type specified.
|
2019-11-17 08:43:01 +01:00
|
|
|
* Must be overridden in a subclass.
|
2012-10-01 22:59:06 +02:00
|
|
|
*
|
|
|
|
* @since 3.5.0
|
2015-05-29 17:43:29 +02:00
|
|
|
*
|
2012-11-22 10:52:16 +01:00
|
|
|
* @abstract
|
2012-10-01 22:59:06 +02:00
|
|
|
*
|
2012-11-22 10:52:16 +01:00
|
|
|
* @param string $mime_type
|
2015-06-27 03:03:25 +02:00
|
|
|
* @return bool
|
2012-10-01 22:59:06 +02:00
|
|
|
*/
|
2012-11-22 10:52:16 +01:00
|
|
|
public static function supports_mime_type( $mime_type ) {
|
2012-11-09 20:38:54 +01:00
|
|
|
return false;
|
2012-10-01 22:59:06 +02:00
|
|
|
}
|
|
|
|
|
2012-11-16 23:02:21 +01:00
|
|
|
/**
|
|
|
|
* Loads image from $this->file into editor.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
* @abstract
|
|
|
|
*
|
2021-01-03 23:04:04 +01:00
|
|
|
* @return true|WP_Error True if loaded; WP_Error on failure.
|
2012-11-16 23:02:21 +01:00
|
|
|
*/
|
2012-11-22 10:52:16 +01:00
|
|
|
abstract public function load();
|
2012-11-16 23:02:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves current image to file.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
2022-06-21 13:18:17 +02:00
|
|
|
* @since 6.0.0 The `$filesize` value was added to the returned array.
|
2012-11-16 23:02:21 +01:00
|
|
|
* @abstract
|
|
|
|
*
|
2021-09-09 22:39:58 +02:00
|
|
|
* @param string $destfilename Optional. Destination filename. Default null.
|
|
|
|
* @param string $mime_type Optional. The mime-type. Default null.
|
2022-06-21 13:18:17 +02:00
|
|
|
* @return array|WP_Error {
|
|
|
|
* Array on success or WP_Error if the file failed to save.
|
|
|
|
*
|
|
|
|
* @type string $path Path to the image file.
|
|
|
|
* @type string $file Name of the image file.
|
|
|
|
* @type int $width Image width.
|
|
|
|
* @type int $height Image height.
|
|
|
|
* @type string $mime-type The mime type of the image.
|
|
|
|
* @type int $filesize File size of the image.
|
|
|
|
* }
|
2012-11-16 23:02:21 +01:00
|
|
|
*/
|
2012-10-01 22:59:06 +02:00
|
|
|
abstract public function save( $destfilename = null, $mime_type = null );
|
2012-11-09 20:38:54 +01:00
|
|
|
|
|
|
|
/**
|
2012-11-16 23:02:21 +01:00
|
|
|
* Resizes current image.
|
|
|
|
*
|
2014-03-27 21:40:17 +01:00
|
|
|
* At minimum, either a height or width must be provided.
|
|
|
|
* If one of the two is set to null, the resize will
|
|
|
|
* maintain aspect ratio according to the provided dimension.
|
|
|
|
*
|
2012-11-16 23:02:21 +01:00
|
|
|
* @since 3.5.0
|
|
|
|
* @abstract
|
|
|
|
*
|
2023-07-11 08:31:22 +02:00
|
|
|
* @param int|null $max_w Image width.
|
|
|
|
* @param int|null $max_h Image height.
|
2023-08-19 02:10:23 +02:00
|
|
|
* @param bool|array $crop {
|
|
|
|
* Optional. Image cropping behavior. If false, the image will be scaled (default).
|
2023-08-18 19:56:17 +02:00
|
|
|
* If true, image will be cropped to the specified dimensions using center positions.
|
|
|
|
* If an array, the image will be cropped using the array to specify the crop location:
|
|
|
|
*
|
|
|
|
* @type string $0 The x crop position. Accepts 'left' 'center', or 'right'.
|
|
|
|
* @type string $1 The y crop position. Accepts 'top', 'center', or 'bottom'.
|
|
|
|
* }
|
2021-01-03 23:04:04 +01:00
|
|
|
* @return true|WP_Error
|
2012-11-16 23:02:21 +01:00
|
|
|
*/
|
|
|
|
abstract public function resize( $max_w, $max_h, $crop = false );
|
|
|
|
|
|
|
|
/**
|
2013-10-08 19:19:09 +02:00
|
|
|
* Resize multiple images from a single source.
|
2013-04-22 22:28:05 +02:00
|
|
|
*
|
2012-11-16 23:02:21 +01:00
|
|
|
* @since 3.5.0
|
|
|
|
* @abstract
|
|
|
|
*
|
2013-10-08 19:19:09 +02:00
|
|
|
* @param array $sizes {
|
2022-09-20 00:53:10 +02:00
|
|
|
* An array of image size arrays. Default sizes are 'small', 'medium', 'large'.
|
2013-10-08 19:19:09 +02:00
|
|
|
*
|
2022-04-29 21:24:09 +02:00
|
|
|
* @type array ...$0 {
|
2023-07-11 08:31:22 +02:00
|
|
|
* @type int $width Image width.
|
|
|
|
* @type int $height Image height.
|
|
|
|
* @type bool|array $crop Optional. Whether to crop the image. Default false.
|
2013-10-08 19:19:09 +02:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* @return array An array of resized images metadata by size.
|
2012-11-16 23:02:21 +01:00
|
|
|
*/
|
|
|
|
abstract public function multi_resize( $sizes );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Crops Image.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
* @abstract
|
|
|
|
*
|
2020-07-23 02:52:05 +02:00
|
|
|
* @param int $src_x The start x position to crop from.
|
|
|
|
* @param int $src_y The start y position to crop from.
|
|
|
|
* @param int $src_w The width to crop.
|
|
|
|
* @param int $src_h The height to crop.
|
|
|
|
* @param int $dst_w Optional. The destination width.
|
|
|
|
* @param int $dst_h Optional. The destination height.
|
2015-06-27 03:03:25 +02:00
|
|
|
* @param bool $src_abs Optional. If the source crop points are absolute.
|
2021-01-03 23:04:04 +01:00
|
|
|
* @return true|WP_Error
|
2012-11-16 23:02:21 +01:00
|
|
|
*/
|
|
|
|
abstract public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rotates current image counter-clockwise by $angle.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
* @abstract
|
|
|
|
*
|
|
|
|
* @param float $angle
|
2021-01-03 23:04:04 +01:00
|
|
|
* @return true|WP_Error
|
2012-11-16 23:02:21 +01:00
|
|
|
*/
|
|
|
|
abstract public function rotate( $angle );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flips current image.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
* @abstract
|
|
|
|
*
|
2015-06-27 03:03:25 +02:00
|
|
|
* @param bool $horz Flip along Horizontal Axis
|
|
|
|
* @param bool $vert Flip along Vertical Axis
|
2021-01-03 23:04:04 +01:00
|
|
|
* @return true|WP_Error
|
2012-11-16 23:02:21 +01:00
|
|
|
*/
|
|
|
|
abstract public function flip( $horz, $vert );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Streams current image to browser.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
* @abstract
|
|
|
|
*
|
2017-08-22 13:12:44 +02:00
|
|
|
* @param string $mime_type The mime type of the image.
|
2021-01-03 23:04:04 +01:00
|
|
|
* @return true|WP_Error True on success, WP_Error object on failure.
|
2012-11-09 20:38:54 +01:00
|
|
|
*/
|
2012-11-16 23:02:21 +01:00
|
|
|
abstract public function stream( $mime_type = null );
|
2012-11-09 20:38:54 +01:00
|
|
|
|
2012-10-01 22:59:06 +02:00
|
|
|
/**
|
2012-11-16 23:02:21 +01:00
|
|
|
* Gets dimensions of image.
|
2012-10-01 22:59:06 +02:00
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2022-09-20 00:53:10 +02:00
|
|
|
* @return int[] {
|
2019-11-05 22:27:02 +01:00
|
|
|
* Dimensions of the image.
|
|
|
|
*
|
|
|
|
* @type int $width The image width.
|
|
|
|
* @type int $height The image height.
|
|
|
|
* }
|
2012-10-01 22:59:06 +02:00
|
|
|
*/
|
|
|
|
public function get_size() {
|
|
|
|
return $this->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-11-16 23:02:21 +01:00
|
|
|
* Sets current image size.
|
2012-10-01 22:59:06 +02:00
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2022-09-20 00:53:10 +02:00
|
|
|
* @param int $width
|
|
|
|
* @param int $height
|
|
|
|
* @return true
|
2012-10-01 22:59:06 +02:00
|
|
|
*/
|
|
|
|
protected function update_size( $width = null, $height = null ) {
|
|
|
|
$this->size = array(
|
2017-12-01 00:11:00 +01:00
|
|
|
'width' => (int) $width,
|
|
|
|
'height' => (int) $height,
|
2012-10-01 22:59:06 +02:00
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-28 05:50:15 +02:00
|
|
|
* Gets the Image Compression quality on a 1-100% scale.
|
2012-10-01 22:59:06 +02:00
|
|
|
*
|
2014-06-28 05:50:15 +02:00
|
|
|
* @since 4.0.0
|
2012-10-01 22:59:06 +02:00
|
|
|
*
|
2020-06-20 00:55:12 +02:00
|
|
|
* @return int Compression Quality. Range: [1,100]
|
2012-10-01 22:59:06 +02:00
|
|
|
*/
|
2014-06-28 05:50:15 +02:00
|
|
|
public function get_quality() {
|
|
|
|
if ( ! $this->quality ) {
|
2014-12-08 17:19:23 +01:00
|
|
|
$this->set_quality();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->quality;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets Image Compression quality on a 1-100% scale.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @param int $quality Compression Quality. Range: [1,100]
|
2015-05-22 08:18:25 +02:00
|
|
|
* @return true|WP_Error True if set successfully; WP_Error on failure.
|
2014-12-08 17:19:23 +01:00
|
|
|
*/
|
|
|
|
public function set_quality( $quality = null ) {
|
2021-08-31 20:59:03 +02:00
|
|
|
// Use the output mime type if present. If not, fall back to the input/initial mime type.
|
|
|
|
$mime_type = ! empty( $this->output_mime_type ) ? $this->output_mime_type : $this->mime_type;
|
|
|
|
// Get the default quality setting for the mime type.
|
|
|
|
$default_quality = $this->get_default_quality( $mime_type );
|
|
|
|
|
2014-12-08 17:19:23 +01:00
|
|
|
if ( null === $quality ) {
|
2013-12-05 00:32:11 +01:00
|
|
|
/**
|
2016-05-22 20:15:28 +02:00
|
|
|
* Filters the default image compression quality setting.
|
2013-12-05 00:32:11 +01:00
|
|
|
*
|
2014-12-15 22:43:22 +01:00
|
|
|
* Applies only during initial editor instantiation, or when set_quality() is run
|
|
|
|
* manually without the `$quality` argument.
|
|
|
|
*
|
2020-07-23 02:52:05 +02:00
|
|
|
* The WP_Image_Editor::set_quality() method has priority over the filter.
|
2014-12-15 22:43:22 +01:00
|
|
|
*
|
2014-06-28 05:50:15 +02:00
|
|
|
* @since 3.5.0
|
2013-12-05 00:32:11 +01:00
|
|
|
*
|
2014-06-28 05:50:15 +02:00
|
|
|
* @param int $quality Quality level between 1 (low) and 100 (high).
|
|
|
|
* @param string $mime_type Image mime type.
|
2013-12-05 00:32:11 +01:00
|
|
|
*/
|
2021-08-31 20:59:03 +02:00
|
|
|
$quality = apply_filters( 'wp_editor_set_quality', $default_quality, $mime_type );
|
2014-06-28 05:50:15 +02:00
|
|
|
|
2021-08-31 20:59:03 +02:00
|
|
|
if ( 'image/jpeg' === $mime_type ) {
|
2014-06-28 05:50:15 +02:00
|
|
|
/**
|
2016-05-22 20:15:28 +02:00
|
|
|
* Filters the JPEG compression quality for backward-compatibility.
|
2014-06-28 05:50:15 +02:00
|
|
|
*
|
2014-12-15 22:43:22 +01:00
|
|
|
* Applies only during initial editor instantiation, or when set_quality() is run
|
|
|
|
* manually without the `$quality` argument.
|
|
|
|
*
|
2020-07-23 02:52:05 +02:00
|
|
|
* The WP_Image_Editor::set_quality() method has priority over the filter.
|
2014-12-15 22:43:22 +01:00
|
|
|
*
|
2014-06-28 05:50:15 +02:00
|
|
|
* The filter is evaluated under two contexts: 'image_resize', and 'edit_image',
|
|
|
|
* (when a JPEG image is saved to file).
|
|
|
|
*
|
|
|
|
* @since 2.5.0
|
|
|
|
*
|
|
|
|
* @param int $quality Quality level between 0 (low) and 100 (high) of the JPEG.
|
|
|
|
* @param string $context Context of the filter.
|
|
|
|
*/
|
|
|
|
$quality = apply_filters( 'jpeg_quality', $quality, 'image_resize' );
|
2014-10-04 15:12:15 +02:00
|
|
|
}
|
2013-12-04 23:49:10 +01:00
|
|
|
|
2014-12-08 17:19:23 +01:00
|
|
|
if ( $quality < 0 || $quality > 100 ) {
|
2021-08-31 20:59:03 +02:00
|
|
|
$quality = $default_quality;
|
2013-12-04 23:49:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-13 20:41:31 +02:00
|
|
|
// Allow 0, but squash to 1 due to identical images in GD, and for backward compatibility.
|
2014-10-04 15:12:15 +02:00
|
|
|
if ( 0 === $quality ) {
|
2014-06-28 05:50:15 +02:00
|
|
|
$quality = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ( $quality >= 1 ) && ( $quality <= 100 ) ) {
|
2013-12-04 23:49:10 +01:00
|
|
|
$this->quality = $quality;
|
|
|
|
return true;
|
|
|
|
} else {
|
2017-12-01 00:11:00 +01:00
|
|
|
return new WP_Error( 'invalid_image_quality', __( 'Attempted to set image quality outside of the range [1,100].' ) );
|
2013-12-04 23:49:10 +01:00
|
|
|
}
|
2012-10-01 22:59:06 +02:00
|
|
|
}
|
|
|
|
|
2021-08-31 20:59:03 +02:00
|
|
|
/**
|
|
|
|
* Returns the default compression quality setting for the mime type.
|
|
|
|
*
|
|
|
|
* @since 5.8.1
|
|
|
|
*
|
|
|
|
* @param string $mime_type
|
|
|
|
* @return int The default quality setting for the mime type.
|
|
|
|
*/
|
|
|
|
protected function get_default_quality( $mime_type ) {
|
|
|
|
switch ( $mime_type ) {
|
|
|
|
case 'image/webp':
|
|
|
|
$quality = 86;
|
|
|
|
break;
|
|
|
|
case 'image/jpeg':
|
|
|
|
default:
|
|
|
|
$quality = $this->default_quality;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $quality;
|
|
|
|
}
|
|
|
|
|
2012-10-01 22:59:06 +02:00
|
|
|
/**
|
|
|
|
* Returns preferred mime-type and extension based on provided
|
|
|
|
* file's extension and mime, or current file's extension and mime.
|
|
|
|
*
|
|
|
|
* Will default to $this->default_mime_type if requested is not supported.
|
|
|
|
*
|
|
|
|
* Provides corrected filename only if filename is provided.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @param string $filename
|
2012-11-09 21:10:33 +01:00
|
|
|
* @param string $mime_type
|
2012-10-01 22:59:06 +02:00
|
|
|
* @return array { filename|null, extension, mime-type }
|
|
|
|
*/
|
|
|
|
protected function get_output_format( $filename = null, $mime_type = null ) {
|
2014-05-06 20:37:29 +02:00
|
|
|
$new_ext = null;
|
2012-10-01 22:59:06 +02:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// By default, assume specified type takes priority.
|
2012-10-01 22:59:06 +02:00
|
|
|
if ( $mime_type ) {
|
|
|
|
$new_ext = $this->get_extension( $mime_type );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $filename ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
$file_ext = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) );
|
2012-10-01 22:59:06 +02:00
|
|
|
$file_mime = $this->get_mime_type( $file_ext );
|
2017-12-01 00:11:00 +01:00
|
|
|
} else {
|
2012-10-01 22:59:06 +02:00
|
|
|
// If no file specified, grab editor's current extension and mime-type.
|
2017-12-01 00:11:00 +01:00
|
|
|
$file_ext = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) );
|
2012-10-01 22:59:06 +02:00
|
|
|
$file_mime = $this->mime_type;
|
|
|
|
}
|
|
|
|
|
2023-07-09 23:25:23 +02:00
|
|
|
/*
|
|
|
|
* Check to see if specified mime-type is the same as type implied by
|
|
|
|
* file extension. If so, prefer extension from file.
|
|
|
|
*/
|
2023-08-06 14:52:25 +02:00
|
|
|
if ( ! $mime_type || ( $file_mime === $mime_type ) ) {
|
2012-10-01 22:59:06 +02:00
|
|
|
$mime_type = $file_mime;
|
2017-12-01 00:11:00 +01:00
|
|
|
$new_ext = $file_ext;
|
2012-10-01 22:59:06 +02:00
|
|
|
}
|
|
|
|
|
2024-08-05 06:13:15 +02:00
|
|
|
$output_format = wp_get_image_editor_output_format( $filename, $mime_type );
|
2021-05-21 00:15:56 +02:00
|
|
|
|
2021-05-22 15:00:02 +02:00
|
|
|
if ( isset( $output_format[ $mime_type ] )
|
|
|
|
&& $this->supports_mime_type( $output_format[ $mime_type ] )
|
2021-05-21 00:15:56 +02:00
|
|
|
) {
|
2021-05-22 15:00:02 +02:00
|
|
|
$mime_type = $output_format[ $mime_type ];
|
|
|
|
$new_ext = $this->get_extension( $mime_type );
|
2021-05-21 00:15:56 +02:00
|
|
|
}
|
|
|
|
|
2023-07-09 23:25:23 +02:00
|
|
|
/*
|
|
|
|
* Double-check that the mime-type selected is supported by the editor.
|
|
|
|
* If not, choose a default instead.
|
|
|
|
*/
|
2012-10-01 22:59:06 +02:00
|
|
|
if ( ! $this->supports_mime_type( $mime_type ) ) {
|
2013-10-07 00:19:10 +02:00
|
|
|
/**
|
2016-05-22 20:15:28 +02:00
|
|
|
* Filters default mime type prior to getting the file extension.
|
2013-10-07 18:28:15 +02:00
|
|
|
*
|
|
|
|
* @see wp_get_mime_types()
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @param string $mime_type Mime type string.
|
|
|
|
*/
|
2012-10-01 22:59:06 +02:00
|
|
|
$mime_type = apply_filters( 'image_editor_default_mime_type', $this->default_mime_type );
|
2017-12-01 00:11:00 +01:00
|
|
|
$new_ext = $this->get_extension( $mime_type );
|
2012-10-01 22:59:06 +02:00
|
|
|
}
|
|
|
|
|
2023-07-09 23:25:23 +02:00
|
|
|
/*
|
|
|
|
* Ensure both $filename and $new_ext are not empty.
|
|
|
|
* $this->get_extension() returns false on error which would effectively remove the extension
|
|
|
|
* from $filename. That shouldn't happen, files without extensions are not supported.
|
|
|
|
*/
|
2021-08-31 20:59:03 +02:00
|
|
|
if ( $filename && $new_ext ) {
|
2016-08-21 01:36:28 +02:00
|
|
|
$dir = pathinfo( $filename, PATHINFO_DIRNAME );
|
|
|
|
$ext = pathinfo( $filename, PATHINFO_EXTENSION );
|
2012-10-01 22:59:06 +02:00
|
|
|
|
|
|
|
$filename = trailingslashit( $dir ) . wp_basename( $filename, ".$ext" ) . ".{$new_ext}";
|
|
|
|
}
|
|
|
|
|
2021-08-31 20:59:03 +02:00
|
|
|
if ( $mime_type && ( $mime_type !== $this->mime_type ) ) {
|
|
|
|
// The image will be converted when saving. Set the quality for the new mime-type if not already set.
|
|
|
|
if ( $mime_type !== $this->output_mime_type ) {
|
|
|
|
$this->output_mime_type = $mime_type;
|
|
|
|
}
|
2022-10-07 21:17:12 +02:00
|
|
|
$this->set_quality();
|
2021-08-31 20:59:03 +02:00
|
|
|
} elseif ( ! empty( $this->output_mime_type ) ) {
|
|
|
|
// Reset output_mime_type and quality.
|
|
|
|
$this->output_mime_type = null;
|
|
|
|
$this->set_quality();
|
|
|
|
}
|
|
|
|
|
2012-10-01 22:59:06 +02:00
|
|
|
return array( $filename, $new_ext, $mime_type );
|
|
|
|
}
|
|
|
|
|
2022-09-20 00:53:10 +02:00
|
|
|
/**
|
|
|
|
* Builds an output filename based on current file, and adding proper suffix
|
2012-10-01 22:59:06 +02:00
|
|
|
*
|
|
|
|
* @since 3.5.0
|
2022-09-20 00:53:10 +02:00
|
|
|
*
|
|
|
|
* @param string $suffix
|
|
|
|
* @param string $dest_path
|
|
|
|
* @param string $extension
|
|
|
|
* @return string filename
|
2012-10-01 22:59:06 +02:00
|
|
|
*/
|
|
|
|
public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) {
|
2020-01-29 01:45:18 +01:00
|
|
|
// $suffix will be appended to the destination filename, just before the extension.
|
2022-09-20 00:53:10 +02:00
|
|
|
if ( ! $suffix ) {
|
2012-10-01 22:59:06 +02:00
|
|
|
$suffix = $this->get_suffix();
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2012-10-01 22:59:06 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$dir = pathinfo( $this->file, PATHINFO_DIRNAME );
|
|
|
|
$ext = pathinfo( $this->file, PATHINFO_EXTENSION );
|
2012-10-01 22:59:06 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$name = wp_basename( $this->file, ".$ext" );
|
2012-10-01 22:59:06 +02:00
|
|
|
$new_ext = strtolower( $extension ? $extension : $ext );
|
|
|
|
|
2019-07-03 01:42:58 +02:00
|
|
|
if ( ! is_null( $dest_path ) ) {
|
Media: Support Stream Wrappers In `WP_Image_Editor_Imagick`
Since `WP_Image_Editor`'s introduction, stream wrappers have functioned in `WP_Image_Editor_GD`, but haven't been properly supported in `WP_Image_Editor_Imagick`.
- Detects stream wrappers and uses `file_put_contents()` along with `Imagick::read/getImageBlob()` for handling when necessary.
- Introduces private method, `WP_Image_Editor_Imagick::write_image` to handle detection and proper saving.
- Introduces `WP_Test_Stream` class for testing stream wrappers, along with new tests for Imagick's stream handling and a stream filename test.
Adds requirement for `Imagick::readImageBlob()`, available in Imagick >= 2.0.0, which aligns with the current requirement of Imagick >= 2.2.0.
Props p00ya, calin, joemcgill, pputzer, jimyaghi, mikeschroder.
Fixes #42663.
Built from https://develop.svn.wordpress.org/trunk@49230
git-svn-id: http://core.svn.wordpress.org/trunk@48992 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-10-20 16:37:05 +02:00
|
|
|
if ( ! wp_is_stream( $dest_path ) ) {
|
|
|
|
$_dest_path = realpath( $dest_path );
|
|
|
|
if ( $_dest_path ) {
|
|
|
|
$dir = $_dest_path;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$dir = $dest_path;
|
2019-07-03 01:42:58 +02:00
|
|
|
}
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2012-10-01 22:59:06 +02:00
|
|
|
|
2022-09-20 00:53:10 +02:00
|
|
|
return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}";
|
2012-10-01 22:59:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds and returns proper suffix for file based on height and width.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2020-01-11 19:32:05 +01:00
|
|
|
* @return string|false suffix
|
2012-10-01 22:59:06 +02:00
|
|
|
*/
|
|
|
|
public function get_suffix() {
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! $this->get_size() ) {
|
2012-10-01 22:59:06 +02:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2012-10-01 22:59:06 +02:00
|
|
|
|
|
|
|
return "{$this->size['width']}x{$this->size['height']}";
|
|
|
|
}
|
|
|
|
|
Media/Upload: rotate images on upload according to EXIF Orientation.
Props msaggiorato, wpdavis, markoheijnen, dhuyvetter, msaggiorato, n7studios, triplejumper12, pbiron, mikeschroder, joemcgill, azaozz.
Fixes #14459.
Built from https://develop.svn.wordpress.org/trunk@46202
git-svn-id: http://core.svn.wordpress.org/trunk@46014 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-20 20:21:57 +02:00
|
|
|
/**
|
|
|
|
* Check if a JPEG image has EXIF Orientation tag and rotate it if needed.
|
|
|
|
*
|
|
|
|
* @since 5.3.0
|
|
|
|
*
|
|
|
|
* @return bool|WP_Error True if the image was rotated. False if not rotated (no EXIF data or the image doesn't need to be rotated).
|
|
|
|
* WP_Error if error while rotating.
|
|
|
|
*/
|
|
|
|
public function maybe_exif_rotate() {
|
|
|
|
$orientation = null;
|
|
|
|
|
|
|
|
if ( is_callable( 'exif_read_data' ) && 'image/jpeg' === $this->mime_type ) {
|
|
|
|
$exif_data = @exif_read_data( $this->file );
|
|
|
|
|
|
|
|
if ( ! empty( $exif_data['Orientation'] ) ) {
|
|
|
|
$orientation = (int) $exif_data['Orientation'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-13 20:14:05 +01:00
|
|
|
* Filters the `$orientation` value to correct it before rotating or to prevent rotating the image.
|
Media/Upload: rotate images on upload according to EXIF Orientation.
Props msaggiorato, wpdavis, markoheijnen, dhuyvetter, msaggiorato, n7studios, triplejumper12, pbiron, mikeschroder, joemcgill, azaozz.
Fixes #14459.
Built from https://develop.svn.wordpress.org/trunk@46202
git-svn-id: http://core.svn.wordpress.org/trunk@46014 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-20 20:21:57 +02:00
|
|
|
*
|
|
|
|
* @since 5.3.0
|
|
|
|
*
|
|
|
|
* @param int $orientation EXIF Orientation value as retrieved from the image file.
|
|
|
|
* @param string $file Path to the image file.
|
|
|
|
*/
|
|
|
|
$orientation = apply_filters( 'wp_image_maybe_exif_rotate', $orientation, $this->file );
|
|
|
|
|
2020-02-09 17:55:09 +01:00
|
|
|
if ( ! $orientation || 1 === $orientation ) {
|
Media/Upload: rotate images on upload according to EXIF Orientation.
Props msaggiorato, wpdavis, markoheijnen, dhuyvetter, msaggiorato, n7studios, triplejumper12, pbiron, mikeschroder, joemcgill, azaozz.
Fixes #14459.
Built from https://develop.svn.wordpress.org/trunk@46202
git-svn-id: http://core.svn.wordpress.org/trunk@46014 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-20 20:21:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( $orientation ) {
|
|
|
|
case 2:
|
|
|
|
// Flip horizontally.
|
2022-09-20 18:59:10 +02:00
|
|
|
$result = $this->flip( false, true );
|
Media/Upload: rotate images on upload according to EXIF Orientation.
Props msaggiorato, wpdavis, markoheijnen, dhuyvetter, msaggiorato, n7studios, triplejumper12, pbiron, mikeschroder, joemcgill, azaozz.
Fixes #14459.
Built from https://develop.svn.wordpress.org/trunk@46202
git-svn-id: http://core.svn.wordpress.org/trunk@46014 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-20 20:21:57 +02:00
|
|
|
break;
|
|
|
|
case 3:
|
2023-07-09 23:25:23 +02:00
|
|
|
/*
|
|
|
|
* Rotate 180 degrees or flip horizontally and vertically.
|
|
|
|
* Flipping seems faster and uses less resources.
|
|
|
|
*/
|
Media/Upload: rotate images on upload according to EXIF Orientation.
Props msaggiorato, wpdavis, markoheijnen, dhuyvetter, msaggiorato, n7studios, triplejumper12, pbiron, mikeschroder, joemcgill, azaozz.
Fixes #14459.
Built from https://develop.svn.wordpress.org/trunk@46202
git-svn-id: http://core.svn.wordpress.org/trunk@46014 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-20 20:21:57 +02:00
|
|
|
$result = $this->flip( true, true );
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
// Flip vertically.
|
2022-09-20 18:59:10 +02:00
|
|
|
$result = $this->flip( true, false );
|
Media/Upload: rotate images on upload according to EXIF Orientation.
Props msaggiorato, wpdavis, markoheijnen, dhuyvetter, msaggiorato, n7studios, triplejumper12, pbiron, mikeschroder, joemcgill, azaozz.
Fixes #14459.
Built from https://develop.svn.wordpress.org/trunk@46202
git-svn-id: http://core.svn.wordpress.org/trunk@46014 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-20 20:21:57 +02:00
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
// Rotate 90 degrees counter-clockwise and flip vertically.
|
|
|
|
$result = $this->rotate( 90 );
|
|
|
|
|
|
|
|
if ( ! is_wp_error( $result ) ) {
|
2022-09-20 18:59:10 +02:00
|
|
|
$result = $this->flip( true, false );
|
Media/Upload: rotate images on upload according to EXIF Orientation.
Props msaggiorato, wpdavis, markoheijnen, dhuyvetter, msaggiorato, n7studios, triplejumper12, pbiron, mikeschroder, joemcgill, azaozz.
Fixes #14459.
Built from https://develop.svn.wordpress.org/trunk@46202
git-svn-id: http://core.svn.wordpress.org/trunk@46014 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-20 20:21:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
// Rotate 90 degrees clockwise (270 counter-clockwise).
|
|
|
|
$result = $this->rotate( 270 );
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
// Rotate 90 degrees counter-clockwise and flip horizontally.
|
|
|
|
$result = $this->rotate( 90 );
|
|
|
|
|
|
|
|
if ( ! is_wp_error( $result ) ) {
|
2022-09-20 18:59:10 +02:00
|
|
|
$result = $this->flip( false, true );
|
Media/Upload: rotate images on upload according to EXIF Orientation.
Props msaggiorato, wpdavis, markoheijnen, dhuyvetter, msaggiorato, n7studios, triplejumper12, pbiron, mikeschroder, joemcgill, azaozz.
Fixes #14459.
Built from https://develop.svn.wordpress.org/trunk@46202
git-svn-id: http://core.svn.wordpress.org/trunk@46014 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-20 20:21:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
// Rotate 90 degrees counter-clockwise.
|
|
|
|
$result = $this->rotate( 90 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2012-10-01 22:59:06 +02:00
|
|
|
/**
|
|
|
|
* Either calls editor's save function or handles file as a stream.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2021-07-01 23:02:57 +02:00
|
|
|
* @param string $filename
|
Code Modernization: Rename parameters that use reserved keywords in `wp-includes/class-wp-image-editor.php`.
While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.
This commit renames the `$function` parameter to `$callback` in `WP_Image_Editor::make_image()`.
Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270].
Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #55327.
Built from https://develop.svn.wordpress.org/trunk@53271
git-svn-id: http://core.svn.wordpress.org/trunk@52860 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-04-26 13:31:07 +02:00
|
|
|
* @param callable $callback
|
2021-07-01 23:02:57 +02:00
|
|
|
* @param array $arguments
|
2015-06-27 03:03:25 +02:00
|
|
|
* @return bool
|
2012-10-01 22:59:06 +02:00
|
|
|
*/
|
Code Modernization: Rename parameters that use reserved keywords in `wp-includes/class-wp-image-editor.php`.
While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.
This commit renames the `$function` parameter to `$callback` in `WP_Image_Editor::make_image()`.
Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270].
Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #55327.
Built from https://develop.svn.wordpress.org/trunk@53271
git-svn-id: http://core.svn.wordpress.org/trunk@52860 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-04-26 13:31:07 +02:00
|
|
|
protected function make_image( $filename, $callback, $arguments ) {
|
2019-07-03 01:42:58 +02:00
|
|
|
$stream = wp_is_stream( $filename );
|
|
|
|
if ( $stream ) {
|
2012-10-01 22:59:06 +02:00
|
|
|
ob_start();
|
2013-07-17 23:16:44 +02:00
|
|
|
} else {
|
2013-08-22 17:56:08 +02:00
|
|
|
// The directory containing the original file may no longer exist when using a replication plugin.
|
|
|
|
wp_mkdir_p( dirname( $filename ) );
|
2012-10-01 22:59:06 +02:00
|
|
|
}
|
|
|
|
|
Code Modernization: Rename parameters that use reserved keywords in `wp-includes/class-wp-image-editor.php`.
While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.
This commit renames the `$function` parameter to `$callback` in `WP_Image_Editor::make_image()`.
Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270].
Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #55327.
Built from https://develop.svn.wordpress.org/trunk@53271
git-svn-id: http://core.svn.wordpress.org/trunk@52860 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-04-26 13:31:07 +02:00
|
|
|
$result = call_user_func_array( $callback, $arguments );
|
2012-10-01 22:59:06 +02:00
|
|
|
|
2012-11-09 20:38:54 +01:00
|
|
|
if ( $result && $stream ) {
|
2012-10-01 22:59:06 +02:00
|
|
|
$contents = ob_get_contents();
|
|
|
|
|
2013-07-17 23:16:44 +02:00
|
|
|
$fp = fopen( $filename, 'w' );
|
2012-10-01 22:59:06 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! $fp ) {
|
2018-02-11 22:55:30 +01:00
|
|
|
ob_end_clean();
|
2012-10-01 22:59:06 +02:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2012-10-01 22:59:06 +02:00
|
|
|
|
|
|
|
fwrite( $fp, $contents );
|
|
|
|
fclose( $fp );
|
|
|
|
}
|
|
|
|
|
2012-11-09 20:38:54 +01:00
|
|
|
if ( $stream ) {
|
2012-10-01 22:59:06 +02:00
|
|
|
ob_end_clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns first matched mime-type from extension,
|
|
|
|
* as mapped from wp_get_mime_types()
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
2015-05-29 17:43:29 +02:00
|
|
|
*
|
2012-10-01 22:59:06 +02:00
|
|
|
* @param string $extension
|
2015-05-22 08:18:25 +02:00
|
|
|
* @return string|false
|
2012-10-01 22:59:06 +02:00
|
|
|
*/
|
|
|
|
protected static function get_mime_type( $extension = null ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! $extension ) {
|
2012-10-01 22:59:06 +02:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2012-10-01 22:59:06 +02:00
|
|
|
|
|
|
|
$mime_types = wp_get_mime_types();
|
|
|
|
$extensions = array_keys( $mime_types );
|
|
|
|
|
2015-08-25 22:28:22 +02:00
|
|
|
foreach ( $extensions as $_extension ) {
|
2012-11-09 20:38:54 +01:00
|
|
|
if ( preg_match( "/{$extension}/i", $_extension ) ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
return $mime_types[ $_extension ];
|
2012-10-01 22:59:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns first matched extension from Mime-type,
|
|
|
|
* as mapped from wp_get_mime_types()
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
2015-05-29 17:43:29 +02:00
|
|
|
*
|
2012-10-01 22:59:06 +02:00
|
|
|
* @param string $mime_type
|
2015-05-22 08:18:25 +02:00
|
|
|
* @return string|false
|
2012-10-01 22:59:06 +02:00
|
|
|
*/
|
|
|
|
protected static function get_extension( $mime_type = null ) {
|
2021-08-24 22:52:03 +02:00
|
|
|
if ( empty( $mime_type ) ) {
|
2012-10-01 22:59:06 +02:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2012-10-01 22:59:06 +02:00
|
|
|
|
2021-08-24 22:52:03 +02:00
|
|
|
return wp_get_default_extension_for_mime_type( $mime_type );
|
2012-10-01 22:59:06 +02:00
|
|
|
}
|
Media: enable generating multiple mime types for image uploads; specifically WebP versions for JPEG images by default.
This changeset adds the capability for core media uploads to generate sub sized images in more than a single mime type. The output formats for each mime type can be controlled through a filter. WebP is used as an additional output format for JPEG images by default to improve front end performance.
When generating additional mime types, only images which are smaller than the respective original are retained. By default, additional mime type images are only generated for the built-in core image sizes and any custom sizes that have opted in.
Image meta is updated with a new 'sources' array containing file details for each mime type. Each image size in the 'sizes' array also gets a new 'sources' array that contains the image file details for each mime type.
This change also increases image upload retries to accommodate additional image sizes. It also adds a `$mime_type` parameter to the `wp_get_missing_image_subsizes` function and filter.
This change adds three new filters to enable full control of secondary mime image generation and output:
* A new filter `wp_image_sizes_with_additional_mime_type_support` that filters the sizes that support secondary mime type output. Developers can use this to control the output of additional mime type sub-sized images on a per size basis.
* A new filter `wp_upload_image_mime_transforms` that filters the output mime types for a given input mime type. Developers can use this to control generation of additional mime types for a given input mime type or even override the original mime type.
* A new filter `wp_content_image_mimes` which controls image mime type output selection and order for frontend content. Developers can use this to control the mime type output preference order for content images. Content images inserted from the media library will use the available image versions based on the order from this filter.
Thanks to the many contributors who helped develop, test and give feedback on this feature.
A haiku to summarize:
Upload a JPEG
Images of all sizes
Output as WebPs
Props flixos90, MatthiasReinholz, studiolxv, markhowellsmead, eatingrules, pbiron, mukesh27, joegrainger, mehulkaklotar, tweetythierry, akshitsethi, peterwilsoncc, eugenemanuilov, mitogh, shetheliving, clarkeemily, codekraft, mikeschroder, clorith, kasparsd, spacedmonkey, trevorpfromsandee, jb510, scofennellgmailcom, seedsca, cagsmith, karinclimber, dainemawer, baxbridge, grapplerulrich, sobatkras, chynnabenton, tonylocalword, barneydavey, kwillmorth, garymatthews919, olliejones, imarkinteractive, jeffpaul, feastdesignco, webbeetle, masteradhoc.
See #55443.
Built from https://develop.svn.wordpress.org/trunk@53751
git-svn-id: http://core.svn.wordpress.org/trunk@53310 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-07-21 20:03:13 +02:00
|
|
|
}
|