ThemeShaper Forums » Thematic

[closed]

Useful Image Resize Function

(7 posts)
  • Started 8 years ago by oakview
  • Latest reply from inzamam
  • This topic is not resolved
  1. oakview
    Member

    If you're like me, I spend time resizing images before uploading them to Wordpress, time I'd rather spend doing something else. I found this function which will replace the uploaded image (if bigger than the larger size defined in your settings) with the large image generated by WordPress to save space in your server, and save bandwidth if you link a thumbnail to the original image. Simply set the large size in the Wordpress admin backend to whatever you want and the function will do the rest. Enjoy!

    // Replace the uploaded image if bigger than the larger size defined
    // in your settings with the large image generated by WordPress.
    //
    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');
    Posted 8 years ago #
  2. nice share!

    Posted 8 years ago #
  3. tested. working.
    could be very useful but i'd pay attention to...
    -if you wish to restyle your theme in the future (e.g making it wider), you will have lost your original sized images

    Posted 8 years ago #
  4. oakview
    Member

    Point well taken Johnny. I was thinking more of how people use their cameras, most have the resolution set far above what can be reasonably displayed on a computer screen, so they resize before uploading.

    Either way, using the function or resizing before upload, your scenarion of "losing the original-sized images" will happen unless originals are stored safely if there's a chance they might be needed later on down the road.

    Posted 8 years ago #
  5. yes, i have huge problems with clients uploading images. they grab them direct from cameras that are suitable for display on 40" plasma screens.
    i also find WordPress doesn't handle large image sizes very well resulting in the 'http error' when uploading. there are hacks that exist but none have ever really fully sorted things for me.

    Posted 8 years ago #
  6. is there anything that resizes the fullsize image to a max size? there is already a plugin that resizes thumbnails, so image resizing is clearly possible.

    Posted 8 years ago #
  7. if you want to resize image than i will you suggest you to visit the below link
    http://www.raiseitsolutions.com/forum/viewtopic.php?f=4&t=3
    please visit the link, here you can know about how to resize image.
    thank you very much.

    Posted 8 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.