Задача: Масштабирование, пропорциональное изменение размеров картинки
Исходник: Вариант №1 из комментов к документации php, язык: php [code #100, hits: 9328]
автор: - [добавлен: 26.03.2006]
  1. function resize($cur_dir, $cur_file, $newwidth, $output_dir) {
  2. $dir_name = $cur_dir;
  3. $olddir = getcwd();
  4. $dir = opendir($dir_name);
  5. $filename = $cur_file;
  6. $format='';
  7. if(preg_match("/.jpg/i", "$filename"))
  8. {
  9. $format = 'image/jpeg';
  10. }
  11. if (preg_match("/.gif/i", "$filename"))
  12. {
  13. $format = 'image/gif';
  14. }
  15. if(preg_match("/.png/i", "$filename"))
  16. {
  17. $format = 'image/png';
  18. }
  19. if($format!='')
  20. {
  21. list($width, $height) = getimagesize($filename);
  22. $newheight=$height*$newwidth/$width;
  23. switch($format)
  24. {
  25. case 'image/jpeg':
  26. $source = imagecreatefromjpeg($filename);
  27. break;
  28. case 'image/gif';
  29. $source = imagecreatefromgif($filename);
  30. break;
  31. case 'image/png':
  32. $source = imagecreatefrompng($filename);
  33. break;
  34. }
  35. $thumb = imagecreatetruecolor($newwidth,$newheight);
  36. imagealphablending($thumb, false);
  37. $source = @imagecreatefromjpeg("$filename");
  38. imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  39. $filename="$output_dir/$filename";
  40. @imagejpeg($thumb, $filename);
  41. }
  42. }
  43.  
  44. //call this function using
  45. resize("./input folder", "picture_file_name", "width", "./output folder");
  46.  
no at name dot com (18-May-2005 12:45):

"I was searching for script, that do not resize on the fly, but copy resized file to other place, so after long searches, i've done this function. I hope it will be usefull for someone:
(This is not original code, i'v taked it from somwhere and edited a ltl :)"

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

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

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