首页 > 后端开发 > php教程 > PHP 生成缩略图片的有关问题

PHP 生成缩略图片的有关问题

WBOY
发布: 2016-06-13 10:52:48
原创
826 人浏览过

PHP 生成缩略图片的问题
我是这样想....
我设置要生成的缩略图片大小为100*50

我的原图片是1000*600

我想把原图片等比缩放,水平或垂直居中.其它的地方用白色填充,不知道我的意思表达清了没

------解决方案--------------------
原图 w1 h1
新图 w2 h2

if w1/h1 > w2/h2 //缩放后高度不足
新图高 h = h2/w2*w1
垂直居中时的偏移y = (h2-h)/2
x = 0
if w1/h1 < w2/h2 //缩放后宽度不足
新图宽 w = w2/h2*h1
水平居中时的偏移x = (x2-x)/2
y = 0


------解决方案--------------------
某cms的图片处理类

PHP 代码
 <?php /*** 图像处理类*/class image{ var $attachinfo;    var $目标文件;    //图片路径 var $imagecreatefromfunc;    var $imagefunc;    var $附加;    var $animatedgif;    var $watermarkquality;    var $watermarktext;    var $thumbstatus;    var $watermarkstatus;        // 解析结构函数,兼容PHP4函数 image($targetfile, $cfg_thumb, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $cfg_watermarktype, $photo_marktrans,$trueMarkimg, $attach = array()) { $ this->__construct($targetfile, $cfg_thumb, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $cfg_watermarktype, $photo_marktrans,$trueMarkimg, $attach);    } // 解析结构函数 function __construct($targetfile, $cfg_thumb, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $cfg_watermarktype, $photo_marktrans,$trueMarkimg, $attach = array()) { $this- >thumbstatus = $cfg_thumb;        $this->watermarktext = $cfg_watermarktext;        $this->watermarkstatus = $photo_waterpos;        $this->watermarkquality = $photo_marktrans;        $this->watermarkminwidth = $photo_wwidth;        $this->watermarkminheight = $photo_wheight;        $this->watermarktype = $cfg_watermarktype;        $this->watermarktrans = $photo_diaphaneity;        $this->animatedgif = 0;        $this->targetfile = $targetfile;        $this->attachinfo = @getimagesize($targetfile);        $this->attach = $attach;        switch($this->attachinfo['mime']) { case 'image/jpeg': $this->imagecreatefromfunc = function_exists('imagecreatefromjpeg') ? 'imagecreatefromjpeg':'';                $this->imagefunc = function_exists('imagejpeg') ? 'imagejpeg':'';                休息;            case 'image/gif': $this->imagecreatefromfunc = function_exists('imagecreatefromgif') ? 'imagecreatefromgif':'';                $this->imagefunc = function_exists('imagegif') ? 'imagegif':'';                休息;            case 'image/png': $this->imagecreatefromfunc = function_exists('imagecreatefrompng') ? 'imagecreatefrompng':'';                $this->imagefunc = function_exists('imagepng') ? 'imagepng':'';                休息;        }//为空则匹配类型的函数不存在 $this->attach['size'] = empty($this->attach['size']) ? @filesize($targetfile) : $this->attach['size'];        if($this->attachinfo['mime'] == 'image/gif') { $fp = fopen($targetfile, 'rb'); }            $targetfilecontent = fread($fp, $this->attach['size']);            fclose($fp);            $this->animatedgif = strpos($targetfilecontent, 'NETSCAPE2.0') === false ? 0:1;        /***  生成缩略图     *     * @access    public     * @param     int  $thumbwidth  图片宽度     * @param     int  $thumbheight  图片高度     * @param     int  $preview  是否预览     * @return    void*/ 函数拇指($thumbwidth, $thumbheight, $preview = 0) { $this->thumb_gd($thumbwidth, $thumbheight, $preview);        if($this->thumbstatus == 2 && $this->watermarkstatus) { $this->image($this->targetfile, $this->attach);            $this->attach['size'] = 文件大小($this->targetfile);        } } /***  使用gd生成缩略图     *     * @access    public     * @param     int  $thumbwidth  图片宽度     * @param     int  $thumbheight  图片高度     * @param     int  $preview  是否预览     * @return    void*/ functionthumb_gd($thumbwidth, $thumbheight, $preview = 0) { if($this->thumbstatus && function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled') && function_exists( 'imagejpeg')) { $imagecreatefromfunc = $this->imagecreatefromfunc;            $imagefunc = $this->thumbstatus == 1 ? 'imagejpeg' : $this->imagefunc;            列表($imagewidth, $imageheight) = $this->attachinfo;            if(!$this->animatedgif && ($imagewidth >= $thumbwidth || $imageheight >= $thumbheight)) { $attach_photo = $imagecreatefromfunc($this->targetfile);                $x_ratio = $thumbwidth / $imagewidth;                $y_ratio = $thumbheight / $imageheight;                if(($x_ratio * $imageheight) thumbstatus == 1 ? $this->targetfile.'.thumb.jpg' : $this->targetfile) : './watermark_tmp.jpg';                $thumb_photo = imagecreatetruecolor($thumb['宽度'], $thumb['高度']);                imagecopyresampled($thumb_photo, $attach_photo, 0, 0, 0, 0, $thumb['width'], $thumb['height'], $imagewidth, $imageheight);                if($this->attachinfo['mime'] == 'image/jpeg') { $imagefunc($thumb_photo, $targetfile, 100); }                } else { $imagefunc($thumb_photo, $targetfile);                } $this->attach['thumb'] = $this->thumbstatus == 1 ? 1:0;            } } } }//课程结束<div class="clear"></div>
登录后复制
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板