Задача: Масштабирование, пропорциональное изменение размеров картинки
Исходник: Вариант №2 из комментов к документации php, язык: php [code #101, hits: 8186]
автор: - [добавлен: 26.03.2006]
  1. /**
  2. * Resize an image and keep the proportions
  3. * @author Allison Beckwith <allison@planetargon.com>
  4. * @param string $filename
  5. * @param integer $max_width
  6. * @param integer $max_height
  7. * @return image
  8. */
  9. function resizeImage($filename, $max_width, $max_height)
  10. {
  11. list($orig_width, $orig_height) = getimagesize($filename);
  12. $width = $orig_width;
  13. $height = $orig_height;
  14. # taller
  15. if ($height > $max_height) {
  16. $width = ($max_height / $height) * $width;
  17. $height = $max_height;
  18. }
  19. # wider
  20. if ($width > $max_width) {
  21. $height = ($max_width / $width) * $height;
  22. $width = $max_width;
  23. }
  24. $image_p = imagecreatetruecolor($width, $height);
  25. $image = imagecreatefromjpeg($filename);
  26. imagecopyresampled($image_p, $image, 0, 0, 0, 0,
  27. $width, $height, $orig_width, $orig_height);
  28. return $image_p;
  29. }
  30.  
robby at planetargon dot com (28-Feb-2005 03:56):

"Most of the examples below don't keep the proportions properly. They keep using if/else for the height/width..and forgetting that you might have a max height AND a max width, not one or the other."

http://www.php.net/imagecopyresized/

Тестировалось на: Apache 1.3.33, PHP 5.0

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