PHP code for proportional scaling of uploaded image thumbnails

WBOY
Release: 2016-07-25 08:52:16
Original
1065 people have browsed it
  1. /**
  2. * *
  3. *Congruent scaling
  4. * @param unknown_type $srcImage source image path
  5. * @param unknown_type $toFile target image path
  6. * @param unknown_type $maxWidth maximum width
  7. * @param unknown_type $maxHeight maximum height
  8. * @param unknown_type $imgQuality image quality
  9. * @return unknown
  10. */
  11. function resize($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100)
  12. {
  13. list($width, $height, $type, $attr) = getimagesize($srcImage);
  14. if($width < $maxWidth || $height < $maxHeight) return ;
  15. switch ($type) {
  16. case 1: $img = imagecreatefromgif($srcImage); break;
  17. case 2: $img = imagecreatefromjpeg($srcImage); break;
  18. case 3: $img = imagecreatefrompng($srcImage); break;
  19. }
  20. $scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例
  21. if($scale < 1) {
  22. $newWidth = floor($scale*$width);
  23. $newHeight = floor($scale*$height);
  24. $newImg = imagecreatetruecolor($newWidth, $newHeight);
  25. imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
  26. $newName = "";
  27. $toFile = preg_replace("/(.gif|.jpg|.jpeg|.png)/i","",$toFile);
  28. switch($type) {
  29. case 1: if(imagegif($newImg, "$toFile$newName.gif", $imgQuality))
  30. return "$newName.gif"; break;
  31. case 2: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
  32. return "$newName.jpg"; break;
  33. case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality))
  34. return "$newName.png"; break;
  35. default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
  36. return "$newName.jpg"; break;
  37. }
  38. imagedestroy($newImg);
  39. }
  40. imagedestroy($img);
  41. return false;
  42. }
复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!