日期:2014-05-17  浏览次数:20658 次

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 code

 <?php   
/**
 * 图像处理类 
 */
class image
{
    var $attachinfo;
    var $targetfile;    //图片路径
    var $imagecreatefromfunc;
    var $imagefunc;
    var $attach;
    var $animatedgif;
    var $watermarkquality;
    var $watermarktext;
    var $thumbstatus;
    var $watermarkstatus;
    
    // 析构函数,兼容PHP4
    function 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' : '';
                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);
        }
    }

    

    /**