Guest User

vt_resize

a guest
Feb 8th, 2013
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.97 KB | None | 0 0
  1. function vt_resize( $attach_id = null, $img_url = null, $width, $height, $crop = false ) {  // this is an attachment, so we have the ID
  2.  
  3. if ( $attach_id ) {        
  4.  
  5. $image_src = wp_get_attachment_image_src( $attach_id, 'full' );    
  6.  
  7. $file_path = get_attached_file( $attach_id );      
  8.  
  9. // this is not an attachment, let's use the image url  
  10.  
  11. } else if ( $img_url ) {               
  12.  
  13. $file_path = parse_url( $img_url );    
  14.  
  15. $file_path = ltrim( $file_path['path'], '/' );     
  16.  
  17. //$file_path = rtrim( ABSPATH, '/' ).$file_path['path'];               
  18.  
  19. $orig_size = getimagesize( $file_path );               
  20.  
  21. $image_src[0] = $img_url;      
  22.  
  23. $image_src[1] = $orig_size[0];     
  24.  
  25. $image_src[2] = $orig_size[1]; 
  26.  
  27. }      
  28.  
  29. $file_info = pathinfo( $file_path );   
  30.  
  31. $extension = '.'. $file_info['extension']; 
  32.  
  33. // the image path without the extension
  34.  
  35. $no_ext_path = $file_info['dirname'].'/'.$file_info['filename'];   
  36.  
  37. $cropped_img_path = $no_ext_path.'-'.$width.'x'.$height.$extension;
  38.  
  39. // checking if the file size is larger than the target size
  40.  
  41. // if it is smaller or the same size, stop right here and return   
  42.  
  43. if ( $image_src[1] > $width || $image_src[2] > $height ) {     
  44.  
  45. // the file is larger, check if the resized version already exists (for crop = true but will also work for crop = false if the sizes match)    
  46.  
  47. if ( file_exists( $cropped_img_path ) ) {          
  48.  
  49.  
  50. $cropped_img_url = str_replace( basename( $image_src[0] ), basename( $cropped_img_path ), $image_src[0] );                     
  51.  
  52.  
  53. $vt_image = array (            
  54.  
  55. 'url' => $cropped_img_url,             
  56.  
  57. 'width' => $width,             
  58.  
  59. 'height' => $height        
  60.  
  61. );                     
  62.  
  63. return $vt_image;      
  64.  
  65. }      
  66.  
  67. // crop = false    
  68.  
  69. if ( $crop == false ) {                
  70.  
  71. // calculate the size proportionaly        
  72.  
  73. $proportional_size = wp_constrain_dimensions( $image_src[1], $image_src[2], $width, $height );         
  74.  
  75. $resized_img_path = $no_ext_path.'-'.$proportional_size[0].'x'.$proportional_size[1].$extension;                       
  76.  
  77. //$resized_img_path = $no_ext_path.'-'.$width.'x'.$height.$extension;
  78.  
  79. // checking if the file already exists         
  80.  
  81. if ( file_exists( $resized_img_path ) ) {                          
  82.  
  83. $resized_img_url = str_replace( basename( $image_src[0] ), basename( $resized_img_path ), $image_src[0] );             
  84.  
  85. $vt_image = array (                
  86.  
  87. 'url' => $resized_img_url,                 
  88.  
  89. 'width' => $new_img_size[0],                   
  90.  
  91. 'height' => $new_img_size[1]               
  92.  
  93. );                             
  94.  
  95. return $vt_image;          
  96.  
  97. }      
  98.  
  99. }      
  100.  
  101. // no cached files - let's finally resize it       
  102.  
  103. $new_img_path = image_resize( $file_path, $width, $height, $crop );    
  104.  
  105. $new_img_size = getimagesize( $new_img_path );     
  106.  
  107. $new_img = str_replace( basename( $image_src[0] ), basename( $new_img_path ), $image_src[0] );     
  108.  
  109. // resized output      
  110.  
  111. $vt_image = array (        
  112.  
  113. 'url' => $new_img,         
  114.  
  115. 'width' => $new_img_size[0],           
  116.  
  117. 'height' => $new_img_size[1]       
  118.  
  119. );             
  120.  
  121. return $vt_image;  
  122.  
  123. }  
  124.  
  125. // default output - without resizing   
  126.  
  127. $vt_image = array (  
  128.  
  129. 'url' => $image_src[0],    
  130.  
  131. 'width' => $image_src[1],      
  132.  
  133. 'height' => $image_src[2]  
  134. );     
  135.  
  136. return $vt_image;}
  137.  
  138. ?>
Advertisement
Add Comment
Please, Sign In to add comment