2012-10-01 16:59:06 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Base WordPress Image Editor
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage Image_Editor
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-11-22 04:52:16 -05:00
|
|
|
* Base image editor class from which implementations extend
|
2012-10-01 16:59:06 -04:00
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
abstract class WP_Image_Editor {
|
2017-11-30 18:11:00 -05:00
|
|
|
protected $file = null;
|
|
|
|
protected $size = null;
|
|
|
|
protected $mime_type = null;
|
2012-10-01 16:59:06 -04:00
|
|
|
protected $default_mime_type = 'image/jpeg';
|
2017-11-30 18:11:00 -05:00
|
|
|
protected $quality = false;
|
|
|
|
protected $default_quality = 82;
|
2012-10-01 16:59:06 -04:00
|
|
|
|
2012-11-22 04:52:16 -05:00
|
|
|
/**
|
|
|
|
* Each instance handles a single file.
|
2016-01-09 20:26:25 -05:00
|
|
|
*
|
|
|
|
* @param string $file Path to the file to load.
|
2012-11-22 04:52:16 -05:00
|
|
|
*/
|
|
|
|
public function __construct( $file ) {
|
|
|
|
$this->file = $file;
|
2012-10-01 16:59:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-11-22 04:52:16 -05:00
|
|
|
* Checks to see if current environment supports the editor chosen.
|
2019-11-17 02:43:01 -05:00
|
|
|
* Must be overridden in a subclass.
|
2012-10-01 16:59:06 -04:00
|
|
|
*
|
|
|
|
* @since 3.5.0
|
2015-05-29 11:43:29 -04:00
|
|
|
*
|
2012-11-22 04:52:16 -05:00
|
|
|
* @abstract
|
2012-10-01 16:59:06 -04:00
|
|
|
*
|
2012-11-22 04:52:16 -05:00
|
|
|
* @param array $args
|
2015-06-26 21:03:25 -04:00
|
|
|
* @return bool
|
2012-10-01 16:59:06 -04:00
|
|
|
*/
|
2012-11-22 04:52:16 -05:00
|
|
|
public static function test( $args = array() ) {
|
|
|
|
return false;
|
2012-10-01 16:59:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-11-22 04:52:16 -05:00
|
|
|
* Checks to see if editor supports the mime-type specified.
|
2019-11-17 02:43:01 -05:00
|
|
|
* Must be overridden in a subclass.
|
2012-10-01 16:59:06 -04:00
|
|
|
*
|
|
|
|
* @since 3.5.0
|
2015-05-29 11:43:29 -04:00
|
|
|
*
|
2012-11-22 04:52:16 -05:00
|
|
|
* @abstract
|
2012-10-01 16:59:06 -04:00
|
|
|
*
|
2012-11-22 04:52:16 -05:00
|
|
|
* @param string $mime_type
|
2015-06-26 21:03:25 -04:00
|
|
|
* @return bool
|
2012-10-01 16:59:06 -04:00
|
|
|
*/
|
2012-11-22 04:52:16 -05:00
|
|
|
public static function supports_mime_type( $mime_type ) {
|
2012-11-09 14:38:54 -05:00
|
|
|
return false;
|
2012-10-01 16:59:06 -04:00
|
|
|
}
|
|
|
|
|
2012-11-16 17:02:21 -05:00
|
|
|
/**
|
|
|
|
* Loads image from $this->file into editor.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
* @abstract
|
|
|
|
*
|
2021-01-03 17:04:04 -05:00
|
|
|
* @return true|WP_Error True if loaded; WP_Error on failure.
|
2012-11-16 17:02:21 -05:00
|
|
|
*/
|
2012-11-22 04:52:16 -05:00
|
|
|
abstract public function load();
|
2012-11-16 17:02:21 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves current image to file.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
* @abstract
|
|
|
|
*
|
|
|
|
* @param string $destfilename
|
|
|
|
* @param string $mime_type
|
|
|
|
* @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string}
|
|
|
|
*/
|
2012-10-01 16:59:06 -04:00
|
|
|
abstract public function save( $destfilename = null, $mime_type = null );
|
2012-11-09 14:38:54 -05:00
|
|
|
|
|
|
|
/**
|
2012-11-16 17:02:21 -05:00
|
|
|
* Resizes current image.
|
|
|
|
*
|
2014-03-27 16:40:17 -04: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 17:02:21 -05:00
|
|
|
* @since 3.5.0
|
|
|
|
* @abstract
|
|
|
|
*
|
2020-06-20 08:58:10 -04:00
|
|
|
* @param int|null $max_w Image width.
|
|
|
|
* @param int|null $max_h Image height.
|
|
|
|
* @param bool $crop
|
2021-01-03 17:04:04 -05:00
|
|
|
* @return true|WP_Error
|
2012-11-16 17:02:21 -05:00
|
|
|
*/
|
|
|
|
abstract public function resize( $max_w, $max_h, $crop = false );
|
|
|
|
|
|
|
|
/**
|
2013-10-08 13:19:09 -04:00
|
|
|
* Resize multiple images from a single source.
|
2013-04-22 16:28:05 -04:00
|
|
|
*
|
2012-11-16 17:02:21 -05:00
|
|
|
* @since 3.5.0
|
|
|
|
* @abstract
|
|
|
|
*
|
2013-10-08 13:19:09 -04:00
|
|
|
* @param array $sizes {
|
|
|
|
* An array of image size arrays. Default sizes are 'small', 'medium', 'large'.
|
|
|
|
*
|
|
|
|
* @type array $size {
|
|
|
|
* @type int $width Image width.
|
|
|
|
* @type int $height Image height.
|
|
|
|
* @type bool $crop Optional. Whether to crop the image. Default false.
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* @return array An array of resized images metadata by size.
|
2012-11-16 17:02:21 -05:00
|
|
|
*/
|
|
|
|
abstract public function multi_resize( $sizes );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Crops Image.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
* @abstract
|
|
|
|
*
|
2020-07-22 20:52:05 -04: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-26 21:03:25 -04:00
|
|
|
* @param bool $src_abs Optional. If the source crop points are absolute.
|
2021-01-03 17:04:04 -05:00
|
|
|
* @return true|WP_Error
|
2012-11-16 17:02:21 -05: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 17:04:04 -05:00
|
|
|
* @return true|WP_Error
|
2012-11-16 17:02:21 -05:00
|
|
|
*/
|
|
|
|
abstract public function rotate( $angle );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flips current image.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
* @abstract
|
|
|
|
*
|
2015-06-26 21:03:25 -04:00
|
|
|
* @param bool $horz Flip along Horizontal Axis
|
|
|
|
* @param bool $vert Flip along Vertical Axis
|
2021-01-03 17:04:04 -05:00
|
|
|
* @return true|WP_Error
|
2012-11-16 17:02:21 -05:00
|
|
|
*/
|
|
|
|
abstract public function flip( $horz, $vert );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Streams current image to browser.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
* @abstract
|
|
|
|
*
|
2017-08-22 07:12:44 -04:00
|
|
|
* @param string $mime_type The mime type of the image.
|
2021-01-03 17:04:04 -05:00
|
|
|
* @return true|WP_Error True on success, WP_Error object on failure.
|
2012-11-09 14:38:54 -05:00
|
|
|
*/
|
2012-11-16 17:02:21 -05:00
|
|
|
abstract public function stream( $mime_type = null );
|
2012-11-09 14:38:54 -05:00
|
|
|
|
2012-10-01 16:59:06 -04:00
|
|
|
/**
|
2012-11-16 17:02:21 -05:00
|
|
|
* Gets dimensions of image.
|
2012-10-01 16:59:06 -04:00
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2019-11-05 16:27:02 -05:00
|
|
|
* @return array {
|
|
|
|
* Dimensions of the image.
|
|
|
|
*
|
|
|
|
* @type int $width The image width.
|
|
|
|
* @type int $height The image height.
|
|
|
|
* }
|
2012-10-01 16:59:06 -04:00
|
|
|
*/
|
|
|
|
public function get_size() {
|
|
|
|
return $this->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-11-16 17:02:21 -05:00
|
|
|
* Sets current image size.
|
2012-10-01 16:59:06 -04:00
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @param int $width
|
|
|
|
* @param int $height
|
2014-12-20 15:56:22 -05:00
|
|
|
* @return true
|
2012-10-01 16:59:06 -04:00
|
|
|
*/
|
|
|
|
protected function update_size( $width = null, $height = null ) {
|
|
|
|
$this->size = array(
|
2017-11-30 18:11:00 -05:00
|
|
|
'width' => (int) $width,
|
|
|
|
'height' => (int) $height,
|
2012-10-01 16:59:06 -04:00
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-27 23:50:15 -04:00
|
|
|
* Gets the Image Compression quality on a 1-100% scale.
|
2012-10-01 16:59:06 -04:00
|
|
|
*
|
2014-06-27 23:50:15 -04:00
|
|
|
* @since 4.0.0
|
2012-10-01 16:59:06 -04:00
|
|
|
*
|
2020-06-19 18:55:12 -04:00
|
|
|
* @return int Compression Quality. Range: [1,100]
|
2012-10-01 16:59:06 -04:00
|
|
|
*/
|
2014-06-27 23:50:15 -04:00
|
|
|
public function get_quality() {
|
|
|
|
if ( ! $this->quality ) {
|
2014-12-08 11:19:23 -05: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 02:18:25 -04:00
|
|
|
* @return true|WP_Error True if set successfully; WP_Error on failure.
|
2014-12-08 11:19:23 -05:00
|
|
|
*/
|
|
|
|
public function set_quality( $quality = null ) {
|
|
|
|
if ( null === $quality ) {
|
2013-12-04 18:32:11 -05:00
|
|
|
/**
|
2016-05-22 14:15:28 -04:00
|
|
|
* Filters the default image compression quality setting.
|
2013-12-04 18:32:11 -05:00
|
|
|
*
|
2014-12-15 16:43:22 -05:00
|
|
|
* Applies only during initial editor instantiation, or when set_quality() is run
|
|
|
|
* manually without the `$quality` argument.
|
|
|
|
*
|
2020-07-22 20:52:05 -04:00
|
|
|
* The WP_Image_Editor::set_quality() method has priority over the filter.
|
2014-12-15 16:43:22 -05:00
|
|
|
*
|
2014-06-27 23:50:15 -04:00
|
|
|
* @since 3.5.0
|
2013-12-04 18:32:11 -05:00
|
|
|
*
|
2014-06-27 23:50:15 -04:00
|
|
|
* @param int $quality Quality level between 1 (low) and 100 (high).
|
|
|
|
* @param string $mime_type Image mime type.
|
2013-12-04 18:32:11 -05:00
|
|
|
*/
|
2014-06-27 23:50:15 -04:00
|
|
|
$quality = apply_filters( 'wp_editor_set_quality', $this->default_quality, $this->mime_type );
|
|
|
|
|
2020-05-16 14:42:12 -04:00
|
|
|
if ( 'image/jpeg' === $this->mime_type ) {
|
2014-06-27 23:50:15 -04:00
|
|
|
/**
|
2016-05-22 14:15:28 -04:00
|
|
|
* Filters the JPEG compression quality for backward-compatibility.
|
2014-06-27 23:50:15 -04:00
|
|
|
*
|
2014-12-15 16:43:22 -05:00
|
|
|
* Applies only during initial editor instantiation, or when set_quality() is run
|
|
|
|
* manually without the `$quality` argument.
|
|
|
|
*
|
2020-07-22 20:52:05 -04:00
|
|
|
* The WP_Image_Editor::set_quality() method has priority over the filter.
|
2014-12-15 16:43:22 -05:00
|
|
|
*
|
2014-06-27 23:50:15 -04: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 09:12:15 -04:00
|
|
|
}
|
2013-12-04 17:49:10 -05:00
|
|
|
|
2014-12-08 11:19:23 -05:00
|
|
|
if ( $quality < 0 || $quality > 100 ) {
|
|
|
|
$quality = $this->default_quality;
|
2013-12-04 17:49:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-13 14:41:31 -04:00
|
|
|
// Allow 0, but squash to 1 due to identical images in GD, and for backward compatibility.
|
2014-10-04 09:12:15 -04:00
|
|
|
if ( 0 === $quality ) {
|
2014-06-27 23:50:15 -04:00
|
|
|
$quality = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ( $quality >= 1 ) && ( $quality <= 100 ) ) {
|
2013-12-04 17:49:10 -05:00
|
|
|
$this->quality = $quality;
|
|
|
|
return true;
|
|
|
|
} else {
|
2017-11-30 18:11:00 -05:00
|
|
|
return new WP_Error( 'invalid_image_quality', __( 'Attempted to set image quality outside of the range [1,100].' ) );
|
2013-12-04 17:49:10 -05:00
|
|
|
}
|
2012-10-01 16:59:06 -04: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 15:10:33 -05:00
|
|
|
* @param string $mime_type
|
2012-10-01 16:59:06 -04:00
|
|
|
* @return array { filename|null, extension, mime-type }
|
|
|
|
*/
|
|
|
|
protected function get_output_format( $filename = null, $mime_type = null ) {
|
2014-05-06 14:37:29 -04:00
|
|
|
$new_ext = null;
|
2012-10-01 16:59:06 -04:00
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// By default, assume specified type takes priority.
|
2012-10-01 16:59:06 -04:00
|
|
|
if ( $mime_type ) {
|
|
|
|
$new_ext = $this->get_extension( $mime_type );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $filename ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
$file_ext = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) );
|
2012-10-01 16:59:06 -04:00
|
|
|
$file_mime = $this->get_mime_type( $file_ext );
|
2017-11-30 18:11:00 -05:00
|
|
|
} else {
|
2012-10-01 16:59:06 -04:00
|
|
|
// If no file specified, grab editor's current extension and mime-type.
|
2017-11-30 18:11:00 -05:00
|
|
|
$file_ext = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) );
|
2012-10-01 16:59:06 -04:00
|
|
|
$file_mime = $this->mime_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if specified mime-type is the same as type implied by
|
2020-01-28 19:45:18 -05:00
|
|
|
// file extension. If so, prefer extension from file.
|
2012-10-01 16:59:06 -04:00
|
|
|
if ( ! $mime_type || ( $file_mime == $mime_type ) ) {
|
|
|
|
$mime_type = $file_mime;
|
2017-11-30 18:11:00 -05:00
|
|
|
$new_ext = $file_ext;
|
2012-10-01 16:59:06 -04:00
|
|
|
}
|
|
|
|
|
2021-05-20 18:15:56 -04:00
|
|
|
/**
|
|
|
|
* Filters the image editor output format mapping.
|
|
|
|
*
|
|
|
|
* Enables filtering the mime type used to save images. By default,
|
|
|
|
* the mapping array is empty, so the mime type matches the source image.
|
|
|
|
*
|
2021-07-15 13:39:59 -04:00
|
|
|
* This should be considered experimental as there are a few edge cases that
|
|
|
|
* still need to be solved.
|
|
|
|
*
|
|
|
|
* When this filter is used in combination with the {@see 'wp_editor_set_quality'}
|
|
|
|
* filter, the image quality value used will be that of the original mime type,
|
|
|
|
* not the mapped one. This could result in unexpected image quality for generated
|
|
|
|
* images. See https://core.trac.wordpress.org/ticket/53667 for more details.
|
|
|
|
*
|
|
|
|
* When a mime type is mapped to another, and two images with the same name are
|
|
|
|
* uploaded (image.jpg and image.webp, for example), the generated images for one
|
|
|
|
* will potentially overwrite the other's.
|
|
|
|
* See https://core.trac.wordpress.org/ticket/53668 for more details.
|
|
|
|
*
|
2021-05-22 09:00:02 -04:00
|
|
|
* @see WP_Image_Editor::get_output_format()
|
2021-05-20 18:15:56 -04:00
|
|
|
*
|
|
|
|
* @since 5.8.0
|
|
|
|
*
|
2021-06-23 15:05:57 -04:00
|
|
|
* @param string[] $output_format {
|
2021-05-20 18:15:56 -04:00
|
|
|
* An array of mime type mappings. Maps a source mime type to a new
|
2021-05-22 09:00:02 -04:00
|
|
|
* destination mime type. Default empty array.
|
2021-05-20 18:15:56 -04:00
|
|
|
*
|
2021-06-23 15:05:57 -04:00
|
|
|
* @type string ...$0 The new mime type.
|
|
|
|
* }
|
2021-05-22 09:00:02 -04:00
|
|
|
* @param string $filename Path to the image.
|
2021-05-20 18:15:56 -04:00
|
|
|
* @param string $mime_type The source image mime type.
|
|
|
|
* }
|
|
|
|
*/
|
2021-05-22 09:00:02 -04:00
|
|
|
$output_format = apply_filters( 'image_editor_output_format', array(), $filename, $mime_type );
|
2021-05-20 18:15:56 -04:00
|
|
|
|
2021-05-22 09:00:02 -04:00
|
|
|
if ( isset( $output_format[ $mime_type ] )
|
|
|
|
&& $this->supports_mime_type( $output_format[ $mime_type ] )
|
2021-05-20 18:15:56 -04:00
|
|
|
) {
|
2021-05-22 09:00:02 -04:00
|
|
|
$mime_type = $output_format[ $mime_type ];
|
|
|
|
$new_ext = $this->get_extension( $mime_type );
|
2021-05-20 18:15:56 -04:00
|
|
|
}
|
|
|
|
|
2012-10-01 16:59:06 -04:00
|
|
|
// Double-check that the mime-type selected is supported by the editor.
|
|
|
|
// If not, choose a default instead.
|
|
|
|
if ( ! $this->supports_mime_type( $mime_type ) ) {
|
2013-10-06 18:19:10 -04:00
|
|
|
/**
|
2016-05-22 14:15:28 -04:00
|
|
|
* Filters default mime type prior to getting the file extension.
|
2013-10-07 12:28:15 -04:00
|
|
|
*
|
|
|
|
* @see wp_get_mime_types()
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @param string $mime_type Mime type string.
|
|
|
|
*/
|
2012-10-01 16:59:06 -04:00
|
|
|
$mime_type = apply_filters( 'image_editor_default_mime_type', $this->default_mime_type );
|
2017-11-30 18:11:00 -05:00
|
|
|
$new_ext = $this->get_extension( $mime_type );
|
2012-10-01 16:59:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $filename ) {
|
2016-08-20 19:36:28 -04:00
|
|
|
$dir = pathinfo( $filename, PATHINFO_DIRNAME );
|
|
|
|
$ext = pathinfo( $filename, PATHINFO_EXTENSION );
|
2012-10-01 16:59:06 -04:00
|
|
|
|
|
|
|
$filename = trailingslashit( $dir ) . wp_basename( $filename, ".$ext" ) . ".{$new_ext}";
|
|
|
|
}
|
|
|
|
|
|
|
|
return array( $filename, $new_ext, $mime_type );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds an output filename based on current file, and adding proper suffix
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @param string $suffix
|
|
|
|
* @param string $dest_path
|
|
|
|
* @param string $extension
|
|
|
|
* @return string filename
|
|
|
|
*/
|
|
|
|
public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) {
|
2020-01-28 19:45:18 -05:00
|
|
|
// $suffix will be appended to the destination filename, just before the extension.
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! $suffix ) {
|
2012-10-01 16:59:06 -04:00
|
|
|
$suffix = $this->get_suffix();
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2012-10-01 16:59:06 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
$dir = pathinfo( $this->file, PATHINFO_DIRNAME );
|
|
|
|
$ext = pathinfo( $this->file, PATHINFO_EXTENSION );
|
2012-10-01 16:59:06 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
$name = wp_basename( $this->file, ".$ext" );
|
2012-10-01 16:59:06 -04:00
|
|
|
$new_ext = strtolower( $extension ? $extension : $ext );
|
|
|
|
|
2019-07-02 19:42:58 -04: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 10:37:05 -04:00
|
|
|
if ( ! wp_is_stream( $dest_path ) ) {
|
|
|
|
$_dest_path = realpath( $dest_path );
|
|
|
|
if ( $_dest_path ) {
|
|
|
|
$dir = $_dest_path;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$dir = $dest_path;
|
2019-07-02 19:42:58 -04:00
|
|
|
}
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2012-10-01 16:59:06 -04:00
|
|
|
|
|
|
|
return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds and returns proper suffix for file based on height and width.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2020-01-11 13:32:05 -05:00
|
|
|
* @return string|false suffix
|
2012-10-01 16:59:06 -04:00
|
|
|
*/
|
|
|
|
public function get_suffix() {
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! $this->get_size() ) {
|
2012-10-01 16:59:06 -04:00
|
|
|
return false;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2012-10-01 16:59:06 -04: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 14:21:57 -04: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'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters the `$orientation` value to correct it before rotating or to prevemnt rotating the image.
|
|
|
|
*
|
|
|
|
* @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 11:55:09 -05: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 14:21:57 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( $orientation ) {
|
|
|
|
case 2:
|
|
|
|
// Flip horizontally.
|
|
|
|
$result = $this->flip( true, false );
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
// Rotate 180 degrees or flip horizontally and vertically.
|
2020-01-28 19:45:18 -05:00
|
|
|
// 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 14:21:57 -04:00
|
|
|
$result = $this->flip( true, true );
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
// Flip vertically.
|
|
|
|
$result = $this->flip( false, true );
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
// Rotate 90 degrees counter-clockwise and flip vertically.
|
|
|
|
$result = $this->rotate( 90 );
|
|
|
|
|
|
|
|
if ( ! is_wp_error( $result ) ) {
|
|
|
|
$result = $this->flip( false, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
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 ) ) {
|
|
|
|
$result = $this->flip( true, false );
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
// Rotate 90 degrees counter-clockwise.
|
|
|
|
$result = $this->rotate( 90 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2012-10-01 16:59:06 -04:00
|
|
|
/**
|
|
|
|
* Either calls editor's save function or handles file as a stream.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2021-07-01 17:02:57 -04:00
|
|
|
* @param string $filename
|
|
|
|
* @param callable $function
|
|
|
|
* @param array $arguments
|
2015-06-26 21:03:25 -04:00
|
|
|
* @return bool
|
2012-10-01 16:59:06 -04:00
|
|
|
*/
|
|
|
|
protected function make_image( $filename, $function, $arguments ) {
|
2019-07-02 19:42:58 -04:00
|
|
|
$stream = wp_is_stream( $filename );
|
|
|
|
if ( $stream ) {
|
2012-10-01 16:59:06 -04:00
|
|
|
ob_start();
|
2013-07-17 17:16:44 -04:00
|
|
|
} else {
|
2013-08-22 11:56:08 -04:00
|
|
|
// The directory containing the original file may no longer exist when using a replication plugin.
|
|
|
|
wp_mkdir_p( dirname( $filename ) );
|
2012-10-01 16:59:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$result = call_user_func_array( $function, $arguments );
|
|
|
|
|
2012-11-09 14:38:54 -05:00
|
|
|
if ( $result && $stream ) {
|
2012-10-01 16:59:06 -04:00
|
|
|
$contents = ob_get_contents();
|
|
|
|
|
2013-07-17 17:16:44 -04:00
|
|
|
$fp = fopen( $filename, 'w' );
|
2012-10-01 16:59:06 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! $fp ) {
|
2018-02-11 16:55:30 -05:00
|
|
|
ob_end_clean();
|
2012-10-01 16:59:06 -04:00
|
|
|
return false;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2012-10-01 16:59:06 -04:00
|
|
|
|
|
|
|
fwrite( $fp, $contents );
|
|
|
|
fclose( $fp );
|
|
|
|
}
|
|
|
|
|
2012-11-09 14:38:54 -05:00
|
|
|
if ( $stream ) {
|
2012-10-01 16:59:06 -04: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 11:43:29 -04:00
|
|
|
*
|
2012-10-01 16:59:06 -04:00
|
|
|
* @param string $extension
|
2015-05-22 02:18:25 -04:00
|
|
|
* @return string|false
|
2012-10-01 16:59:06 -04:00
|
|
|
*/
|
|
|
|
protected static function get_mime_type( $extension = null ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! $extension ) {
|
2012-10-01 16:59:06 -04:00
|
|
|
return false;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2012-10-01 16:59:06 -04:00
|
|
|
|
|
|
|
$mime_types = wp_get_mime_types();
|
|
|
|
$extensions = array_keys( $mime_types );
|
|
|
|
|
2015-08-25 16:28:22 -04:00
|
|
|
foreach ( $extensions as $_extension ) {
|
2012-11-09 14:38:54 -05:00
|
|
|
if ( preg_match( "/{$extension}/i", $_extension ) ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
return $mime_types[ $_extension ];
|
2012-10-01 16:59:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns first matched extension from Mime-type,
|
|
|
|
* as mapped from wp_get_mime_types()
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
2015-05-29 11:43:29 -04:00
|
|
|
*
|
2012-10-01 16:59:06 -04:00
|
|
|
* @param string $mime_type
|
2015-05-22 02:18:25 -04:00
|
|
|
* @return string|false
|
2012-10-01 16:59:06 -04:00
|
|
|
*/
|
|
|
|
protected static function get_extension( $mime_type = null ) {
|
2020-04-04 23:02:11 -04:00
|
|
|
$extensions = explode( '|', array_search( $mime_type, wp_get_mime_types(), true ) );
|
2012-10-01 16:59:06 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( empty( $extensions[0] ) ) {
|
2012-10-01 16:59:06 -04:00
|
|
|
return false;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2012-10-01 16:59:06 -04:00
|
|
|
|
|
|
|
return $extensions[0];
|
|
|
|
}
|
2012-11-09 15:10:33 -05:00
|
|
|
}
|
2012-11-22 04:52:16 -05:00
|
|
|
|