Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Save crop for custom image sizes

By: lordalan on Apr 3rd, 2011  |  syntax: PHP  |  size: 1.34 KB  |  views: 1,163  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. // Array of custom image sizes to add
  4. $my_image_sizes = array(
  5.         array( 'name'=>'slideshow-full', 'width'=>544, 'height'=>380, 'crop'=>true ),
  6.         array( 'name'=>'slideshow-thumb', 'width'=>30, 'height'=>30, 'crop'=>true ),
  7.         array( 'name'=>'attraction-thumb', 'width'=>67, 'height'=>67, 'crop'=>false ),
  8. );
  9.  
  10. // For each new image size, run add_image_size() and update_option() to add the necessary info.
  11. // update_option() is good because it only updates the database if the value has changed. It also adds the option if it doesn't exist
  12. foreach ( $my_image_sizes as $my_image_size ){
  13.         add_image_size( $my_image_size['name'], $my_image_size['width'], $my_image_size['height'], $my_image_size['crop'] );
  14.         update_option( $my_image_size['name']."_size_w", $my_image_size['width'] );
  15.         update_option( $my_image_size['name']."_size_h", $my_image_size['height'] );
  16.         update_option( $my_image_size['name']."_crop", $my_image_size['crop'] );
  17. }
  18.  
  19. // Hook into the 'intermediate_image_sizes' filter used by image-edit.php.
  20. // This adds the custom sizes into the array of sizes it uses when editing/saving images.
  21. add_filter( 'intermediate_image_sizes', 'my_add_image_sizes' );
  22. function my_add_image_sizes( $sizes ){
  23.         global $my_image_sizes;
  24.         foreach ( $my_image_sizes as $my_image_size ){
  25.                 $sizes[] = $my_image_size['name'];
  26.         }
  27.         return $sizes;
  28. }
  29.  
  30. ?>