php按比例生成缩略图代码

本文章里面image就是你要生成的图片地址,这是一php按比例生成缩略图代码,只要给你图片它就能生成指定大小的图片,并且不变形ekt.

php按比例生成缩略图代码如下:

  1. function resizeimage($image,$width,$height,$scale) {
  2. list($imagewidth, $imageheight, $imagetype) = getimagesize($image);
  3. $imagetype = image_type_to_mime_type($imagetype);
  4. $newimagewidth = ceil($width * $scale);
  5. $newimageheight = ceil($height * $scale);
  6. $newimage = imagecreatetruecolor($newimagewidth,$newimageheight);
  7. switch($imagetype) {
  8. case "image/gif":
  9. $source=imagecreatefromgif($image);
  10. break;
  11. case "image/pjpeg":
  12. case "image/jpeg":
  13. case "image/jpg":
  14. $source=imagecreatefromjpeg($image);
  15. break;
  16. case "image/png":
  17. case "image/x-png":
  18. $source=imagecreatefrompng($image);
  19. break;
  20. }
  21. imagecopyresampled($newimage,$source,0,0,0,0,$newimagewidth,$newimageheight,$width,$height);
  22. switch($imagetype) {
  23. case "image/gif":
  24. imagegif($newimage,$image);
  25. break;
  26. case "image/pjpeg":
  27. case "image/jpeg":
  28. case "image/jpg":
  29. imagejpeg($newimage,$image,90);
  30. break;
  31. case "image/png":
  32. case "image/x-png":
  33. imagepng($newimage,$image);
  34. break;
  35. }
  36. //开源代码phpfensi.com
  37. chmod($image, 0777);
  38. return $image;
  39. }