Undefined Define: IMAGE_EDIT_OVERWRITE

#

A couple of months ago I was working on a Trac ticket dealing with images and ran into the IMAGE_EDIT_OVERWRITE constant.

I had always assumed it did as it said: when set to true and you edit an image, overwrite the original image instead of creating a new one. But a quick test revealed this was not the case.

I took a quick run over to the Editing wp-config.php page on the WordPress Codex. But IMAGE_EDIT_OVERWRITE was nowhere to be found. In fact, a search of the Codex only uncovered one result: the WordPress 2.9 release notes, when the constant was first introduced. And unfortunately it said very little:

Add ‘IMAGE_EDIT_OVERWRITE’ constant to control edited image save or replace, most useful for setups that have dynamic image resizing

Dang. That’s not very helpful at all.

So I dove head first into the rabbit hole, reviewing the code and testing behaviour in the dashboard.

Default Behaviour

When you edit an image, WordPress’ default behaviour (i.e. IMAGE_EDIT_OVERWRITE is false) is to create a new image and leave the original image alone. Actually it creates a new set of images as it needs to generate all the smaller size images as well.

Let’s say you have the following photo…

photo-1024x683.jpg
photo-150x150.jpg
photo-300x200.jpg
photo.jpg

If you crop it, you would end up with something like this…

photo-1024x683.jpg
photo-150x150.jpg
photo-300x200.jpg
photo-e1430240013837-1024x1024.jpg
photo-e1430240013837-150x150.jpg
photo-e1430240013837-300x300.jpg
photo-e1430240013837.jpg
photo.jpg

If you crop it again, something like this…

photo-1024x683.jpg
photo-150x150.jpg
photo-300x200.jpg
photo-e1430240013837-1024x1024.jpg
photo-e1430240013837-150x150.jpg
photo-e1430240013837-300x300.jpg
photo-e1430240013837.jpg
photo-e1430240387218-150x150.jpg
photo-e1430240387218-150x300.jpg
photo-e1430240387218-512x1024.jpg
photo-e1430240387218.jpg
photo.jpg

Restore Original Image

Each time you edit, it adds a new set of images with a unique appendage.

If you restore the original image, the edits remain on your server. They are not removed. Again, this is the default behaviour, when IMAGE_EDIT_OVERWRITE is false.

Flip the Switch

When you add define( 'IMAGE_EDIT_OVERWRITE', true ); to your wp-config.php the behaviour changes. When you edit an image, it still creates a new image and leaves the original image alone. But when you edit again, it overwrites the first set of images rather than create a new set.

So in our example above, if we had IMAGE_EDIT_OVERWRITE set to true, we would instead end up with the following after the second crop…

photo-1024x683.jpg
photo-150x150.jpg
photo-300x200.jpg
photo-e1430240013837-1024x1024.jpg
photo-e1430240013837-150x150.jpg
photo-e1430240013837-150x300.jpg
photo-e1430240013837-300x300.jpg
photo-e1430240013837-512x1024.jpg
photo-e1430240013837.jpg
photo.jpg

Restore Original Image

As you can see, we just have one set of edited images. You can also see that it overwrote one edited image (i.e. 150×150), created new ones, and ignored others. I expected it to remove the 1024×1024 and 300×300 images, but it didn’t.

If you restore the original image, the set of edited images will be removed. But unfortunately, the images that I expected to be removed earlier are not removed now either…

photo-1024x683.jpg
photo-150x150.jpg
photo-300x200.jpg
photo-e1430240013837-1024x1024.jpg
photo-e1430240013837-300x300.jpg
photo.jpg

I’m guessing that these leftover images are the result of a bug and so I’ve created a new Trac ticket for this. I plan to dig into it on Friday for our monthly company WP Core Contrib Day.

I’ve also added a new Cleanup Image Edits section to the “Editing wp-config.php” page of the WordPress Codex.

I’m a bit perplexed as to why this constant exists at all. It seems like it should be the default behaviour. I think I’ll be turning on IMAGE_EDIT_OVERWRITE on all my sites going forward. I can’t think of a reason why I’d ever want rejected image edits sitting around on my server.

Have you run into this before or is this news to you? Let me know in the comments.

About the Author

Brad Touesnard

As founder of Delicious Brains Inc., Brad wears many hats; from coding and design, to marketing and partnerships. Before starting Delicious Brains, Brad was a busy freelance web developer, specializing in front-end development.