WP_get_image_editor resizing images in a foreach loop

wp_get_image_editor can return a WP_Error object, which appears to be what’s happening in the code. The code never checks for this and tries to resize an image, thinking it’s calling resize on an image editing object.

You need to make 2 adjustments:

  • check if $editor is a WP_Error object
  • add code to print out the error objects message so you know what the error is

But finally, there’s another bug you’ve yet to encounter which will come next due to how you’re resizing. Consider the following:

  • we load file.png into memory, it’s 512×512
  • we now resize our image in memory from 512 to 32
  • we save the 32×32 image
  • we now resize our 32×32 image in memory to 64×64
  • we save the 64×64 image
  • the 64 image in memory gets resized to 128
  • etc..

All your resized images will be upscaled versions of the 32×32, not the original.

Also, consider that the resize might return neither true or an error object, but false