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";
}
?>

發佈了80 篇原創文章 · 獲贊 7 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章