🎉 Black Friday Sale is Live: Flat 30% Off on all Products

WordPress 5.3 handles big images automatically

WordPress 5.3 “Kirk” was released with most refined user experience and improved block editor on November 12, 2019. Apart from some major changes, it’s good news for user and developers that from now on WordPress will handle and manage big images automatically by generating optimized version of them.

This new feature is similar to what Imsanity and ShortPixel Image Optimizer plugins do but the plugins are not able to retain the original size.

big_image_size_threshold

5.3 has introduced a new filter big_image_size_threshold which filters the default threshold value( 2560px ). If an image’s height or width is greater than the default threshold value then WordPress will automatically scale down the image and use it as the main image.

More about the new filter big_image_size_threshold from source code:

/**
 * 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 that order).
 * @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 );

wp_get_original_image_path

But the original image remains intact and it can rendered using a new function wp_get_original_image_path() or wp_get_original_image_url().

More about the new filter wp_get_original_image_path() from source code:

/**
 * Retrieves the path to an uploaded image file.
 *
 * Similar to `get_attached_file()` however some images may have been processed after uploading
 * to make them suitable for web use. In this case the attached "full" size file is usually replaced
 * with a scaled down version of the original image. This function always returns the path
 * to the originally uploaded image file.
 *
 * @since 5.3.0
 *
 * @param int $attachment_id Attachment ID.
 * @return string|false Path to the original image file or false if the attachment is not an image.
 */
if ( ! wp_attachment_is_image( $attachment_id ) ) {
		return false;
	}

	$image_meta = wp_get_attachment_metadata( $attachment_id );
	$image_file = get_attached_file( $attachment_id );

	if ( empty( $image_meta['original_image'] ) ) {
		$original_image = $image_file;
	} else {
		$original_image = path_join( dirname( $image_file ), $image_meta['original_image'] );
	}

	/**
	 * Filters the path to the original image.
	 *
	 * @since 5.3.0
	 *
	 * @param string $original_image Path to original image file.
	 * @param int    $attachment_id  Attachment ID.
	 */
	return apply_filters( 'wp_get_original_image_path', $original_image, $attachment_id );
}

Disabling the feature:

If a user or developer wants to use big images than the default threshold, then it can disabled by :
1. adding following code in your functions.php
add_filter( 'big_image_size_threshold', '__return_false' );
2. installing Disable “BIG Image” Threshold plugin:

Drawbacks:

Developers argue that it would be better if there is an option to remove the originally uploaded image to save the server space.

@webtrainingwheels argues the downside of the new feature being automatic:

  1. If a user actually wants a huge image for some reason, they’re going to be confused about what’s happening.
  2. By fixing the issue for them with no communication, we’re missing out on the opportunity to better inform people about why they shouldn’t be uploading massive images to begin with. Would be great to have a little UI message to communicate “hey, we fixed this huge image for you. Here’s why [insert link to helpful article]”

Know more about this feature here:
https://make.wordpress.org/core/2019/10/09/introducing-handling-of-big-images-in-wordpress-5-3/#utm_medium=referral&utm_source=facebook.com&utm_content=social
More about new features on WordPress 5.3:
https://wordpress.org/news/2019/11/kirk/

Leave a Reply

Your email address will not be published. Required fields are marked *