当前位置:编程学习 > php >>

php给图片加水印六

<?

 function _countMaskPos()
    {
        if($this->_isFull())
        {
            switch($this->mask_position)
            {
                case 1:
                    // 左上
                    $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
                    $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
                    break;
 
                case 2:
                    // 左下
                    $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
                    $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;
                    break;
 
                case 3:
                    // 右上
                    $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;
                    $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
                    break;
 
                case 4:
                    // 右下
                    $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;
                    $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;
                    break;
 
                default:
                    // 默认将水印放到右下,偏移指定像素
                    $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;
                    $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;
                    break;
            }
        }
        else
        {
            switch($this->mask_position)
            {
                case 1:
                    // 左上
                    $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
                    $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
                    break;
 
                case 2:
                    // 左下
                    $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
                    $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;
                    break;
 
                case 3:
                    // 右上
                    $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;
                    $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
                    break;
 
                case 4:
                    // 右下
                    $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;
                    $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;
                    break;
 
                default:
                    // 默认将水印放到右下,偏移指定像素
                    $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;
                    $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;
                    break;
            }
        }
    }
 
    /**
     * 设置字体信息
     */
    function _setFontInfo()
    {
        if(is_numeric($this->font))
        {
            $this->font_w  = imagefontwidth($this->font);
            $this->font_h  = imagefontheight($this->font);
 
            // 计算水印字体所占宽高
            $word_length   = strlen($this->mask_word);
            $this->mask_w  = $this->font_w*$word_length;
            $this->mask_h  = $this->font_h;
        }
        else
        {
            $arr = imagettfbbox ($this->font_size,0, $this->font,$this->mask_word);
            $this->mask_w  = abs($arr[0] - $arr[2]);
            $this->mask_h  = abs($arr[7] - $arr[1]);
        }
    }
 
    /**
     * 设置新图尺寸
     *
     * @param    integer     $img_w   目标宽度
     * @param    integer     $img_h   目标高度
     */
    function _setNewImgSize($img_w, $img_h=null)
    {
        $num = func_num_args();
        if(1 == $num)
        {
            $this->img_scale = $img_w;// 宽度作为比例
            $this->fill_w = round($this->src_w * $this->img_scale / 100) - $this->img_border_size*2;
            $this->fill_h = round($this->src_h * $this->img_scale / 100) - $this->img_border_size*2;
 
            // 源文件起始坐标
            $this->src_x  = 0;
            $this->src_y  = 0;
            $this->copy_w = $this->src_w;
            $this->copy_h = $this->src_h;
 
            // 目标尺寸
            $this->dst_w   = $this->fill_w + $this->img_border_size*2;
            $this->dst_h   = $this->fill_h + $this->img_border_size*2;
        }
 
        if(2 == $num)
        {
            $fill_w   = (int)$img_w - $this->img_border_size*2;
            $fill_h   = (int)$img_h - $this->img_border_size*2;
            if($fill_w < 0 || $fill_h < 0)
            {
                die("图片边框过大,已超过了图片的宽度");
            }
            $rate_w = $this->src_w/$fill_w;
            $rate_h = $this->src_h/$fill_h;
 
            switch($this->cut_type)
            {
                case 0:
                    // 如果原图大于缩略图,产生缩小,否则不缩小
                    if($rate_w < 1 && $rate_h < 1)
                    {
                        $this->fill_w = (int)$this->src_w;
                        $this->fill_h = (int)$this->src_h;
                    }
                    else
                    {
                        if($rate_w >= $rate_h)
                        {
                            $this->fill_w = (int)$fill_w;
                            $this->fill_h = round($this->src_h/$rate_w);
                        }
                        else
                        {
                            $this->fill_w = round($this->src_w/$rate_h);
                            $this->fill_h = (int)$fill_h;
                        }
                    }
 
                    $this->src_x  = 0;
                    $this->src_y  = 0;
 
                    $this->copy_w = $this->src_w;
                    $this->copy_h = $this->src_h;
 
                    // 目标尺寸
                    $this->dst_w   = $this->fill_w + $this->img_border_size*2;
                    $this->dst_h   = $this->fill_h + $this->img_border_size*2 - 10 ;//补偿round误差 消除白边;
                    break;
 
                // 自动裁切
                case 1:
                    // 如果图片是缩小剪切才进行操作
                    if($rate_w >= 1 && $rate_h >=1)
                    {
                        if($this->src_w > $this->src_h)
                        {
                            $src_x = round($this->src_w-$this->src_h)/2;
                            $this->setSrcCutPosition($src_x, 0);
                            $this->setRectangleCut($fill_h, $fill_h);
 
                            $this->copy_w = $this->src_h;
                            $this->copy_h = $this->src_h;
                           
                        }
                        elseif($this->src_w < $this->src_h)
                        {
                            $src_y = round($this->src_h-$this->src_w)/2;
                            $this->setSrcCutPosition(0, $src_y);
                            $this->setRectangleCut($fill_w, $fill_h);
 
                            $this->copy_w = $this->src_w;
                            $this->copy_h = $this->src_w;
                        }
                        else
                        {
                            $this->setSrcCutPosition(0, 0);
                            $this->copy_w = $this->src_w;
                            $this->copy_h = $this->src_w;
                            $this->setRectangleCut($fill_w, $fill_h);
                        }
                    }
                    else
                    {
                        $this->setSrcCutPosition(0, 0);
                        $this->setRectangleCut($this->src_w, $this->src_h);
 
                        $this->copy_w = $this->src_w;
                        $this->copy_h = $this->src_h;
                    }
 
                    // 目标尺寸
                    $this->dst_w   = $this->fill_w + $this->img_border_size*2;
                    $this->dst_h   = $this->fill_h + $this->img_border_size*2;
                   
                    break;
 
                // 手工裁切
                case 2:
                    $this->copy_w = $this->fill_w;
                    $this->copy_h = $this->fill_h;
 
                    // 目标尺寸
                    $this->dst_w   = $this->fill_w + $this->img_border_size*2;
                    $this->dst_h   = $this->fill_h + $this->img_border_size*2;               
                   
                    break;
                default:
                    break;
 
            }
        }
 
        // 目标文件起始坐标
        $this->start_x = $this->img_border_size;
        $this->start_y = $this->img_border_size;
    }
 
    /**
     * 检查水印图是否大于生成后的图片宽高
     */
    function _isFull()
    {
        Return (   $this->mask_w + $this->mask_offset_x > $this->fill_w
                || $this->mask_h + $this->mask_offset_y > $this->fill_h)
                   ?true:false;
    }
 
    /**
     * 检查水印图是否超过原图
     */
    function _checkMaskValid()
    {
        if(    $this->mask_w + $this->mask_offset_x > $this->src_w
            || $this->mask_h + $this->mask_offset_y > $this->src_h)
        {
            die("水印图片尺寸大于原图,请缩小水印图");
        }
    }
 
    /**
     * 取得图片类型
     *
     * @param    string     $file_path    文件路径
     */
    function _getImgType($file_path)
    {
        $type_list = array("1"=>"gif","2"=>"jpg","3"=>"png","4"=>"swf","5" => "psd","6"=>"bmp","15"=>"wbmp");
        if(file_exists($file_path))
        {
            $img_info = @getimagesize ($file_path);
            if(isset($type_list[$img_info[2]]))
            {
                Return $type_list[$img_info[2]];
            }
        }
        else
        {
            die("文件不存在,不能取得文件类型!");
        }
    }
 
    /**
     * 检查图片类型是否合法,调用了array_key_exists函数,此函数要求
     * php版本大于4.1.0
     *
     * @param    string     $img_type    文件类型
     */
    function _checkValid($img_type)
    {
        if(!array_key_exists($img_type, $this->all_type))
        {
            Return false;
        }
    }
 
    /**
     * 按指定路径生成目录
     *
     * @param    string     $path    路径
     */
    function _mkdirs($path)
    {
        $adir = explode('/',$path);
        $dirlist = '';
        $rootdir = array_shift($adir);
        if(($rootdir!='.'||$rootdir!='..')&&!file_exists($rootdir))
        {
            @mkdir($rootdir);
        }
        foreach($adir as $key=>$val)
        {
            if($val!='.'&&$val!='..')
            {
                $dirlist .= "/".$val;
                $dirpath = $rootdir.$dirlist;
                if(!file_exists($dirpath))
                {
                    @mkdir($dirpath);
                    @chmod($dirpath,0777);
                }
            }
        }
    }
 
    /**
     * 垂直翻转
     *
     * @param    string     $src    图片源
     */
    function _flipV($src)
    {
        $src_x = $this->getImgWidth($src);
        $src_y = $this->getImgHeight($src);
 
        $new_im = imagecreatetruecolor($src_x, $src_y);
        for ($y = 0; $y < $src_y; $y++)
        {
            imagecopy($new_im, $src, 0, $src_y - $y - 1, 0, $y, $src_x, 1);
        }
        $this->h_src = $new_im;
    }
 
    /**
     * 水平翻转
     *
     * @param    string     $src    图片源
     */
    function _flipH($src)
    {
        $src_x = $this->getImgWidth($src);
        $src_y = $this->getImgHeight($src);
 
        $new_im = imagecreatetruecolor($src_x, $src_y);
        for ($x = 0; $x < $src_x; $x++)
        {
            imagecopy($new_im, $src, $src_x - $x - 1, 0, $x, 0, 1, $src_y);
        }
        $this->h_src = $new_im;
    }
}
?>

下面来看使用方法:

require_once('lib/ImageHandler.phpcls');
$imgHandler_Obj = new ImageHandler();

/* 默认缩小放大 */
$imgHandler_Obj->setSrcImg("img/xrk.jpg");
$imgHandler_Obj->setDstImg("tmp/New_Image_0.jpg");
$imgHandler_Obj->setCutType(0);
// 指定缩放比例
$imgHandler_Obj->createImg(20);//50 100 200 试试

 

/* 1, 文字水印图片缩小
 
$imgHandler_Obj->setSrcImg("img/xrk.jpg");
$imgHandler_Obj->setMaskFont("Fonts/STXINGKA.TTF");
$imgHandler_Obj->setMaskFontSize(15);
$imgHandler_Obj->setMaskFontColor("#0061A7");
$imgHandler_Obj->setMaskTxtPct(20);
$imgHandler_Obj->setDstImgBorder(0,"#dddddd");
$imgHandler_Objext = "Happy漫步者";
//$str = mb_convert_encoding($imgHandler_Objext,"UTF-8","GBK");
$imgHandler_Obj->setMaskWord($imgHandler_Objext);
 
// 指定固定宽高 输出到浏览器
$imgHandler_Obj->createImg(400,400);
*/

 


/* 2, 加水印图片缩小
$imgHandler_Obj->setSrcImg("img/test.jpg");
$imgHandler_Obj->setDstImg("tmp/New_Image_2.jpg");
$imgHandler_Obj->setMaskImg("img/t.gif");
$imgHandler_Obj->setMaskPosition(0);//0为在图片右下角加入水印
$imgHandler_Obj->setMaskImgPct(60);
$imgHandler_Obj->setDstImgBorder(1,"#dddddd");
 
// 指定缩放比例
$imgHandler_Obj->createImg(400,400);
*/

 

/* 3, 同步缩小
    $imgHandler_Obj->setSrcImg("img/test.jpg");
    $imgHandler_Obj->setCutType(0);//同步缩小
    $imgHandler_Obj->setDstImg("tmp/New_Image_3.jpg");
    $imgHandler_Obj->createImg(150,160);

 */

/* 4, 剪裁图片
 $imgHandler_Obj->setSrcImg("img/test.jpg");
    $imgHandler_Obj->setCutType(2);//指明为手工裁切
    $imgHandler_Obj->setSrcCutPosition(50, 50);// 源图起点坐标
    $imgHandler_Obj->setRectangleCut(300, 200);// 裁切尺寸
    $imgHandler_Obj->setDstImg("tmp/New_Image_4.jpg");
    $imgHandler_Obj->createImg(300,200);
*/

补充:Php教程,Php常用代码
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,