Guest User

Untitled

a guest
Apr 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. <?php
  2. add_theme_support( 'builder-3.0' );
  3.  
  4. // registuring post thumbnail sizes
  5. if ( function_exists( 'add_image_size' ) ) {
  6. add_image_size( 'index-thumb', 250, 180, true ); // (cropped)
  7. }
  8.  
  9. // Custom image resize function by iThemes.com. iTeration 19
  10. if ( !function_exists( 'ithemes_filter_image_downsize' ) ) {
  11. add_filter( 'image_downsize', 'ithemes_filter_image_downsize', 10, 3 ); // Latch in when a custom image size is called.
  12. add_filter( 'intermediate_image_sizes_advanced', 'ithemes_filter_image_downsize_blockextra', 10, 3 ); // Custom image size blocker to block generation of thumbs for sizes other sizes except when called.
  13. function ithemes_filter_image_downsize( $result, $id, $size ) {
  14. global $_ithemes_temp_downsize_size;
  15. if ( is_array( $size ) ) { // Dont bother with non-named sizes. Let them proceed normally. We need to set something to block the blocker though.
  16. $_ithemes_temp_downsize_size = 'array_size';
  17. return;
  18. }
  19.  
  20. // Store current meta information and size data.
  21. global $_ithemes_temp_downsize_meta;
  22. $_ithemes_temp_downsize_size = $size;
  23. $_ithemes_temp_downsize_meta = wp_get_attachment_metadata( $id );
  24.  
  25. if ( !is_array( $imagedata = wp_get_attachment_metadata( $id ) ) ) { return $result; }
  26. if ( !is_array( $size ) && !empty( $imagedata['sizes'][$size] ) ) {
  27. $data = $imagedata['sizes'][$size];
  28. // Some handling if the size defined for this size name has changed.
  29. global $_wp_additional_image_sizes;
  30. if ( empty( $_wp_additional_image_sizes[$size] ) ) { // Not a custom size so return data as is.
  31. $img_url = wp_get_attachment_url( $id );
  32. $img_url = path_join( dirname( $img_url ), $data['file'] );
  33. return array( $img_url, $data['width'], $data['height'], true );
  34. } else { // Custom size so only return if current image file dimensions match the defined ones.
  35. global $_wp_additional_image_sizes;
  36. if ( ( $_wp_additional_image_sizes[$size]['width'] == $data['width'] ) && ( $_wp_additional_image_sizes[$size]['height'] == $data['height'] ) ) { // Only return if size hasnt changed.
  37. $img_url = wp_get_attachment_url( $id );
  38. $img_url = path_join( dirname( $img_url ), $data['file'] );
  39. return array( $img_url, $data['width'], $data['height'], true );
  40. }
  41. }
  42. }
  43.  
  44. require_once( ABSPATH . '/wp-admin/includes/image.php' );
  45. $uploads = wp_upload_dir();
  46. if ( !is_array( $uploads ) || ( false !== $uploads['error'] ) ) { return $result; }
  47. $file_path = "{$uploads['basedir']}/{$imagedata['file']}";
  48.  
  49. // Image is resized within the function in the following line.
  50. $temp_meta_information = wp_generate_attachment_metadata( $id, $file_path ); // triggers filter_image_downsize_blockextra() function via filter within. generate images. returns new meta data for image (only includes the just-generated image size).
  51.  
  52. $meta_information = $_ithemes_temp_downsize_meta; // Get the old original meta information.
  53.  
  54. if ( array_key_exists( 'sizes', $temp_meta_information ) ) {
  55. $meta_information['sizes'][$_ithemes_temp_downsize_size] = $temp_meta_information['sizes'][$_ithemes_temp_downsize_size]; // Merge old meta back in.
  56. }
  57. wp_update_attachment_metadata( $id, $meta_information ); // Update image meta data.
  58.  
  59. unset( $_ithemes_temp_downsize_size ); // Cleanup.
  60. unset( $_ithemes_temp_downsize_meta );
  61.  
  62. return $result;
  63. }
  64. /* Prevents image resizer from resizing ALL images; just the currently requested size. */
  65. function ithemes_filter_image_downsize_blockextra( $sizes ) {
  66. global $_ithemes_temp_downsize_size;
  67. if ( empty( $_ithemes_temp_downsize_size ) || ( $_ithemes_temp_downsize_size == 'array_size' ) ) { // Dont bother with non-named sizes. Let them proceed normally.
  68. return $sizes;
  69. }
  70. $sizes = array( $_ithemes_temp_downsize_size => $sizes[$_ithemes_temp_downsize_size] ); // Strip out all extra meta data so only the requested size will be generated.
  71. return $sizes;
  72. }
  73. }
  74.  
  75.  
  76. wp_enqueue_script('jquery');
  77.  
  78. ?>
Add Comment
Please, Sign In to add comment