Advertisement
lordalan

Save crop for custom image sizes

Apr 3rd, 2011
1,935
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.34 KB | None | 0 0
  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. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement