How to Resize Your Images Programmatically Using WP Image Editor

How to Resize Your Images Programmatically Using WP Image Editor

John Hewick

Did you frequently have to use an image_resize function in WordPress to resize your images programmatically? You will probably have already noticed that you are now redirected to the WP Image Editor instead. This performs the same function as before, but there is a slightly different process to using it. Below, you can find out more information on how to resize images in place of this function.

Gran an Instance of the Image Editor

Once you have your image, you will then need to grab an instance of the image editor class. This will allow you to adjust the image located on the path.  When you do this, you will either receive an instance of the image editor you will get a WP_Error. If there is no error then you can start resizing the image.

Resize the Image

When resizing an image, there is only three pieces of information that you need. This is the width, height and you need to know whether or not you want to crop the image. Cropping an image is usually preferred as this will ensure that there is no annoying stretching or distorting at odd aspect ratios.

The first thing that we would recommend that you do when resizing an image is find out what size the image currently is and then use a multiplier to make the new dimensions that you want to resize the image. So, if you were trying to resize an image and you wanted to increase the width by 40% and the height by 30%, you would need to know what the current size of the image is and then use the new width and height to resize the image. This really is a lot easier to follow than many people think.

Save the Image

Once you have resized the image, it is then vital that you remember to save it! This is the final stage of the process and the image will be written out to the uploads directory. You will also tend to find the year and month WordPress gives you here too.

Once you have done this, you will then be able to see the final version which has been resized and the file will also be saved appropriately.

Now that you know the best way to resize your images programmatically using WP Image Editor, you can successfully upload and resize images to fit perfectly into your WordPress blog. If this article helped you, let us know in the comments, or you can also let us know if you have any more top tips when resizing an image.

John Hewick

Author John Hewick

John is a full stack developer, with 10 years’ experience building websites with WordPress. Working with Elementary Digital for the last 4 years John has met all the challenges that have been sent his way.

More posts by John Hewick