永发信息网

php创建缩略图问题

答案:1  悬赏:0  手机版
解决时间 2021-02-07 12:18
  • 提问者网友:兔牙战士
  • 2021-02-07 00:25
网上找了好多php创建缩略图类,感觉是那个复杂啊?难道php从一个图像创建缩略图然后保存到另外一个文件夹那么费事吗?我个人认为直接给原图重新定义一个宽、或者高 是否可以就搞定了呢?还是我想的太天真...
最佳答案
  • 五星知识达人网友:忘川信使
  • 2021-02-07 01:47
其实PHP创建缩略图就是在PHP在原图片的基础上创建一张新的图片的过程,而用PHP创建图像的过程一般分成四部:
第一步:创建一张画布(只要是画图都需要一张画布的)
第二步:在画布画东西(可以画各种图形,如长方形,直线,等等,也可以在画布上写字啥的,或者画其他的图形)
第三步:画完图之后,将图片输出,将图片输出到浏览器,在浏览器显示出来,或者保存为一张新 的图片(缩略图一般是保存为图片文件的)
第四步:因为创建画布时打开了文件流,所以要关闭资源,节省内存。(个人觉得你可以这样理解,打开了一画布,把它铺开了,画完了就把画布卷起来,收起来,不要占着铺的地方)


具体的代码如下:(这段代码来源于ThinkPHP的图像类)
<?php
class Thumb{
   
   static create($img, $thumbname, $type='', $maxWidth=200, $maxHeight=50)
   {
       $info = getimagesize($img);    //获取原图的图像信息(长、宽、格式等)
       if ($info !== false) {
            $srcWidth = $info['width'];
            $srcHeight = $info['height'];
            $type = empty($type) ? $info['type'] : $type;
            $type = strtolower($type);
            $interlace = $interlace ? 1 : 0;
            unset($info);
            $scale = min($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 计算缩放比例
            if ($scale >= 1) {
                // 超过原图大小不再缩略
                $width = $srcWidth;
                $height = $srcHeight;
            } else {
                // 缩略图尺寸
                $width = (int) ($srcWidth * $scale);
                $height = (int) ($srcHeight * $scale);
            }

            // 载入原图(在原图的基础上创建画布,为第一步)
            $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
            if(!function_exists($createFun)) {
                return false;
            }
            $srcImg = $createFun($image);

            //第二步开始
            //创建缩略图
            if ($type != 'gif' && function_exists('imagecreatetruecolor'))
                $thumbImg = imagecreatetruecolor($width, $height);
            else
                $thumbImg = imagecreate($width, $height);
              //png和gif的透明处理 by luofei614
            if('png'==$type){
                imagealphablending($thumbImg, false);//取消默认的混色模式(为解决阴影为绿色的问题)
                imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息(为解决阴影为绿色的问题)    
            }elseif('gif'==$type){
                $trnprt_indx = imagecolortransparent($srcImg);
                 if ($trnprt_indx >= 0) {
                        //its transparent
                       $trnprt_color = imagecolorsforindex($srcImg , $trnprt_indx);
                       $trnprt_indx = imagecolorallocate($thumbImg, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
                       imagefill($thumbImg, 0, 0, $trnprt_indx);
                       imagecolortransparent($thumbImg, $trnprt_indx);
              }
            }
            // 复制图片
            if (function_exists("ImageCopyResampled"))
                imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
            else
                imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);

           //第三步:输出图像
            // 生成图片
            $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
            $imageFun($thumbImg, $thumbname);

            //第四步:关闭画布
            imagedestroy($thumbImg);
            imagedestroy($srcImg);
            return $thumbname;
        }
        return false;

   }

}
?>你使用的时候直接用:
require Thumb.class.php
$thumb = Thumb::create('s.jpg','thumb_s.jpg',100,50);希望我的回答你能满意
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯