PHP生成缩略图实例讲解

 3861

这篇文章主要介绍了PHP生成缩略图实例讲解,文章列举了实例代码,有正好需要的同学可以借鉴下。


封装的方法函数:

  1. <?php
  2. /**
  3.  * 生成缩略图
  4.  * $imgSrc     图片源路径
  5.  * $thumbWidth   缩略图宽度
  6.  * $thumbHeight  缩略图高度
  7.  * $thumbSrc    缩略图路径
  8.  * $isCut     是否剪切图片
  9.  */
  10. function createThumbImg($imgSrc, $thumbWidth, $thumbHeight, $thumbSrc, $isCut = false) {
  11.     //1.获取图片的类型
  12.     $type = substr(strrchr($imgSrc, "."), 1);
  13.     //2.初始化图象
  14.     if ($type == "jpg" || $type == "jpeg") {
  15.         //创建一块画布,并从JPEG文件或URL地址载入一副图像
  16.         $sourceImg = imagecreatefromjpeg($imgSrc);
  17.     }elseif ($type == "gif") {
  18.         //创建一块画布,并从GIF文件或URL地址载入一副图像
  19.         $sourceImg = imagecreatefromgif($imgSrc);
  20.     }elseif ($type == "png") {
  21.         //创建一块画布,并从PNG文件或URL地址载入一副图像
  22.         $sourceImg = imagecreatefrompng($imgSrc);
  23.     }elseif ($type == "wbmp") {
  24.         //创建一块画布,并从WBMP文件或URL地址载入一副图像
  25.         $sourceImg = imagecreatefromwbmp($imgSrc);
  26.     }
  27.     //取得图像宽度
  28.     $width = imagesx($sourceImg);
  29.     //取得图像高度
  30.     $height = imagesy($sourceImg);
  31.  
  32.     //3.生成图象
  33.     //缩略图的图象比例
  34.     $scale = ($thumbWidth) / ($thumbHeight);
  35.     //源图片的图象比例
  36.     $ratio = ($width) / ($height);
  37.     if (($isCut) == 1) {
  38.         //高度优先
  39.         if ($ratio >= $scale) {        
  40.             //创建真彩图像资源(imagecreatetruecolor()函数使用GDLibrary创建新的真彩色图像)
  41.             $newimg = imagecreatetruecolor($thumbWidth, $thumbHeight);
  42.             //图像处理
  43.             imagecopyresampled($newimg, $sourceImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, (($height) * $scale), $height);
  44.             //以JPEG格式将图像输出到浏览器或文件
  45.             ImageJpeg($newimg, $thumbSrc);
  46.         }
  47.         //宽度优先
  48.         if ($ratio < $scale) {       
  49.             $newimg = imagecreatetruecolor($thumbWidth, $thumbHeight);
  50.             imagecopyresampled($newimg, $sourceImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $width, (($width) / $scale));
  51.             ImageJpeg($newimg, $thumbSrc);
  52.         }
  53.     } else {
  54.         if ($ratio >= $scale) {
  55.             $newimg = imagecreatetruecolor($thumbWidth, ($thumbWidth) / $ratio);
  56.             imagecopyresampled($newimg, $sourceImg, 0, 0, 0, 0, $thumbWidth, ($thumbWidth) / $ratio, $width, $height);
  57.             ImageJpeg($newimg, $thumbSrc);
  58.         }
  59.         if ($ratio < $scale) {
  60.             $newimg = imagecreatetruecolor(($thumbHeight) * $ratio, $thumbHeight);
  61.             imagecopyresampled($newimg, $sourceImg, 0, 0, 0, 0, ($thumbHeight) * $ratio, $thumbHeight, $width, $height);
  62.             ImageJpeg($newimg, $thumbSrc);
  63.         }
  64.     }
  65.     //销毁图像
  66.     ImageDestroy($sourceImg);
  67. }
  68. ?>

调用示例:

  1. <?php
  2.     //图片源路径
  3.     $imgSrc="D:/PHP/test/demo.jpg";
  4.     //缩略图路径
  5.     $thumbSrc="D:/PHP/test/thumb.jpg";
  6.     createThumbImg($path,100,100,$thumbSrc);
  7. ?>



TAG标签:
本文网址:https://www.zztuku.com/detail-8710.html
站长图库 - PHP生成缩略图实例讲解
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐