Задача: Масштабирование, пропорциональное изменение размеров картинки
Исходник: php image resize, язык: php [code #628, hits: 12921]
аноним: Сергей [добавлен: 11.08.2011]
  1. function img_resize($src, $dest, $width, $height, $rgb = 0x000000, $quality = 100) {
  2. if (!file_exists($src)) {
  3. return false;
  4. }
  5.  
  6. $size = getimagesize($src);
  7.  
  8. if ($size === false) {
  9. return false;
  10. }
  11.  
  12. $format = strtolower(substr($size['mime'], strpos($size['mime'], '/') + 1));
  13. $icfunc = 'imagecreatefrom'.$format;
  14.  
  15. if (!function_exists($icfunc)) {
  16. return false;
  17. }
  18.  
  19. $x_ratio = $width / $size[0];
  20. $y_ratio = $height / $size[1];
  21.  
  22. if ($height == 0) {
  23.  
  24. $y_ratio = $x_ratio;
  25. $height = $y_ratio * $size[1];
  26.  
  27. } elseif ($width == 0) {
  28.  
  29. $x_ratio = $y_ratio;
  30. $width = $x_ratio * $size[0];
  31.  
  32. }
  33.  
  34. $ratio = min($x_ratio, $y_ratio);
  35. $use_x_ratio = ($x_ratio == $ratio);
  36.  
  37. $new_width = $use_x_ratio ? $width : floor($size[0] * $ratio);
  38. $new_height = !$use_x_ratio ? $height : floor($size[1] * $ratio);
  39. $new_left = $use_x_ratio ? 0 : floor(($width - $new_width) / 2);
  40. $new_top = !$use_x_ratio ? 0 : floor(($height - $new_height) / 2);
  41.  
  42. $isrc = $icfunc($src);
  43. $idest = imagecreatetruecolor($width, $height);
  44. $rgb2 = imagecolorallocate($idest, 245, 245, 245);
  45. imagefill($idest, 0, 0, $rgb2);
  46. imagecopyresampled($idest, $isrc, $new_left, $new_top, 0, 0, $new_width, $new_height, $size[0], $size[1]);
  47.  
  48. imagejpeg($idest, $dest, $quality);
  49.  
  50. imagedestroy($isrc);
  51. imagedestroy($idest);
  52.  
  53. return true;
  54. }
php image resize

+добавить реализацию