当前位置:Gxlcms > PHP教程 > PHP按最大宽高缩略图片函数代码

PHP按最大宽高缩略图片函数代码

时间:2021-07-01 10:21:17 帮助过:2人阅读

很简单的按照最大宽高来缩略图片的代码,方便新手和懒得自己写的人




  1. function thumb($imagefile, $maxwidth, $maxheight) {
  2. $dim = getimagesize($imagefile);
  3. $width = $dim[0]; //原图宽度
  4. $height = $dim[1]; //原图高度
  5. $original = imagecreatefromjpeg($imagefile);
  6. $thcrown = $maxwidth/$maxheight; //缩略图最大宽度与最大高度比
  7. $crown = $width/$height; //原图宽高比
  8. if($crown/$thcrown >= 1){
  9. $thumbWidth = $maxwidth;
  10. $thumbHeight = $maxwidth/$crown;
  11. } else {
  12. $thumbHeight = $maxheight;
  13. $thumbWidth = $maxheight*$crown;
  14. }
  15. $thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
  16. imagecopyresampled($thumb, $original, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $width, $height);
  17. return $thumb;
  18. }

人气教程排行