PHP 產生縮圖片的有關問題

來源:互聯網
上載者:User
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
 __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) < $thumbheight)                {                    $thumb['height'] = ceil($x_ratio * $imageheight);                    $thumb['width'] = $thumbwidth;                }                else                {                    $thumb['width'] = ceil($y_ratio * $imagewidth);                    $thumb['height'] = $thumbheight;                }                $targetfile = !$preview ? ($this->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 
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.