1. 程式人生 > 其它 >PHP實現圖片和文字水印(PHP給圖片新增水印功能)

PHP實現圖片和文字水印(PHP給圖片新增水印功能)

PHP實現圖片和文字水印

/************************************************圖片水印功能開始************************************************************/

/**
 * @desc 圖片新增文字水印
 * @param string|file  $source 背景圖片,即需要加水印的圖片,暫只支援GIF,JPG,PNG格式
 * @param int|string  $waterImage  圖片水印,即作為水印的圖片,暫只支援GIF,JPG,PNG格式
 * @param int|string  $waterPos    水印位置,有10種狀態,0為隨機位置 1為頂端居左,2為頂端居中,3為頂端居右; * 4為中部居左,5為中部居中,6為中部居右; * 7為底端居左,8為底端居中,9為底端居右
 * @param int|string  $waterText   文字水印,即把文字作為為水印,支援ASCII碼,不支援中文
 * @param int|string  $fontSize    文字大小,值為1、2、3、4或5,預設為5
 * @param int|string  $textColor   文字顏色,值為十六進位制顏色值,預設為#FF0000(紅色)
 *
 */
function CreateImageWaterMark($source,$waterImage,$waterText,$waterPos = 0, $fontSize=25, $textColor='', $font = '') {
    if(empty($font)){
        $font = ROOT_PATH."/static/common/lib/assets/fonts/simkai.ttf";
    }
    $date =  ROOT_PATH.'static/upload/watermark/' . date ( 'Ymd' ) . '/';
    $img = $date . md5 ( $source .$waterImage. $waterText.$waterPos . $fontSize . $textColor ) . '5.jpg';
    if (file_exists ($img)) {
        return $img;
    }

    $main   = imagecreatefromjpeg ( $source );

    $width  = imagesx ( $main );//原圖寬度
    $height = imagesy ( $main );//原圖高度

    $target = imagecreatetruecolor ( $width, $height );

    if (!empty($textColor) && (strlen($textColor) == 7)) {

        $R = hexdec(substr($textColor, 1, 2));

        $G = hexdec(substr($textColor, 3, 2));

        $B = hexdec(substr($textColor, 5));

        $white = imagecolorallocate($target, $R, $G, $B);
    } else {

        $white = imagecolorallocate ( $target, 255, 0, 0 );//字的RGB顏色
    }

//    $white  = imagecolorallocate ( $target, 255, 255, 255 );
    imagefill ( $target, 0, 0, $white );

    imagecopyresampled ( $target, $main, 0, 0, 0, 0, $width, $height, $width, $height );

    //設定影象的混色模式
    imagealphablending($target, true);


    //水印位置
    if ($waterImage) {//圖片水印
        $water_info = getimagesize($waterImage);

        $water_w = $water_info[0]; //取得水印圖片的寬

        $water_h = $water_info[1]; //取得水印圖片的高

        //判斷水印圖片的寬高
        if ($waterImage && (($width < $water_w) || ($height < $water_h))) {


            //將水印圖按背景圖的比例進行寬度壓縮
            $waterImage = CompressImage($waterImage,$width,$height);

            $water_info = getimagesize($waterImage);

            $water_w = $water_info[0]; //取得水印圖片的寬

            $water_h = $water_info[1]; //取得水印圖片的高

        }

    } else {//文字水印
        $fontBox = imagettfbbox($fontSize, 0, $font, $waterText);//文字水平居中實質
        $water_w = $fontBox[2] - $fontBox[6];
        $water_h = $fontBox[3] - $fontBox[7];
        unset($fontBox);
    }

    switch ($waterPos) {
        case 1://1為頂端居左

            $posX = 0;// // -10 是距離右側10px 可以自己調節

            $posY = $waterImage ? 0 : intval($fontSize);
//            $posY = intval($fontSize);
//            $posY = intval($fontSize) + 10; // + 10 是距離頂部10px 可以自己調節

            break;

        case 2://2為頂端居中

            $posX = ($width - $water_w) / 2;

            $posY = $waterImage ? 0 : intval($fontSize);
//            $posY = intval($fontSize);
//            $posY = intval($fontSize) + 10; // + 10 是距離頂部10px 可以自己調節

            break;

        case 3://3為頂端居右

            $posX = $waterImage ? $width - $water_w : $width - $water_w -10;//-10 是距離右側10px 可以自己調節

            $posY = $waterImage ? 0 : intval($fontSize);
//            $posY = intval($fontSize);
//            $posY = intval($fontSize) + 10; // + 10 是距離頂部10px 可以自己調節

            break;

        case 4://4為中部居左

            $posX = 0;

            $posY = ($height - $water_h) / 2;

            break;

        case 5://5為中部居中

            $posX = ($width - $water_w) / 2;

            $posY = ($height - $water_h) / 2;

            break;

        case 6://6為中部居右

            $posX = $width - $water_w - 10;   // -10 是距離右側10px 可以自己調節

            $posY = ($height - $water_h) / 2;

            break;

        case 7://7為底端居左

            $posX = 0;

            $posY = $height - $water_h;

            break;

        case 8://8為底端居中

            $posX = ($width - $water_w) / 2;

            $posY = $height - $water_h;

            break;

        case 9://9為底端居右

            $posX = $width - $water_w - 10;   // -10 是距離右側10px 可以自己調節

            $posY = $height - $water_h - 10;   // -10 是距離底部10px 可以自己調節

            break;
        case 0://隨機
        default://隨機
            $posX = rand(0, ($width - $water_w));

            $posY = rand(0, ($height - $water_h));

            break;
    }

    if($waterImage && $waterText){
        if (!empty($textColor) && (strlen($textColor) == 7)) {

            $R = hexdec(substr($textColor, 1, 2));

            $G = hexdec(substr($textColor, 3, 2));

            $B = hexdec(substr($textColor, 5));

            $fontColor = imagecolorallocate($target, $R, $G, $B);
        } else {

            $fontColor = imagecolorallocate ( $target, 255, 0, 0 );//字的RGB顏色
        }
        $fontBox = imagettfbbox($fontSize, 0, $font, $waterText);
        imagettftext ( $target, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), 560, $fontColor, $font, $waterText );


        //水印圖片
        $water_info = getimagesize($waterImage);
        switch ($water_info[2]) {//取得水印圖片的格式
            case 1:
                $child1 = imagecreatefromgif($waterImage);
//                @unlink($waterImage);
                break;

            case 2:
                $child1 = imagecreatefromjpeg($waterImage);
//                @unlink($waterImage);
                break;

            case 3:
                $child1 = imagecreatefrompng($waterImage);
//                @unlink($waterImage);
                break;

            default:die('水印圖片格式不支援');
        }
//        $child1 = imagecreatefrompng( $waterImage);
        imagecopymerge ( $target, $child1, $posX, $posY, 0, 0, imagesx ( $child1 ), imagesy ( $child1 ), 100 );
    }elseif($waterImage){
        //水印圖片
        $water_info = getimagesize($waterImage);
        switch ($water_info[2]) {//取得水印圖片的格式
            case 1:
                $child1 = imagecreatefromgif($waterImage);
//                @unlink($waterImage);
                break;

            case 2:
                $child1 = imagecreatefromjpeg($waterImage);
//                @unlink($waterImage);
                break;

            case 3:
                $child1 = imagecreatefrompng($waterImage);
//                @unlink($waterImage);
                break;

            default:die('水印圖片格式不支援');
        }
//        $child1 = imagecreatefrompng( $waterImage);
        imagecopymerge ( $target, $child1, $posX, $posY, 0, 0, imagesx ( $child1 ), imagesy ( $child1 ), 100 );
    }else{
        if (!empty($textColor) && (strlen($textColor) == 7)) {

            $R = hexdec(substr($textColor, 1, 2));

            $G = hexdec(substr($textColor, 3, 2));

            $B = hexdec(substr($textColor, 5));

            $fontColor = imagecolorallocate($target, $R, $G, $B);
        } else {

            $fontColor = imagecolorallocate ( $target, 255, 0, 0 );//字的RGB顏色
        }

        imagettftext ( $target, $fontSize, 0, $posX, $posY, $fontColor, $font, $waterText );
    }

    //eof of 合成圖片
    if(!is_dir($date)){
        @mkdir ($date,0777,true);
    }

    imagejpeg ( $target,$img, 95 );

    imagedestroy ( $main );
    imagedestroy ( $target );
    if($waterImage){
        imagedestroy ( $child1 );
    }
    return $img;
}


/**
 * @desc 按比例壓縮圖片寬高
 * @param string|file $image 圖片,暫只支援GIF,JPG,PNG格式
 * @param int|string  $n_w    圖片寬度
 * @param int|string  $n_h    圖片高度
 *
 */
function CompressImage($image,$n_w,$n_h){
    $date =  ROOT_PATH.'static/upload/watermark/deal/' . date ( 'Ymd' ).'/';
    $imgUrl = $date . md5 ($image) . '.jpg';
    if (file_exists ($imgUrl)) {
        return $imgUrl;
    }
    $size=getimagesize($image);
    switch($size[2]){
        case 1:
            $img=imagecreatefromgif($image);
            break;
        case 2:
            $img=imagecreatefromjpeg($image);
            break;
        case 3:
            $img=imagecreatefrompng($image);
            break;
        default:
            return '圖片格式不支援';
    }
    $width = imagesx($img);//原圖片寬度
    $height= imagesy($img);//原圖片高度

    //縮圖最大寬度與最大高度比
    $thcrown = $n_w/$n_h;

    //原圖寬高比
    $crown = $width/$height;

    if($crown/$thcrown >= 1){
        $n_h = $n_w/$crown;
    } else {
        $n_w = $n_h*$crown;
    }

    //1.建立畫布
    $new = imagecreatetruecolor($n_w, $n_h);
    $img = imagecreatefrompng($image);
    //2.上色
    imagealphablending($new,false);//這裡很重要,意思是不合並顏色,直接用$target_im影象顏色替換,包括透明色;
    imagesavealpha($new,true);//這裡很重要,意思是不要丟了$target_im影象的透明色;
    $tag_white = imagecolorallocatealpha($new, 255, 255, 255,127);//把生成新圖的白色改為透明色 存為tag_white
//    $color=imagecolorallocate($new,255,255,255);
    //3.設定透明
    imagecolortransparent($new,$tag_white);
    imagefill($new,0,0,$tag_white);
    //copy部分影象並調整
    imagecopyresized($new, $img,0, 0,0, 0,$n_w, $n_h, $width, $height);
//    imagecopymerge($new, $img, 0, 0, 0, 0, $n_w, $n_h, 100);//合併原圖和新生成的透明圖

    //刪除原圖片
    @unlink($image);

    //影象輸出新圖片、另存為
    if(!is_dir($date)){
        @mkdir($date,0777,true);
    }

    $imageUrl  = $date . md5 ($image) . '.jpg';
//    imagepng($new, $dest_path);

    imagejpeg ( $new,$image, 95 );
    imagedestroy($new);
    imagedestroy($img);
    return $imageUrl;
}


function generateImg($source,$waterImage, $text1, $text2, $text3, $font = '') {
    if(empty($font)){
        $font = ROOT_PATH."/static/common/lib/assets/fonts/simkai.ttf";
    }
    $date =  ROOT_PATH.'static/upload/watermark/' . date ( 'Ymd' ) . '/';
    $img = $date . md5 ( $source . $text1 . $text2 . $text3 ) . '.jpg';
    if (file_exists ($img)) {
        return $img;
    }

    $main = imagecreatefromjpeg ( $source );

    $width = imagesx ( $main );
    $height = imagesy ( $main );

    $target = imagecreatetruecolor ( $width, $height );

    $white = imagecolorallocate ( $target, 255, 255, 255 );
    imagefill ( $target, 0, 0, $white );

    imagecopyresampled ( $target, $main, 0, 0, 0, 0, $width, $height, $width, $height );

    $fontSize = 18;//磅值字型
    $fontColor = imagecolorallocate ( $target, 255, 0, 0 );//字的RGB顏色
    $fontBox = imagettfbbox($fontSize, 0, $font, $text1);//文字水平居中實質
    imagettftext ( $target, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), 190, $fontColor, $font, $text1 );

    $fontBox = imagettfbbox($fontSize, 0, $font, $text2);
    imagettftext ( $target, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), 370, $fontColor, $font, $text2 );

    $fontBox = imagettfbbox($fontSize, 0, $font, $text3);
    imagettftext ( $target, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), 560, $fontColor, $font, $text3 );

    //imageantialias($target, true);//抗鋸齒,有些PHP版本有問題,謹慎使用

    imagefilledpolygon ( $target, array (10 + 0, 0 + 142, 0, 12 + 142, 20 + 0, 12 + 142), 3, $fontColor );//畫三角形
    imageline($target, 100, 200, 20, 142, $fontColor);//畫線
    imagefilledrectangle ( $target, 50, 100, 250, 150, $fontColor );//畫矩形

    //bof of 合成圖片
//    $child1 = imagecreatefromjpeg ( $waterImage);
    $child1 = imagecreatefrompng( $waterImage);
    imagecopymerge ( $target, $child1, 0, 400, 0, 0, imagesx ( $child1 ), imagesy ( $child1 ), 100 );
    //eof of 合成圖片

    @mkdir ($date);
    imagejpeg ( $target,$img, 95 );

    imagedestroy ( $main );
    imagedestroy ( $target );
    imagedestroy ( $child1 );
    return $img;
}

/************************************************圖片水印功能結束************************************************************/