--- a/wp/wp-includes/class-wp-image-editor-gd.php Tue Oct 22 16:11:46 2019 +0200
+++ b/wp/wp-includes/class-wp-image-editor-gd.php Tue Dec 15 13:49:49 2020 +0100
@@ -23,7 +23,7 @@
public function __destruct() {
if ( $this->image ) {
- // we don't need the original in memory anymore
+ // We don't need the original in memory anymore.
imagedestroy( $this->image );
}
}
@@ -41,9 +41,9 @@
return false;
}
- // On some setups GD library does not provide imagerotate() - Ticket #11536
+ // On some setups GD library does not provide imagerotate() - Ticket #11536.
if ( isset( $args['methods'] ) &&
- in_array( 'rotate', $args['methods'] ) &&
+ in_array( 'rotate', $args['methods'], true ) &&
! function_exists( 'imagerotate' ) ) {
return false;
@@ -146,9 +146,9 @@
*
* @since 3.5.0
*
- * @param int|null $max_w Image width.
- * @param int|null $max_h Image height.
- * @param bool $crop
+ * @param int|null $max_w Image width.
+ * @param int|null $max_h Image height.
+ * @param bool $crop
* @return true|WP_Error
*/
public function resize( $max_w, $max_h, $crop = false ) {
@@ -171,16 +171,18 @@
}
/**
- * @param int $max_w
- * @param int $max_h
+ * @param int $max_w
+ * @param int $max_h
* @param bool|array $crop
* @return resource|WP_Error
*/
protected function _resize( $max_w, $max_h, $crop = false ) {
$dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop );
+
if ( ! $dims ) {
return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ), $this->file );
}
+
list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims;
$resized = wp_imagecreatetruecolor( $dst_w, $dst_h );
@@ -195,16 +197,24 @@
}
/**
- * Resize multiple images from a single source.
+ * Create multiple smaller images from a single source.
+ *
+ * Attempts to create all sub-sizes and returns the meta data at the end. This
+ * may result in the server running out of resources. When it fails there may be few
+ * "orphaned" images left over as the meta data is never returned and saved.
+ *
+ * As of 5.3.0 the preferred way to do this is with `make_subsize()`. It creates
+ * the new images one at a time and allows for the meta data to be saved after
+ * each new image is created.
*
* @since 3.5.0
*
* @param array $sizes {
- * An array of image size arrays. Default sizes are 'small', 'medium', 'medium_large', 'large'.
+ * An array of image size data arrays.
*
* 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.
+ * maintain aspect ratio according to the source image.
*
* @type array $size {
* Array of height, width values, and whether to crop.
@@ -217,46 +227,72 @@
* @return array An array of resized images' metadata by size.
*/
public function multi_resize( $sizes ) {
- $metadata = array();
- $orig_size = $this->size;
+ $metadata = array();
foreach ( $sizes as $size => $size_data ) {
- if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) {
- continue;
- }
-
- if ( ! isset( $size_data['width'] ) ) {
- $size_data['width'] = null;
- }
- if ( ! isset( $size_data['height'] ) ) {
- $size_data['height'] = null;
- }
-
- if ( ! isset( $size_data['crop'] ) ) {
- $size_data['crop'] = false;
- }
+ $meta = $this->make_subsize( $size_data );
- $image = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
- $duplicate = ( ( $orig_size['width'] == $size_data['width'] ) && ( $orig_size['height'] == $size_data['height'] ) );
-
- if ( ! is_wp_error( $image ) && ! $duplicate ) {
- $resized = $this->_save( $image );
-
- imagedestroy( $image );
-
- if ( ! is_wp_error( $resized ) && $resized ) {
- unset( $resized['path'] );
- $metadata[ $size ] = $resized;
- }
+ if ( ! is_wp_error( $meta ) ) {
+ $metadata[ $size ] = $meta;
}
-
- $this->size = $orig_size;
}
return $metadata;
}
/**
+ * Create an image sub-size and return the image meta data value for it.
+ *
+ * @since 5.3.0
+ *
+ * @param array $size_data {
+ * Array of size data.
+ *
+ * @type int $width The maximum width in pixels.
+ * @type int $height The maximum height in pixels.
+ * @type bool $crop Whether to crop the image to exact dimensions.
+ * }
+ * @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta,
+ * WP_Error object on error.
+ */
+ public function make_subsize( $size_data ) {
+ if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) {
+ return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) );
+ }
+
+ $orig_size = $this->size;
+
+ if ( ! isset( $size_data['width'] ) ) {
+ $size_data['width'] = null;
+ }
+
+ if ( ! isset( $size_data['height'] ) ) {
+ $size_data['height'] = null;
+ }
+
+ if ( ! isset( $size_data['crop'] ) ) {
+ $size_data['crop'] = false;
+ }
+
+ $resized = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
+
+ if ( is_wp_error( $resized ) ) {
+ $saved = $resized;
+ } else {
+ $saved = $this->_save( $resized );
+ imagedestroy( $resized );
+ }
+
+ $this->size = $orig_size;
+
+ if ( ! is_wp_error( $saved ) ) {
+ unset( $saved['path'] );
+ }
+
+ return $saved;
+ }
+
+ /**
* Crops Image.
*
* @since 3.5.0
@@ -271,8 +307,8 @@
* @return bool|WP_Error
*/
public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
- // If destination width/height isn't specified, use same as
- // width/height from source.
+ // If destination width/height isn't specified,
+ // use same as width/height from source.
if ( ! $dst_w ) {
$dst_w = $src_w;
}
@@ -379,10 +415,10 @@
}
/**
- * @param resource $image
+ * @param resource $image
* @param string|null $filename
* @param string|null $mime_type
- * @return WP_Error|array
+ * @return array|WP_Error
*/
protected function _save( $image, $filename = null, $mime_type = null ) {
list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type );
@@ -391,12 +427,12 @@
$filename = $this->generate_filename( null, null, $extension );
}
- if ( 'image/gif' == $mime_type ) {
+ if ( 'image/gif' === $mime_type ) {
if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) {
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
}
- } elseif ( 'image/png' == $mime_type ) {
- // convert from full colors to index colors, like original PNG.
+ } elseif ( 'image/png' === $mime_type ) {
+ // Convert from full colors to index colors, like original PNG.
if ( function_exists( 'imageistruecolor' ) && ! imageistruecolor( $image ) ) {
imagetruecolortopalette( $image, false, imagecolorstotal( $image ) );
}
@@ -404,7 +440,7 @@
if ( ! $this->make_image( $filename, 'imagepng', array( $image, $filename ) ) ) {
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
}
- } elseif ( 'image/jpeg' == $mime_type ) {
+ } elseif ( 'image/jpeg' === $mime_type ) {
if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->get_quality() ) ) ) {
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
}
@@ -412,20 +448,20 @@
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
}
- // Set correct file permissions
+ // Set correct file permissions.
$stat = stat( dirname( $filename ) );
- $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
- @ chmod( $filename, $perms );
+ $perms = $stat['mode'] & 0000666; // Same permissions as parent folder, strip off the executable bits.
+ chmod( $filename, $perms );
- /**
- * Filters the name of the saved image file.
- *
- * @since 2.6.0
- *
- * @param string $filename Name of the file.
- */
return array(
'path' => $filename,
+ /**
+ * Filters the name of the saved image file.
+ *
+ * @since 2.6.0
+ *
+ * @param string $filename Name of the file.
+ */
'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
'width' => $this->size['width'],
'height' => $this->size['height'],
@@ -463,8 +499,8 @@
* @since 3.5.0
*
* @param string|stream $filename
- * @param callable $function
- * @param array $arguments
+ * @param callable $function
+ * @param array $arguments
* @return bool
*/
protected function make_image( $filename, $function, $arguments ) {