Post Pic

How to automatically use resized images instead of originals

This script will replace the uploaded image (if bigger than the larger size defined in your settings) by the large image generated by WordPress to save space in your server, and save bandwidth if you link a thumbnail to the original image, like when a lightbox plugin is used.

Simply paste the following code on your functions.php file and save it. No other action is needed!

function replace_uploaded_image($image_data) {
    // if there is no large image : return
    if (!isset($image_data['sizes']['large'])) return $image_data;

    // paths to the uploaded image and the large image
    $upload_dir = wp_upload_dir();
    $uploaded_image_location = $upload_dir['basedir'] . '/' .$image_data['file'];
    $large_image_location = $upload_dir['path'] . '/'.$image_data['sizes']['large']['file'];

    // delete the uploaded image
    unlink($uploaded_image_location);

    // rename the large image
    rename($large_image_location,$uploaded_image_location);

    // update image metadata and return them
    $image_data['width'] = $image_data['sizes']['large']['width'];
    $image_data['height'] = $image_data['sizes']['large']['height'];
    unset($image_data['sizes']['large']);

    return $image_data;
}
add_filter('wp_generate_attachment_metadata','replace_uploaded_image');

Thanks to Serge Rauberfor sharing his great tip with us!

13 Responses

Feb 08 2010 14:32

Could be useful :) Thx Jean

Feb 08 2010 16:56

Nice script, it will save time by not having to manually resize images.

Merci beaucoup !

Feb 08 2010 22:30

Thanks for giving the necessary code.

Feb 09 2010 14:26

Very useful for clients who upload direct from their cameras.

Feb 09 2010 14:34

- For PHP developers -

How could I get this scripts and insert a watermark on my upload images?

I’m needing something like this for now… thanks.

Feb 12 2010 02:02

if you want to add watermark on the image you should look at a solution like Smart Image Resizer.

shiftingpixel.com/2008/03/03/smart-image-resizer/

if you read in the comments, there is one about adding watermark functionality to it.

hope it helps

Mar 22 2010 20:28

Wow, this helps soooo much. This is the number one problem I have with my clients. No matter how many times I tell them to resize their pictures, there are always some that don’t.

I was thinking of building a filter that automatically linked to the large image version, instead of the original, but this eliminates the problem earlier in the process, and saves storage on top of it.

Thanks again, much appreciated.

May 12 2010 11:52

Oh thank you! I was searching this code for soo long… :)

Jun 14 2010 00:10

I used this code and got the proper width, but the image assumed the max large height – which made it not proportional.

Is there any way to define a max width in this code, and let the height determine itself – as opposed to have it take the large img size?

Thanks very much.

Aug 21 2010 03:57

it’s very useful, but on my blogs i use CDN from max, so it’s cheap, and server load is very fast.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required