Advertisement
gufoe

Image resizing function

Apr 29th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.31 KB | None | 0 0
  1.     /**
  2.     * Images scaling
  3.     * @param string  $ini_path Path to initial image.
  4.     * @param string $dest_path Path to save new image.
  5.     * @param array $params [optional] Must be an associative array of params
  6.     * $params['width'] int New image width.
  7.     * $params['height'] int New image height.
  8.     * $params['constraint'] array.$params['constraint']['width'], $params['constraint'][height]
  9.     * If specified the $width and $height params will be ignored.
  10.     * New image will be resized to specified value either by width or height.
  11.     * $params['aspect_ratio'] bool If false new image will be stretched to specified values.
  12.     * If true aspect ratio will be preserved an empty space filled with color $params['rgb']
  13.     * It has no sense for $params['constraint'].
  14.     * $params['crop'] bool If true new image will be cropped to fit specified dimensions. It has no sense for $params['constraint'].
  15.     * $params['rgb'] Hex code of background color. Default 0xFFFFFF.
  16.     * $params['quality'] int New image quality (0 - 100). Default 100.
  17.     * @return bool True on success.
  18.     */
  19.     function resize($ini_path, $dest_path, $params = array()) {
  20.         $width = !empty($params['width']) ? $params['width'] : null;
  21.         $height = !empty($params['height']) ? $params['height'] : null;
  22.         $constraint = !empty($params['constraint']) ? $params['constraint'] : false;
  23.         $rgb = !empty($params['rgb']) ?  $params['rgb'] : 0xFFFFFF;
  24.         $quality = !empty($params['quality']) ?  $params['quality'] : 100;
  25.         $aspect_ratio = isset($params['aspect_ratio']) ?  $params['aspect_ratio'] : true;
  26.         $crop = isset($params['crop']) ?  $params['crop'] : true;
  27.  
  28.         if (!file_exists($ini_path)) return false;
  29.    
  30.    
  31.         if (!is_dir($dir=dirname($dest_path))) mkdir($dir);
  32.    
  33.         $img_info = getimagesize($ini_path);
  34.         if ($img_info === false) return false;
  35.    
  36.         $ini_p = $img_info[0]/$img_info[1];
  37.         if ( $constraint ) {
  38.             $con_p = $constraint['width']/$constraint['height'];
  39.             $calc_p = $constraint['width']/$img_info[0];
  40.    
  41.             if ( $ini_p < $con_p ) {
  42.                 $height = $constraint['height'];
  43.                 $width = $height*$ini_p;
  44.             } else {
  45.                 $width = $constraint['width'];
  46.                 $height = $img_info[1]*$calc_p;
  47.             }
  48.         } else {
  49.             if ( !$width && $height ) {
  50.                 $width = ($height*$img_info[0])/$img_info[1];
  51.             } else if ( !$height && $width ) {
  52.                 $height = ($width*$img_info[1])/$img_info[0];
  53.             } else if ( !$height && !$width ) {
  54.                 $width = $img_info[0];
  55.                 $height = $img_info[1];
  56.             }
  57.         }
  58.    
  59.         preg_match('/\.([^\.]+)$/i',basename($dest_path), $match);
  60.         $ext = strtolower($match[1]);
  61.         $output_format = ($ext == 'jpg') ? 'jpeg' : $ext;
  62.    
  63.         $format = strtolower(substr($img_info['mime'], strpos($img_info['mime'], '/')+1));
  64.         $icfunc = "imagecreatefrom" . $format;
  65.    
  66.         $iresfunc = "image" . $output_format;
  67.    
  68.         if (!function_exists($icfunc)) return false;
  69.    
  70.         $dst_x = $dst_y = 0;
  71.         $src_x = $src_y = 0;
  72.         $res_p = $width/$height;
  73.         if ( $crop && !$constraint ) {
  74.             $dst_w  = $width;
  75.             $dst_h = $height;
  76.             if ( $ini_p > $res_p ) {
  77.                 $src_h = $img_info[1];
  78.                 $src_w = $img_info[1]*$res_p;
  79.                 $src_x = ($img_info[0] >= $src_w) ? floor(($img_info[0] - $src_w) / 2) : $src_w;
  80.             } else {
  81.                 $src_w = $img_info[0];
  82.                 $src_h = $img_info[0]/$res_p;
  83.                 $src_y    = ($img_info[1] >= $src_h) ? floor(($img_info[1] - $src_h) / 2) : $src_h;
  84.             }
  85.         } else {
  86.             if ( $ini_p > $res_p ) {
  87.                 $dst_w = $width;
  88.                 $dst_h = $aspect_ratio ? floor($dst_w/$img_info[0]*$img_info[1]) : $height;
  89.                 $dst_y = $aspect_ratio ? floor(($height-$dst_h)/2) : 0;
  90.             } else {
  91.                 $dst_h = $height;
  92.                 $dst_w = $aspect_ratio ? floor($dst_h/$img_info[1]*$img_info[0]) : $width;
  93.                 $dst_x = $aspect_ratio ? floor(($width-$dst_w)/2) : 0;
  94.             }
  95.             $src_w = $img_info[0];
  96.             $src_h = $img_info[1];
  97.         }
  98.    
  99.         $isrc = $icfunc($ini_path);
  100.         $idest = imagecreatetruecolor($width, $height);
  101.         if ( ($format == 'png' || $format == 'gif') && $output_format == $format ) {
  102.             imagealphablending($idest, false);
  103.             imagesavealpha($idest,true);
  104.             imagefill($idest, 0, 0, IMG_COLOR_TRANSPARENT);
  105.             imagealphablending($isrc, true);
  106.             $quality = 0;
  107.         } else {
  108.             imagefill($idest, 0, 0, $rgb);
  109.         }
  110.         imagecopyresampled($idest, $isrc, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
  111.         $res = $iresfunc($idest, $dest_path, $quality);
  112.    
  113.         imagedestroy($isrc);
  114.         imagedestroy($idest);
  115.    
  116.         return $res;
  117.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement