jegtheme

jeg-image.php

Jun 24th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.03 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. /**
  36.  * get post thumbnail image, and return array of attribute  
  37.  **/
  38. function j_get_post_header_image($post, $size){
  39.     $attachment_id  = get_post_thumbnail_id($post->ID);
  40.     $image = wp_get_attachment_image_src($attachment_id, $size);   
  41.     if ( $image ) {
  42.         list($src, $width, $height) = $image;
  43.         $attachment =& get_post($attachment_id);
  44.         $default_attr = array(
  45.             'src'   => $src,
  46.             'class' => "attachment-$size",
  47.             'alt'   => trim(strip_tags( get_the_title() ))
  48.         );
  49.         if ( empty($default_attr['alt']) )
  50.             $default_attr['alt'] = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption
  51.         if ( empty($default_attr['alt']) )
  52.             $default_attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title
  53.            
  54.         return $default_attr;
  55.     } else {
  56.         return NULL;
  57.     }  
  58. }
  59.  
  60.  
  61. /**
  62.  * Build image tag from array
  63.  *
  64.  * @param array $arrs
  65.  */
  66. function j_build_image ($arrs)
  67. {
  68.     $html = "<img ";
  69.    
  70.     foreach( $arrs as $name => $value ) {
  71.         $html .= " $name=" . '"' . $value . '"';
  72.     }
  73.     $html .= " />";
  74.    
  75.     return $html;
  76. }
  77.  
  78.  
  79. /**
  80.  * get image after resizing
  81.  *
  82.  * @param $img  image url
  83.  * @param $dim  defined image dimension
  84.  * @param $w    image width
  85.  * @param $h    image height
  86.  * @param $a    align
  87.  * @param $zc   zoom crop
  88.  */
  89. function jeg_get_image($img = '', $dim = '',  $w = false, $h = false , $nocrop = false , $fillcolor = "000000")
  90. {
  91.     if($dim != '')
  92.     {
  93.         $imageDim = jeg_get_theme_image_sizes();
  94.         if(isset($imageDim[$dim]))
  95.         {
  96.             $w = $imageDim[$dim][0];
  97.             $h = $imageDim[$dim][1];
  98.         }
  99.     }
  100.    
  101.     return jeg_image_resizer($img, $w, $h, $nocrop, $fillcolor);
  102. }
  103.  
  104. /**
  105.  * Get image meta description for image ( basedir , baseurl , imgpath , extension , height , width , destpath , imagetype)
  106.  *
  107.  * @param string $source URL source of image
  108.  * @return array of image meta description
  109.  */
  110. function jeg_get_image_meta ($source)
  111. {
  112.     // meta image container
  113.     $meta = array();
  114.    
  115.     // define upload path & dir
  116.     $upload_info        = wp_upload_dir();
  117.     $meta['basedir']    = $upload_info['basedir'];
  118.     $meta['baseurl']    = $upload_info['baseurl'];
  119.    
  120.     // check if image is local
  121.     if(strpos( $source, $meta['baseurl'] ) === false)
  122.     {
  123.         return false;
  124.     } else
  125.     {
  126.         $destpath           = str_replace( $meta['baseurl'], '', $source);
  127.         $meta['imgpath']    = $meta['basedir'] . $destpath;
  128.        
  129.         // check if img path exists, and is an image indeed
  130.         if( !file_exists($meta['imgpath']) OR !getimagesize($meta['imgpath']) )
  131.         {
  132.             return false;
  133.         } else
  134.         {
  135.             // get image info
  136.             $info = pathinfo($meta['imgpath']);        
  137.             $meta['extension'] = $info['extension'];
  138.            
  139.             // get image size
  140.             $imagesize = getimagesize($meta['imgpath']);           
  141.             $meta['width']      = $imagesize[0];
  142.             $meta['height']     = $imagesize[1];
  143.             $meta['imagetype']  = image_type_to_mime_type($imagesize[2]);
  144.            
  145.             // now define dest path
  146.             $meta['destpath']   = str_replace( '.' . $meta['extension'], '' , $destpath);
  147.            
  148.             // return this meta
  149.             return $meta;
  150.         }
  151.     }
  152. }
  153.  
  154. /**
  155.  * Jeg Custom Resizer function
  156.  *
  157.  * @param string    $source
  158.  * @param int       $w
  159.  * @param int       $h
  160.  * @param boolean   $crop
  161.  * @param string of color hex
  162.  */
  163. function jeg_image_resizer ($source, $w, $h, $nocrop, $fillcolor)
  164. {
  165.     $meta = jeg_get_image_meta($source);
  166.     $imgpath = '';
  167.    
  168.     if($meta)
  169.     {
  170.         if ($nocrop) {
  171.             // show all image without croping zone
  172.             $imgpath = jeg_resize_nocrop($meta, $w, $h, $fillcolor);
  173.         } else {
  174.             // show image with croping zone to fill container
  175.             $imgpath = jeg_resize_crop($meta, $w, $h);
  176.         }
  177.         return $imgpath;
  178.     } else {
  179.         return false;
  180.     }
  181. }
  182.  
  183.  
  184. /**
  185.  * Show all image without croping zone
  186.  *
  187.  * @param array of $meta image
  188.  * @param int $w
  189.  * @param int $h
  190.  * @param string hext of color
  191.  * @param int $quality
  192.  */
  193. function jeg_resize_nocrop($meta, $w, $h, $fillcolor, $quality = 100)
  194. {
  195.     /*
  196.      * Do some math to figure out which way we'll need to crop the image
  197.      * to get it proportional to the new size, then crop or adjust as needed
  198.      */  
  199.     $x_ratio = $w / $meta['width'];
  200.     $y_ratio = $h / $meta['height'];
  201.  
  202.     if (($meta['width'] <= $w) && ($meta['height'] <= $h)) {
  203.         $new_w = $meta['width'];
  204.         $new_h = $meta['height'];
  205.     } elseif (($x_ratio * $meta['height']) < $h) {
  206.         $new_h = ceil($x_ratio * $meta['height']);
  207.         $new_w = $w;
  208.     } else {
  209.         $new_w = ceil($y_ratio * $meta['width']);
  210.         $new_h = $h;
  211.     }
  212.    
  213.     $destination = $meta['basedir'] . $meta['destpath'] . '-' . $new_w . 'x' . $new_h . '-nocrop' . '.' . $meta['extension'] ;
  214.     $desturl    = $meta['baseurl'] . $meta['destpath'] . '-' . $new_w . 'x' . $new_h . '-nocrop' . '.' . $meta['extension'] ;
  215.    
  216.     // check if file exist
  217.     if( file_exists($destination) ) {
  218.         return $desturl;
  219.     }
  220.    
  221.     switch ($meta['imagetype']) {
  222.         case 'image/jpeg':
  223.             $source = imagecreatefromjpeg($meta['imgpath']);
  224.             break;
  225.         case 'image/gif':
  226.             $source = imagecreatefromgif($meta['imgpath']);
  227.             break;
  228.         case 'image/png':
  229.             $source = imagecreatefrompng($meta['imgpath']);
  230.             break;
  231.         default:
  232.             return;
  233.     }
  234.        
  235.     // do resizing and add background to fill
  236.     $newpic = imagecreatetruecolor(round($new_w), round($new_h));
  237.     imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $meta['width'], $meta['height']);
  238.     $final = imagecreatetruecolor($w, $h);
  239.        
  240.     $fillbg = jeg_hex2RGB($fillcolor);
  241.     $backgroundColor = imagecolorallocate($final, $fillbg['red'], $fillbg['green'], $fillbg['blue']);
  242.     imagefill($final, 0, 0, $backgroundColor);
  243.     imagecopy($final, $newpic, (($w - $new_w)/ 2), (($h - $new_h) / 2), 0, 0, $new_w, $new_h);
  244.          
  245.     if (imagejpeg($final, $destination, $quality)) {
  246.         return $desturl;
  247.     }
  248.        
  249.     return false;    
  250. }
  251.  
  252. /**
  253.  * Build image that will fill the container
  254.  *
  255.  * @todo : check kalau image lebih kecil, maka pake jeg_resize_nocrop
  256.  *
  257.  * @param array $meta of meta image
  258.  * @param int $w
  259.  * @param int $h
  260.  * @return string of url path
  261.  */
  262. function jeg_resize_crop ($meta, $w, $h)
  263. {
  264.     //get image size after cropping
  265.     $dims = image_resize_dimensions($meta['width'], $meta['height'], $w, $h, true);
  266.     $new_w = $dims[4];
  267.     $new_h = $dims[5];
  268.    
  269.     //use this to check if cropped image already exists, so we can return that instead
  270.     $destination = $meta['basedir'] . $meta['destpath'] . '-' . $new_w . 'x' . $new_h . '.' . $meta['extension'] ;
  271.     $desturl    = $meta['baseurl'] . $meta['destpath'] . '-' . $new_w . 'x' . $new_h . '.' . $meta['extension'] ;
  272.    
  273.     // check if file is exist
  274.     if(file_exists($destination) && getimagesize($destination)) {
  275.         return $desturl;
  276.     } else {
  277.         $resized_img_path = jeg_image_resize_hub( $meta['imgpath'], $new_w, $new_h, true );
  278.         $resized_rel_path = str_replace( $meta['basedir'], '', $resized_img_path);
  279.         return ( $meta['baseurl'] . $resized_rel_path );
  280.     }
  281.    
  282.     return $desturl;
  283. }
  284.  
  285.  
  286. /**
  287.  * this function allow user to use themes bellow version 3.4
  288.  */
  289. function jeg_image_resize_hub($img_path, $width, $height, $crop = false) {
  290.     $resized_img_path  = '';
  291.    
  292.     if(function_exists('wp_get_image_editor')) {
  293.         $image = wp_get_image_editor($img_path);
  294.         $image->resize($width, $height, $crop);
  295.        
  296.         $resized_img_path = $image->generate_filename();
  297.         $image->save($resized_img_path);   
  298.     } else {       
  299.         $resized_img_path = image_resize( $img_path, $width, $height, $crop );
  300.     }
  301.    
  302.     return $resized_img_path;
  303. }
Advertisement
Add Comment
Please, Sign In to add comment