图片上添加多行文字水印

 

 

/**
 * Text Watermark Point:
 *   #1      #2    #3
 *   #4      #5    #6
 *   #7      #8    #9
 */

/**
 * 给图片添加文字水印 可控制位置,旋转,多行文字    **有效字体未验证**
 * @param string $imgurl  图片地址
 * @param array $text   水印文字(多行以'|'分割)
 * @param int $fontSize 字体大小
 * @param type $color 字体颜色  如: 255,255,255
 * @param int $point 水印位置
 * @param type $font 字体
 * @param int $angle 旋转角度  允许值:  0-90   270-360 不含
 * @param string $newimgurl  新图片地址 默认使用后缀命名图片
 * @return boolean
 */
function createWordsWatermark($imgurl, $text, $fontSize = '14', $color = '0,0,0', $point = '1', $font = 'simhei.ttf', $angle = 0, $newimgurl = '') {

    $imageCreateFunArr = array('image/jpeg' => 'imagecreatefromjpeg', 'image/png' => 'imagecreatefrompng', 'image/gif' => 'imagecreatefromgif');
    $imageOutputFunArr = array('image/jpeg' => 'imagejpeg', 'image/png' => 'imagepng', 'image/gif' => 'imagegif');

//获取图片的mime类型
    $imgsize = getimagesize($imgurl);

    if (empty($imgsize)) {
        return false; //not image
    }

    $imgWidth = $imgsize[0];
    $imgHeight = $imgsize[1];
    $imgMime = $imgsize['mime'];

    if (!isset($imageCreateFunArr[$imgMime])) {
        return false; //do not have create img function
    }
    if (!isset($imageOutputFunArr[$imgMime])) {
        return false; //do not have output img function
    }

    $imageCreateFun = $imageCreateFunArr[$imgMime];
    $imageOutputFun = $imageOutputFunArr[$imgMime];

    $im = $imageCreateFun($imgurl);

    /*
     * 参数判断
     */
    $color = explode(',', $color);
    $text_color = imagecolorallocate($im, intval($color[0]), intval($color[1]), intval($color[2])); //文字水印颜色
    $point = intval($point) > 0 && intval($point) < 10 ? intval($point) : 1; //文字水印所在的位置
    $fontSize = intval($fontSize) > 0 ? intval($fontSize) : 14;
    $angle = ($angle >= 0 && $angle < 90 || $angle > 270 && $angle < 360) ? $angle : 0; //判断输入的angle值有效性
    $fontUrl = "C:\phpstudy_pro\WWW\lecent_prison_fund_sys\upload\simhei.ttf"; //有效字体未验证
    $text = explode('|', $text);
    $newimgurl = $newimgurl ? $newimgurl : $imgurl . '_WordsWatermark.jpg'; //新图片地址 统一图片后缀

    /**
     *  根据文字所在图片的位置方向,计算文字的坐标
     * 首先获取文字的宽,高, 写一行文字,超出图片后是不显示的
     */
    $textLength = count($text) - 1;
    $maxtext = 0;
    foreach ($text as $val) {
        $maxtext = strlen($val) > strlen($maxtext) ? $val : $maxtext;
    }

    $textSize = imagettfbbox($fontSize, 0, $fontUrl, $maxtext);
    $textWidth = $textSize[2] - $textSize[1]; //文字的最大宽度
    $textHeight = $textSize[1] - $textSize[7]; //文字的高度
    $lineHeight = $textHeight + 100; //文字的行高
//是否可以添加文字水印 只有图片的可以容纳文字水印时才添加
    if ($textWidth + 40 > $imgWidth || $lineHeight * $textLength + 40 > $imgHeight) {
        return false; //图片太小了,无法添加文字水印
    }

    if ($point == 1) { //左上角
        $porintLeft = 20;
        $pointTop = 20;
    } elseif ($point == 2) { //上中部
        $porintLeft = floor(($imgWidth - $textWidth) / 2);
        $pointTop = 20;
    } elseif ($point == 3) { //右上部
        $porintLeft = $imgWidth - $textWidth - 20;
        $pointTop = 20;
    } elseif ($point == 4) { //左中部
        $porintLeft = 20;
        $pointTop = floor(($imgHeight - $textLength * $lineHeight) / 2);
    } elseif ($point == 5) { //正中部
        $porintLeft = $imgWidth;
        $pointTop = floor(($imgHeight - $textLength * $lineHeight) / 2);
    } elseif ($point == 6) { //右中部
        $porintLeft = $imgWidth - $textWidth - 20;
        $pointTop = floor(($imgHeight - $textLength * $lineHeight) / 2);
    } elseif ($point == 7) { //左下部
        $porintLeft = 20;
        $pointTop = $imgHeight - $textLength * $lineHeight - 20;
    } elseif ($point == 8) { //中下部
        $porintLeft = floor(($imgWidth - $textWidth) / 2);
        $pointTop = $imgHeight - $textLength * $lineHeight - 20;
    } elseif ($point == 9) { //右下部
        $porintLeft = $imgWidth - $textWidth - 20;
        $pointTop = $imgHeight - $textLength * $lineHeight - 20;
    }

//如果有angle旋转角度,则重新设置 top ,left 坐标值
    if ($angle != 0) {
        if ($angle < 90) {
            $diffTop = ceil(sin($angle * M_PI / 180) * $textWidth);

            if (in_array($point, array(1, 2, 3))) {// 上部 top 值增加
                $pointTop += $diffTop;
            } elseif (in_array($point, array(4, 5, 6))) {// 中部 top 值根据图片总高判断
                if ($textWidth > ceil($imgHeight / 2)) {
                    $pointTop += ceil(($textWidth - $imgHeight / 2) / 2);
                }
            }
        } elseif ($angle > 270) {
            $diffTop = ceil(sin((360 - $angle) * M_PI / 180) * $textWidth);

            if (in_array($point, array(7, 8, 9))) {// 上部 top 值增加
                $pointTop -= $diffTop;
            } elseif (in_array($point, array(4, 5, 6))) {// 中部 top 值根据图片总高判断
                if ($textWidth > ceil($imgHeight / 2)) {
                    $pointTop = ceil(($imgHeight - $diffTop) / 2);
                }
            }
        }
    }



    foreach ($text as $key => $val) {
        imagettftext($im, $fontSize, 30, random_int(0,$porintLeft), ($pointTop + $key * $lineHeight), $text_color, $fontUrl, $val);
    }

// 输出图像
// JPEG图像生成的图像的质量的是一个范围从0(最低质量,最小的文件大小)到100(最高质量,最大文件大小)
// PNG生成图像的质量范围从0到9的
    $imageOutputFun($im, $newimgurl, 80);

// 释放内存
    imagedestroy($im);
    return $newimgurl;
}




//$imgurl, $text, $fontSize='14', $color='0,0,0', $point='1', $font = 'simhei.ttf', $angle=0, $newimgurl=''
$img = createWordsWatermark('../upload/bg.jpeg', '资金管理系统|资金管理系统|资金管理系统|资金管理系统|资金管理系统|资金管理系统', '14', '150,150,150', '5', '24', '30', '../upload/newpic.jpg');

$imfile = '../upload/newpic.jpg';//原图

$origim = imagecreatefromjpeg($imfile);//从 JPEG 文件或 URL 新建一图像

$w=imagesx($origim);//原图宽度

$h=imagesy($origim);//原图高度

$newimg = imagecreatetruecolor($w, $h);//返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像。imagecreatetruecolor//

$color=imagecolorallocatealpha($newimg,0,0,0,0);//为一幅图像分配颜色 + alpha; 和 imagecolorallocate() 相同,但多了一个额外的透明度参数 alpha,其值从 0 到 127。0 表示完全不透明,127 表示完全透明。

imagecolortransparent($newimg,$color);//将某个颜色定义为透明色

imagefill($newimg,0,0,$color);//区域填充;resource $image , int $x , int $y , int $color

imagecopy($origim,$newimg, 0,0, 0, 0,$w, $h);//拷贝图像的一部分;将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。

imagejpeg($origim, '../upload/newpic.jpg');//输出图象到浏览器或文件。;resource $image [, string $filename [, int $quality ]]

echo '<img src="' . $img . '" />';

 

posted @ 2022-07-18 14:48  程序员小艺  阅读(175)  评论(0)    收藏  举报