欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php生成缩略图代码

程序员文章站 2022-06-08 15:55:22
...
  1. /**********************
  2. *@filename - path to the image
  3. *@tmpname - temporary path to thumbnail
  4. *@xmax - max width
  5. *@ymax - max height
  6. */
  7. function resize_image($filename, $tmpname, $xmax, $ymax)
  8. {
  9. $ext = explode(".", $filename);
  10. $ext = $ext[count($ext)-1];
  11. if($ext == "jpg" || $ext == "jpeg")
  12. $im = imagecreatefromjpeg($tmpname);
  13. elseif($ext == "png")
  14. $im = imagecreatefrompng($tmpname);
  15. elseif($ext == "gif")
  16. $im = imagecreatefromgif($tmpname);
  17. $x = imagesx($im);
  18. $y = imagesy($im);
  19. if($x return $im;
  20. if($x >= $y) {
  21. $newx = $xmax;
  22. $newy = $newx * $y / $x;
  23. }
  24. else {
  25. $newy = $ymax;
  26. $newx = $x / $y * $newy;
  27. }
  28. $im2 = imagecreatetruecolor($newx, $newy);
  29. imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
  30. return $im2;
  31. }
复制代码

php