搜索
首页php教程PHP源码织梦CMS中图片处理类

代码

<?php   if(!defined(&#39;DEDEINC&#39;)) exit(&#39;dedecms&#39;);
/**
 * 图像处理类
 *
 * @version        $Id: image.class.php 1 18:10 2010年7月5日Z tianya $
 * @package        DedeCMS.Libraries
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */
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[&#39;mime&#39;])
        {
            case &#39;image/jpeg&#39;:
                $this->imagecreatefromfunc = function_exists(&#39;imagecreatefromjpeg&#39;) ? &#39;imagecreatefromjpeg&#39; : &#39;&#39;;
                $this->imagefunc = function_exists(&#39;imagejpeg&#39;) ? &#39;imagejpeg&#39; : &#39;&#39;;
                break;
            case &#39;image/gif&#39;:
                $this->imagecreatefromfunc = function_exists(&#39;imagecreatefromgif&#39;) ? &#39;imagecreatefromgif&#39; : &#39;&#39;;
                $this->imagefunc = function_exists(&#39;imagegif&#39;) ? &#39;imagegif&#39; : &#39;&#39;;
                break;
            case &#39;image/png&#39;:
                $this->imagecreatefromfunc = function_exists(&#39;imagecreatefrompng&#39;) ? &#39;imagecreatefrompng&#39; : &#39;&#39;;
                $this->imagefunc = function_exists(&#39;imagepng&#39;) ? &#39;imagepng&#39; : &#39;&#39;;
                break;
        }//为空则匹配类型的函数不存在

        $this->attach[&#39;size&#39;] = empty($this->attach[&#39;size&#39;]) ? @filesize($targetfile) : $this->attach[&#39;size&#39;];
        if($this->attachinfo[&#39;mime&#39;] == &#39;image/gif&#39;)
        {
            $fp = fopen($targetfile, &#39;rb&#39;);
            $targetfilecontent = fread($fp, $this->attach[&#39;size&#39;]);
            fclose($fp);
            $this->animatedgif = strpos($targetfilecontent, &#39;NETSCAPE2.0&#39;) === 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[&#39;size&#39;] = filesize($this->targetfile);
        }
    }

    /**
     *  图片水印
     *
     * @access    public
     * @param     int   $preview  是否预览
     * @return    void
     */
    function watermark($preview = 0)
    {
        if($this->watermarkminwidth && $this->attachinfo[0] <= $this->watermarkminwidth && $this->watermarkminheight && $this->attachinfo[1] <= $this->watermarkminheight)
        {
            return ;
        }
        $this->watermark_gd($preview);
    }

    /**
     *  使用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(&#39;imagecreatetruecolor&#39;) && function_exists(&#39;imagecopyresampled&#39;) && function_exists(&#39;imagejpeg&#39;))
        {
            $imagecreatefromfunc = $this->imagecreatefromfunc;
            $imagefunc = $this->thumbstatus == 1 ? &#39;imagejpeg&#39; : $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[&#39;height&#39;] = ceil($x_ratio * $imageheight);
                    $thumb[&#39;width&#39;] = $thumbwidth;
                }
                else
                {
                    $thumb[&#39;width&#39;] = ceil($y_ratio * $imagewidth);
                    $thumb[&#39;height&#39;] = $thumbheight;
                }
                $targetfile = !$preview ? ($this->thumbstatus == 1 ? $this->targetfile.&#39;.thumb.jpg&#39; : $this->targetfile) : &#39;./watermark_tmp.jpg&#39;;
                $thumb_photo = imagecreatetruecolor($thumb[&#39;width&#39;], $thumb[&#39;height&#39;]);
                imagecopyresampled($thumb_photo, $attach_photo, 0, 0, 0, 0, $thumb[&#39;width&#39;], $thumb[&#39;height&#39;], $imagewidth, $imageheight);
                if($this->attachinfo[&#39;mime&#39;] == &#39;image/jpeg&#39;)
                {
                    $imagefunc($thumb_photo, $targetfile, 100);
                }
                else
                {
                    $imagefunc($thumb_photo, $targetfile);
                }
                $this->attach[&#39;thumb&#39;] = $this->thumbstatus == 1 ? 1 : 0;
            }
        }
    }

    /**
     *  使用gd进行水印
     *
     * @access    public
     * @param     int   $preview  是否预览
     * @return    void
     */
    function watermark_gd($preview = 0)
    {
        if($this->watermarkstatus && function_exists(&#39;imagecopy&#39;) && function_exists(&#39;imagealphablending&#39;) && function_exists(&#39;imagecopymerge&#39;))
        {
            $imagecreatefunc = $this->imagecreatefromfunc;
            $imagefunc = $this->imagefunc;
            list($imagewidth, $imageheight) = $this->attachinfo;
            if($this->watermarktype < 2)
            {
                $watermark_file = $this->watermarktype == 1 ? DEDEDATA.&#39;/mark/mark.png&#39; : DEDEDATA.&#39;/mark/mark.gif&#39;;
                $watermarkinfo = @getimagesize($watermark_file);
                $watermark_logo = $this->watermarktype == 1 ? @imagecreatefrompng($watermark_file) : @imagecreatefromgif($watermark_file);
                if(!$watermark_logo)
                {
                    return ;
                }
                list($logowidth, $logoheight) = $watermarkinfo;
            }
            else
            {
                $box = @imagettfbbox($this->watermarktext[&#39;size&#39;], $this->watermarktext[&#39;angle&#39;], $this->watermarktext[&#39;fontpath&#39;],$this->watermarktext[&#39;text&#39;]);
                $logowidth = max($box[2], $box[4]) - min($box[0], $box[6]);
                $logoheight = max($box[1], $box[3]) - min($box[5], $box[7]);
                $ax = min($box[0], $box[6]) * -1;
                $ay = min($box[5], $box[7]) * -1;
            }
            $wmwidth = $imagewidth - $logowidth;
            $wmheight = $imageheight - $logoheight;
            if(($this->watermarktype < 2 && is_readable($watermark_file) || $this->watermarktype == 2) && $wmwidth > 10 && $wmheight > 10 && !$this->animatedgif)
            {
                switch($this->watermarkstatus)
                {
                    case 1:

                        $x = +5;
                        $y = +5;
                        break;
                    case 2:
                        $x = ($imagewidth - $logowidth) / 2;
                        $y = +5;
                        break;
                    case 3:
                        $x = $imagewidth - $logowidth - 5;
                        $y = +5;
                        break;
                    case 4:
                        $x = +5;
                        $y = ($imageheight - $logoheight) / 2;
                        break;
                    case 5:
                        $x = ($imagewidth - $logowidth) / 2;
                        $y = ($imageheight - $logoheight) / 2;
                        break;
                    case 6:
                        $x = $imagewidth - $logowidth - 5;
                        $y = ($imageheight - $logoheight) / 2;
                        break;
                    case 7:
                        $x = +5;
                        $y = $imageheight - $logoheight - 5;
                        break;
                    case 8:
                        $x = ($imagewidth - $logowidth) / 2;
                        $y = $imageheight - $logoheight - 5;
                        break;
                    case 9:
                        $x = $imagewidth - $logowidth - 5;
                        $y = $imageheight - $logoheight -5;
                        break;
                }
                $dst_photo = @imagecreatetruecolor($imagewidth, $imageheight);
                $target_photo = $imagecreatefunc($this->targetfile);
                imagecopy($dst_photo, $target_photo, 0, 0, 0, 0, $imagewidth, $imageheight);
                if($this->watermarktype == 1)
                {
                    imagecopy($dst_photo, $watermark_logo, $x, $y, 0, 0, $logowidth, $logoheight);
                }
                elseif($this->watermarktype == 2)
                {
                    if(($this->watermarktext[&#39;shadowx&#39;] || $this->watermarktext[&#39;shadowy&#39;]) && $this->watermarktext[&#39;shadowcolor&#39;])
                    {
                        $shadowcolorrgb = explode(&#39;,&#39;, $this->watermarktext[&#39;shadowcolor&#39;]);
                        $shadowcolor = imagecolorallocate($dst_photo, $shadowcolorrgb[0], $shadowcolorrgb[1], $shadowcolorrgb[2]);
                        imagettftext($dst_photo, $this->watermarktext[&#39;size&#39;], $this->watermarktext[&#39;angle&#39;],
                        $x + $ax + $this->watermarktext[&#39;shadowx&#39;], $y + $ay + $this->watermarktext[&#39;shadowy&#39;], $shadowcolor,
                        $this->watermarktext[&#39;fontpath&#39;], $this->watermarktext[&#39;text&#39;]);
                    }
                    $colorrgb = explode(&#39;,&#39;, $this->watermarktext[&#39;color&#39;]);
                    $color = imagecolorallocate($dst_photo, $colorrgb[0], $colorrgb[1], $colorrgb[2]);
                    imagettftext($dst_photo, $this->watermarktext[&#39;size&#39;], $this->watermarktext[&#39;angle&#39;],
                    $x + $ax, $y + $ay, $color, $this->watermarktext[&#39;fontpath&#39;], $this->watermarktext[&#39;text&#39;]);
                }
                else
                {
                    imagealphablending($watermark_logo, true);
                    imagecopymerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logowidth, $logoheight, $this->watermarktrans);
                }
                $targetfile = !$preview ? $this->targetfile : &#39;./watermark_tmp.jpg&#39;;
                if($this->attachinfo[&#39;mime&#39;] == &#39;image/jpeg&#39;)
                {
                    $imagefunc($dst_photo, $targetfile, $this->watermarkquality);
                }
                else
                {
                    $imagefunc($dst_photo, $targetfile);
                }
                $this->attach[&#39;size&#39;] = filesize($this->targetfile);
            }
        }
    }
}//End Class
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中