wpseek.com
A WordPress-centric search engine for devs and theme authors



_wp_maybe_scale_and_rotate_image › WordPress Function

Since6.1.0
Deprecatedn/a
_wp_maybe_scale_and_rotate_image ( $file, $attachment_id, $imagesize, $exif_meta, $mime_type )
Access:
  • private
Parameters: (5)
  • (string) $file Full path to the image file.
    Required: Yes
  • (int) $attachment_id Attachment ID.
    Required: Yes
  • (array) $imagesize { Indexed array of the image width and height in pixels. @type int $0 The image width. @type int $1 The image height. }
    Required: Yes
  • (array|null) $exif_meta EXIF metadata if extracted from the image file.
    Required: Yes
  • (string) $mime_type Output mime type.
    Required: Yes
Returns:
  • (array) Array with three entries: The WP_Image_Editor instance, whether the image was resized, and whether the image was rotated (booleans). Each entry can alternatively be a WP_Error in case something went wrong.
Defined at:
Codex:

Returns a WP_Image_Editor instance where the image file has been scaled and rotated as necessary.



Source

function _wp_maybe_scale_and_rotate_image( $file, $attachment_id, $imagesize, $exif_meta, $mime_type ) {
	$resized = false;
	$rotated = false;

	$editor = wp_get_image_editor( $file, array( 'mime_type' => $mime_type ) );
	if ( is_wp_error( $editor ) ) {
		// This image cannot be edited.
		return array( $editor, $resized, $rotated );
	}

	if ( ! empty( $mime_type ) ) {
		$editor->set_output_mime_type( $mime_type );
	}

	// Do not scale (large) PNG images. May result in sub-sizes that have greater file size than the original. See #48736.
	if ( 'image/png' !== $mime_type ) {
		/**
		 * Filters the "BIG image" threshold value.
		 *
		 * If the original image width or height is above the threshold, it will be scaled down. The threshold is
		 * used as max width and max height. The scaled down image will be used as the largest available size, including
		 * the `_wp_attached_file` post meta value.
		 *
		 * Returning `false` from the filter callback will disable the scaling.
		 *
		 * @since 5.3.0
		 *
		 * @param int    $threshold     The threshold value in pixels. Default 2560.
		 * @param array  $imagesize     {
		 *     Indexed array of the image width and height in pixels.
		 *
		 *     @type int $0 The image width.
		 *     @type int $1 The image height.
		 * }
		 * @param string $file          Full path to the uploaded image file.
		 * @param int    $attachment_id Attachment post ID.
		 */
		$threshold = (int) apply_filters( 'big_image_size_threshold', 2560, $imagesize, $file, $attachment_id );

		// If the original image's dimensions are over the threshold,
		// scale the image and use it as the "full" size.
		if ( $threshold && ( $imagesize[0] > $threshold || $imagesize[1] > $threshold ) ) {
			// Resize the image.
			$resized = $editor->resize( $threshold, $threshold );

			// If there is EXIF data, rotate according to EXIF Orientation.
			if ( ! is_wp_error( $resized ) && is_array( $exif_meta ) ) {
				$rotated = $editor->maybe_exif_rotate();
			}
		} elseif ( ! empty( $exif_meta['orientation'] ) && 1 !== (int) $exif_meta['orientation'] ) {
			// Rotate the whole original image if there is EXIF data and "orientation" is not 1.
			$rotated = $editor->maybe_exif_rotate();
		}
	}

	return array( $editor, $resized, $rotated );
}