RtThemesSupport

rttheme 17 vtresize

May 6th, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.39 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  *
  5.  *  This is the edited version of vtresize for RT-Themes
  6.  *
  7.  */
  8.  
  9.  
  10. /*
  11.  * Resize images dynamically using wp built in functions
  12.  * Victor Teixeira
  13.  *
  14.  * php 5.2+
  15.  *
  16.  * Exemplo de uso:
  17.  *
  18.  * <?php
  19.  * $thumb = get_post_thumbnail_id();
  20.  * $image = vt_resize( $thumb, '', 140, 110, true );
  21.  * ?>
  22.  * <img src="<?php echo $image[url]; ?>" width="<?php echo $image[width]; ?>" height="<?php echo $image[height]; ?>" />
  23.  *
  24.  * @param int $attach_id
  25.  * @param string $img_url
  26.  * @param int $width
  27.  * @param int $height
  28.  * @param bool $crop
  29.  * @return array
  30.  */
  31.  
  32.    
  33. function vt_resize( $attach_id = null, $img_url = null, $width, $height, $crop = false ) {
  34.  
  35.     $file_path = "";
  36.     $image_src = array();
  37.  
  38.     // this is an attachment, so we have the ID
  39.     if ( $attach_id ) {
  40.    
  41.         $image_src = wp_get_attachment_image_src( $attach_id, 'full' );
  42.         $file_path = get_attached_file( $attach_id );
  43.    
  44.    
  45.     // this is not an attachment, let's use the image url
  46.     } else if ( $img_url ) {
  47.        
  48.         $file_path = parse_url( $img_url );
  49.         $file_path = $_SERVER['DOCUMENT_ROOT'] . $file_path['path'];
  50.  
  51.         //Next two lines added by NEO
  52.         $file_path = substr($file_path,(stripos($file_path, '/wp-content/'))); 
  53.         $file_path = getcwd() .$file_path;         
  54.        
  55.         /* alternative find paths
  56.             $file_path = parse_url( $img_url );
  57.             $uploads = wp_upload_dir();  
  58.             $file_path = $uploads['basedir'] . str_replace("/wp-content/uploads","", $file_path['path']);
  59.         */
  60.        
  61.         $orig_size = file_exists($file_path) ? getimagesize( $file_path ) : false;
  62.  
  63.        
  64.         $image_src[0] = $img_url;
  65.         $image_src[1] = $orig_size[0];
  66.         $image_src[2] = $orig_size[1];
  67.        
  68.        
  69.         //let WP find image urls
  70.         if(!$orig_size) {
  71.            
  72.             $get_image_id_from_url = rt_get_attachment_id_from_src($img_url);
  73.             if($get_image_id_from_url){  
  74.                 $image_src = wp_get_attachment_image_src( $get_image_id_from_url, 'full' );
  75.                 $file_path = get_attached_file( $get_image_id_from_url );  
  76.             }  
  77.         }
  78.     }
  79.      
  80.  
  81.  
  82.     $file_info = pathinfo( $file_path );
  83.     $extension = isset($file_info['extension']) ? '.'. $file_info['extension'] : "";
  84.  
  85.     // the image path without the extension
  86.     $no_ext_path = isset( $file_info['dirname'] ) && isset( $file_info['filename'] ) ? $file_info['dirname'].'/'.$file_info['filename'] : "";
  87.  
  88.     $cropped_img_path = $no_ext_path.'-'.$width.'x'.$height.$extension;
  89.  
  90.     // checking if the file size is larger than the target size
  91.     // if it is smaller or the same size, stop right here and return
  92.     if ( isset($image_src[1]) && $image_src[1] > $width || isset($image_src[2]) && $image_src[2] > $height ) {
  93.  
  94.         // 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)
  95.         if ( file_exists( $cropped_img_path)  && $crop != false) {
  96.  
  97.             $cropped_img_url = str_replace( basename( $image_src[0] ), basename( $cropped_img_path ), $image_src[0] );
  98.            
  99.             $vt_image = array (
  100.                 'url' => $cropped_img_url,
  101.                 'width' => $width,
  102.                 'height' => $height
  103.             );
  104.            
  105.             return $vt_image;
  106.         }
  107.  
  108.         // $crop = false
  109.         if ( $crop == false ) {
  110.             // calculate the size proportionaly
  111.             $proportional_size = wp_constrain_dimensions( $image_src[1], $image_src[2], $width, $height );
  112.             $resized_img_path = $no_ext_path.'-'.$proportional_size[0].'x'.$proportional_size[1].$extension;           
  113.  
  114.             // checking if the file already exists
  115.             if ( file_exists( $resized_img_path ) ) {
  116.            
  117.                 $resized_img_url = str_replace( basename( $image_src[0] ), basename( $resized_img_path ), $image_src[0] );
  118.  
  119.                 $vt_image = array (
  120.                     'url' => $resized_img_url,
  121.                     'width' => $proportional_size[0],
  122.                     'height' => $proportional_size[1]
  123.                 );
  124.                
  125.                 return $vt_image;
  126.             }
  127.         }
  128.  
  129.         // no cache files - let's finally resize it
  130.         $new_img_path = image_resize( $file_path, $width, $height, $crop );
  131.         $new_img_size = getimagesize( $new_img_path );
  132.         $new_img = str_replace( basename( $image_src[0] ), basename( $new_img_path ), $image_src[0] );
  133.  
  134.         // resized output
  135.         $vt_image = array (
  136.             'url' => $new_img,
  137.             'width' => $new_img_size[0],
  138.             'height' => $new_img_size[1]
  139.         );
  140.        
  141.         return $vt_image;
  142.     }
  143.  
  144.     // default output - without resizing
  145.     $vt_image = array (
  146.         'url' => isset($image_src[0]) ? $image_src[0] : "" ,
  147.         'width' => isset($image_src[0]) ? $image_src[1] : "" ,
  148.         'height' => isset($image_src[0]) ? $image_src[2] : ""
  149.     );
  150.    
  151.     return $vt_image;
  152. }
  153. ?>
Advertisement
Add Comment
Please, Sign In to add comment