Resize Image without cropping

When you upload an image to WordPress, the following happens:

  • The file is checked and moved into the appropriate location in the uploads folder
  • A post is created of type ‘attachment’
  • This post is filled with metadata from the uploaded file
  • For each image size defined, a copy of the original image is created according to what that image size specifies
  • Any code that references that image, does so using the attachment post ID, not the URL

By default, you will get:

  • The original image file
  • A ‘Large’ image
  • A ‘Medium’ image
  • A ‘Thumbnail’ image

So your question does not make any sense in those terms. You have an XY problem, and you’re trying to figure out a solution to your solution.

Instead of trying to resize the image to 100×100, and arriving at a completely brand new problem, you should have asked what the solution to the original problem was:

“How do I display an attachment at 100×100 without cropping?”

The answer lies in the image system.

When dealing with an image attachment, the APIs to grab the original image take a size parameter, e.g.:

the_post_thumbnail('medium');

What if you could specify your own size? The API lets you pass in an array like this:

the_post_thumbnail( array( 100,100 ) );

But this doesn’t give you a 100×100 image, it gives you the closest matching size that already exists, with height and width parameters of 100. it won’t go back and resize everything for you.

Instead, if we add a new image size, and specify that it doesn’t use cropping, like this:

// add_image_size( $name, $width, $height, $crop );
add_image_size( 'Lokis100x100', 100, 100, false );

Then do this:

the_post_thumbnail( 'Lokis100x100' );

You should get what you wanted. But there is one important thing to note. WordPress does its resizing on upload. You will need to regenerate those images or re-upload the image for it to work on those already on the site. There are numerous plugins such as regen thumbnails that will do this.

The ‘Lokis100x100’ image size I defined above will not crop the image. This means if I upload a 200×100 image, I will get a 100×50 image, downsized to fit into the specified dimensions. If crop was set to true, I would get a 100×100 image with the top and bottom cropped.

If somehow you are uploading images and storing their URLs, this is the wrong way to do things, and you should ask a question about this. I would recommend asking:

“When I upload an image file, I’m storing its URL. How do I handle this as an attachment post ID instead?”

As for wp_get_image_editor

Your code checks if the functions returned an error, but then makes no attempt to handle that scenario

For example:

$editor = wp_get_image_editor( $avatar_full_path );
if ( ! is_wp_error( $editor ) ) {
    ...
}

What happens if is_wp_error is true? Nothing. The $editor object would be a WP_Error object not an image editor, and it would contain a message telling you what went wrong, but because you’re not printing that message out, you don’t know what it says. These messages can be very clear about what went wrong, such as the file you specified doesn’t exist, or can’t be read, or the file couldn’t be saved

Leave a Comment