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

/************************************************图片水印功能结束************************************************************/

 

posted @ 2021-07-17 14:10  MargoHu  阅读(795)  评论(3编辑  收藏  举报