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

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

程序员文章站 2022-06-14 20:42:26
...
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 新图宽 w = w2/h2*h1
水平居中时的偏移x = (x2-x)/2
y = 0


------解决方案--------------------
某cms的图片处理类
PHP code
 __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' : '';                break;            case 'image/gif':                $this->imagecreatefromfunc = function_exists('imagecreatefromgif') ? 'imagecreatefromgif' : '';                $this->imagefunc = function_exists('imagegif') ? 'imagegif' : '';                break;            case 'image/png':                $this->imagecreatefromfunc = function_exists('imagecreatefrompng') ? 'imagecreatefrompng' : '';                $this->imagefunc = function_exists('imagepng') ? 'imagepng' : '';                break;        }//为空则匹配类型的函数不存在        $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     */    function thumb($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'] = filesize($this->targetfile);        }    }        /**     *  使用gd生成缩略图     *     * @access    public     * @param     int  $thumbwidth  图片宽度     * @param     int  $thumbheight  图片高度     * @param     int  $preview  是否预览     * @return    void     */    function thumb_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;            list($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['width'], $thumb['height']);                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;            }        }    }    }//End Class
PHP 生成缩略图片的有关问题

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频