infarh

img_resize

Nov 9th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.10 KB | None | 0 0
  1. function img_resize($src, $dest, $width, $height, $rgb=0xFFFFFF, $quality=100) {
  2.     if (!file_exists($src)) return false;
  3.  
  4.     $size = getimagesize($src);
  5.  
  6.     if ($size === false) return false;
  7.  
  8.     $format = strtolower(substr($size['mime'], strpos($size['mime'],'/')+1));
  9.  
  10.     $icfunc = "imagecreatefrom" . $format;
  11.     if (!function_exists($icfunc)) return false;
  12.  
  13.     $x_ratio = $width / $size[0];
  14.     $y_ratio = $height / $size[1];
  15.  
  16.     $ratio       = min($x_ratio, $y_ratio);
  17.     $use_x_ratio = ($x_ratio == $ratio);
  18.  
  19.     $new_width   = $use_x_ratio  ? $width  : floor($size[0] * $ratio);
  20.     $new_height  = !$use_x_ratio ? $height : floor($size[1] * $ratio);
  21.  
  22.     $new_left    = $use_x_ratio  ? 0 : floor(($width - $new_width) / 2);
  23.     $new_top     = !$use_x_ratio ? 0 : floor(($height - $new_height) / 2);
  24.  
  25.     $isrc = $icfunc($src);
  26.     $idest = imagecreatetruecolor($width, $height);
  27.  
  28.     imagefill($idest, 0, 0, $rgb);
  29.     imagecopyresampled($idest, $isrc, $new_left, $new_top, 0, 0, $new_width, $new_height, $size[0], $size[1]);
  30.  
  31.     imagejpeg($idest, $dest, $quality);
  32.  
  33.     imagedestroy($isrc);
  34.     imagedestroy($idest);
  35.  
  36.     return true;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment