How to automatically use resized images instead of originals | Wordpress


/ Published in: PHP
Save to your folder(s)

Place snippet in functions.php, edit maximum image size in Admin -> Settings -> Media -> Image Sizes ("Large Size") (Wordpress 3.1)


Copy this code and paste it in your HTML
  1. function replace_uploaded_image($image_data) {
  2. // if there is no large image : return
  3. if (!isset($image_data['sizes']['large'])) return $image_data;
  4.  
  5. // paths to the uploaded image and the large image
  6. $upload_dir = wp_upload_dir();
  7. $uploaded_image_location = $upload_dir['basedir'] . '/' .$image_data['file'];
  8. $large_image_location = $upload_dir['path'] . '/'.$image_data['sizes']['large']['file'];
  9.  
  10. // delete the uploaded image
  11. unlink($uploaded_image_location);
  12.  
  13. // rename the large image
  14. rename($large_image_location,$uploaded_image_location);
  15.  
  16. // update image metadata and return them
  17. $image_data['width'] = $image_data['sizes']['large']['width'];
  18. $image_data['height'] = $image_data['sizes']['large']['height'];
  19. unset($image_data['sizes']['large']);
  20.  
  21. return $image_data;
  22. }
  23. add_filter('wp_generate_attachment_metadata','replace_uploaded_image');

URL: http://www.wprecipes.com/how-to-automatically-use-resized-image-instead-of-originals

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.