php实现图片加水印

1) 判断GD库是否加载,文件是否存在
2)获取图片的宽和高,并计算出水印字符的宽和高,进行比较$img_height<$font_width等时,直接返回,不加水印
3)根据图片类型加载响应的文件imagecreatefromXXXXX
4)如果目标文件不存在,则把水印加载在原始图片,如果保存为jpeg时的压缩小于0 或者大于100时,取边界值,
如果水印字符的大小小于0或者大于5时,取边界值,如果水印颜色不是7位的16进制格式时,直接返回
5)根据加水印的位置1,2,3,4,5,6,7非别对应左上,右上,中间,左下,右下,随机位置,单个水印字符的随机颜色
6)根据原始文件的类型,保存为对应的格式,如果目标文件未指定,则把水印加载在原始文件上面

<?php
function water($r_img, $d_img='',$gzip=80,$str="www.verycd.com", $f_size=5, $pos=5,$f_color="#ff0000")
{
    if(extension_loaded("gd") && file_exists($r_img))
    {
        $img_info=  getimagesize($r_img);
        $img_width=  $img_info[0];
        $img_height=  $img_info[1];
        if($f_size <1 ) $f_size=1;
        if($f_size>5) $f_size=5;

        $font_width=  imagefontwidth($f_size)*strlen($str);
        $font_height=  imagefontheight($f_size);

        if($img_width < $font_width && $img_height < $font_height) return false;
        if(strlen($f_color)!=7) return false;

        switch($img_info[2])
        {
            case 1:
                $img = imagecreatefromgif($r_img);
                break;
            case 2:
                $img=  imagecreatefromjpeg($r_img);
                break;
            case 3:
                $img=  imagecreatefrompng($r_img);
                break;
            default:
                return false;
        }
        $r=hexdec( substr($f_color, 1,2));
        $g =hexdec( substr($f_color, 3,2));
        $b =hexdec(substr($f_color, 5,2));
        $font_color=  imagecolorallocate($img, $r, $g, $b);
        /*
         *  pos=1 left_top
         * pos=2 right_top
         * pos=3 middle
         * pos=4 left_bottom
         * pos=5 right_bottom
         * pos=6  mt_rand
         * default mt_rand with color
         */
        switch($pos)
        {
            case 1:
                imagestring($img, $f_size, 5, 5, $str, $font_color);
                break;
            case 2:
                imagestring($img, $f_size, $img_info[0]-$font_width, $font_height, $str, $font_color);
                break;
            case 3:
                imagestring($img, $f_size, ceil(($img_info[0]-$font_width)/2),ceil( ($img_info[1]-$font_height)/2), $str, $font_color);
                break;
            case 4:
                imagestring($img, $f_size,5, $img_info[1]-$font_height, $str, $font_color );
                break;
            case 5:
                imagestring($img, $f_size, $img_info[0]-$font_width, $img_info[1]-$font_height, $str, $font_color);
                break;
          case 6:
                imagestring($img, $f_size, mt_rand(5, $img_info[0]-$font_width), mt_rand(5, $img_info[1]-$font_height), $str, $font_color);
                break;
            default :
                $len =strlen($str);
                for($i=0; $i<$len; $i++)
                {
                    imagechar($img, $f_size, ceil(($img_info[0]-$font_width)/2),ceil( ($img_info[1]-$font_height)/2),$str[$i],
                            imagecolorallocate($img, mt_rand(0,255),mt_rand(0,255), mt_rand(0,255)));

                }
                 break;

        }
        $d_img = $d_img==''?$r_img:$d_img;
        if($gzip<0) $gzip=0;
        if($gzip>100) $gzip=100;
        switch($img_info[2])
        {
            case 1:
                imagegif($img, $d_img);
                break;
            case 2:
                imagejpeg($img, $d_img, $gzip);
                break;
            case 3:
                imagepng($img, $d_img);
                break;
        }


        return true;
    }
    return false;

}

if(water("./image/1.jpeg"))
{
    echo "add water successed";
}
else
{
    echo "add water failed";
}
?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值