jegtheme

init-image.php

Feb 4th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.13 KB | None | 0 0
  1. <?php
  2. /**
  3.  * All image functionality
  4.  * available hook :
  5.  *      - jeg_register_image_size (register / modify another image size)
  6.  *
  7.  * @author jegbagus
  8.  */
  9.  
  10. /**
  11.  * initalize image size
  12.  */
  13. add_action('init', 'jeg_theme_image_sizes');
  14.  
  15. function jeg_theme_image_sizes()
  16. {
  17.     $imageDim = array (
  18.         "post-feature-image"        => array(602, 300)
  19.     );
  20.    
  21.     $imageDim = apply_filters('jeg_register_image_size', $imageDim);
  22.    
  23.     if (function_exists('add_theme_support'))
  24.     {
  25.         add_theme_support ( 'post-thumbnails' );
  26.         foreach($imageDim as $id => $val) {
  27.             add_image_size($id, $val[0], $val[1], true);
  28.         }
  29.     }
  30.    
  31.     return $imageDim;
  32. }
  33.  
  34. /**
  35.  * Build image tag from array
  36.  *
  37.  * @param array $arrs
  38.  */
  39. function j_build_image ($arrs)
  40. {
  41.     $html = "<img ";
  42.    
  43.     foreach( $arrs as $name => $value ) {
  44.         $html .= " $name=" . '"' . $value . '"';
  45.     }
  46.     $html .= " />";
  47.    
  48.     return $html;
  49. }
  50.  
  51.  
  52. /**
  53.  * get image after resizing
  54.  *
  55.  * @param $img  image url
  56.  * @param $dim  defined image dimension
  57.  * @param $w    image width
  58.  * @param $h    image height
  59.  * @param $a    align
  60.  * @param $zc   zoom crop
  61.  */
  62. function jeg_get_image($img = '', $dim = '',  $w = false, $h = false , $nocrop = false , $fillcolor = "000000")
  63. {
  64.     if($dim != '')
  65.     {
  66.         $imageDim = jeg_get_theme_image_sizes();
  67.         if(isset($imageDim[$dim]))
  68.         {
  69.             $w = $imageDim[$dim][0];
  70.             $h = $imageDim[$dim][1];
  71.         }
  72.     }
  73.    
  74.     return jeg_image_resizer($img, $w, $h, $nocrop, $fillcolor);
  75. }
  76.  
  77. /**
  78.  * Get image meta description for image ( basedir , baseurl , imgpath , extension , height , width , destpath , imagetype)
  79.  *
  80.  * @param string $source URL source of image
  81.  * @return array of image meta description
  82.  */
  83. function jeg_get_image_meta ($source)
  84. {
  85.     // meta image container
  86.     $meta = array();
  87.    
  88.     // define upload path & dir
  89.     $upload_info        = wp_upload_dir();
  90.     $meta['basedir']    = $upload_info['basedir'];
  91.     $meta['baseurl']    = $upload_info['baseurl'];
  92.    
  93.     // check if image is local
  94.     if(strpos( $source, $meta['baseurl'] ) === false)
  95.     {
  96.         return false;
  97.     } else
  98.     {
  99.         $destpath           = str_replace( $meta['baseurl'], '', $source);
  100.         $meta['imgpath']    = $meta['basedir'] . $destpath;
  101.        
  102.         // check if img path exists, and is an image indeed
  103.         if( !file_exists($meta['imgpath']) OR !getimagesize($meta['imgpath']) )
  104.         {
  105.             return false;
  106.         } else
  107.         {
  108.             // get image info
  109.             $info = pathinfo($meta['imgpath']);        
  110.             $meta['extension'] = $info['extension'];
  111.            
  112.             // get image size
  113.             $imagesize = getimagesize($meta['imgpath']);           
  114.             $meta['width']      = $imagesize[0];
  115.             $meta['height']     = $imagesize[1];
  116.             $meta['imagetype']  = image_type_to_mime_type($imagesize[2]);
  117.            
  118.             // now define dest path
  119.             $meta['destpath']   = str_replace( '.' . $meta['extension'], '' , $destpath);
  120.            
  121.             // return this meta
  122.             return $meta;
  123.         }
  124.     }
  125. }
  126.  
  127. /**
  128.  * Jeg Custom Resizer function
  129.  *
  130.  * @param string    $source
  131.  * @param int       $w
  132.  * @param int       $h
  133.  * @param boolean   $crop
  134.  * @param string of color hex
  135.  */
  136. function jeg_image_resizer ($source, $w, $h, $nocrop, $fillcolor)
  137. {
  138.     $meta = jeg_get_image_meta($source);
  139.     $imgpath = '';
  140.    
  141.     if($meta)
  142.     {
  143.         if ($nocrop) {
  144.             // show all image without croping zone
  145.             $imgpath = jeg_resize_nocrop($meta, $w, $h, $fillcolor);
  146.         } else {
  147.             // show image with croping zone to fill container
  148.             $imgpath = jeg_resize_crop($meta, $w, $h);
  149.         }
  150.         return $imgpath;
  151.     } else {
  152.         return false;
  153.     }
  154. }
  155.  
  156.  
  157. /**
  158.  * Show all image without croping zone
  159.  *
  160.  * @param array of $meta image
  161.  * @param int $w
  162.  * @param int $h
  163.  * @param string hext of color
  164.  * @param int $quality
  165.  */
  166. function jeg_resize_nocrop($meta, $w, $h, $fillcolor, $quality = 100)
  167. {
  168.     /*
  169.      * Do some math to figure out which way we'll need to crop the image
  170.      * to get it proportional to the new size, then crop or adjust as needed
  171.      */  
  172.     $x_ratio = $w / $meta['width'];
  173.     $y_ratio = $h / $meta['height'];
  174.  
  175.     if (($meta['width'] <= $w) && ($meta['height'] <= $h)) {
  176.         $new_w = $meta['width'];
  177.         $new_h = $meta['height'];
  178.     } elseif (($x_ratio * $meta['height']) < $h) {
  179.         $new_h = ceil($x_ratio * $meta['height']);
  180.         $new_w = $w;
  181.     } else {
  182.         $new_w = ceil($y_ratio * $meta['width']);
  183.         $new_h = $h;
  184.     }
  185.    
  186.     $destination = $meta['basedir'] . $meta['destpath'] . '-' . $new_w . 'x' . $new_h . '-nocrop' . '.' . $meta['extension'] ;
  187.     $desturl    = $meta['baseurl'] . $meta['destpath'] . '-' . $new_w . 'x' . $new_h . '-nocrop' . '.' . $meta['extension'] ;
  188.    
  189.     // check if file exist
  190.     if( file_exists($destination) ) {
  191.         return $desturl;
  192.     }
  193.    
  194.     switch ($meta['imagetype']) {
  195.         case 'image/jpeg':
  196.             $source = imagecreatefromjpeg($meta['imgpath']);
  197.             break;
  198.         case 'image/gif':
  199.             $source = imagecreatefromgif($meta['imgpath']);
  200.             break;
  201.         case 'image/png':
  202.             $source = imagecreatefrompng($meta['imgpath']);
  203.             break;
  204.         default:
  205.             return;
  206.     }
  207.        
  208.     // do resizing and add background to fill
  209.     $newpic = imagecreatetruecolor(round($new_w), round($new_h));
  210.     imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $meta['width'], $meta['height']);
  211.     $final = imagecreatetruecolor($w, $h);
  212.        
  213.     $fillbg = jeg_hex2RGB($fillcolor);
  214.     $backgroundColor = imagecolorallocate($final, $fillbg['red'], $fillbg['green'], $fillbg['blue']);
  215.     imagefill($final, 0, 0, $backgroundColor);
  216.     imagecopy($final, $newpic, (($w - $new_w)/ 2), (($h - $new_h) / 2), 0, 0, $new_w, $new_h);
  217.          
  218.     if (imagejpeg($final, $destination, $quality)) {
  219.         return $desturl;
  220.     }
  221.        
  222.     return false;    
  223. }
  224.  
  225. /**
  226.  * Build image that will fill the container
  227.  *
  228.  * @param array $meta of meta image
  229.  * @param int $w
  230.  * @param int $h
  231.  * @return string of url path
  232.  */
  233. function jeg_resize_crop ($meta, $w, $h)
  234. {
  235.     //get image size after cropping
  236.     $dims = image_resize_dimensions($meta['width'], $meta['height'], $w, $h);
  237.     $new_w = $dims[4];
  238.     $new_h = $dims[5];
  239.    
  240.     //use this to check if cropped image already exists, so we can return that instead
  241.     $destination = $meta['basedir'] . $meta['destpath'] . '-' . $new_w . 'x' . $new_h . '.' . $meta['extension'] ;
  242.     $desturl    = $meta['baseurl'] . $meta['destpath'] . '-' . $new_w . 'x' . $new_h . '.' . $meta['extension'] ;
  243.    
  244.     // check if file is exist
  245.     if(file_exists($destination) && getimagesize($destination)) {
  246.         return $desturl;
  247.     } else {
  248.         $resized_img_path = jeg_image_resize_hub( $meta['imgpath'], $w, $h );
  249.         $resized_rel_path = str_replace( $meta['basedir'], '', $resized_img_path);
  250.         return ( $meta['baseurl'] . $resized_rel_path );
  251.     }
  252.        
  253.     return $desturl;
  254. }
  255.  
  256.  
  257. /**
  258.  * this function allow user to use themes bellow version 3.4
  259.  */
  260. function jeg_image_resize_hub($img_path, $width, $height, $crop = false) {
  261.     $resized_img_path  = '';
  262.    
  263.     if(function_exists('wp_get_image_editor')) {
  264.         $image = wp_get_image_editor($img_path);
  265.         $image->resize($width, $height, $crop);
  266.        
  267.         $resized_img_path = $image->generate_filename();
  268.         $image->save($resized_img_path);   
  269.     } else {       
  270.         $resized_img_path = image_resize( $img_path, $width, $height, $crop );
  271.     }
  272.    
  273.     return $resized_img_path;
  274. }
Advertisement
Add Comment
Please, Sign In to add comment