php实现图片加水印

来源:互联网 发布:unity3d鼠标点击物体 编辑:程序博客网 时间:2024/06/11 18:40
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)根据原始文件的类型,保存为对应的格式,如果目标文件未指定,则把水印加载在原始文件上面
<?phpfunction 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