php 生成水印图片左下角(替换掉原图片)

生成水印图片  content水印多行需要换行的文字(必须靠边写换行) 另外水印位置 函数内部可修改

【图片地址】

$imagePath = "/static/images/123.jpg";

【水印内容,换行前不可以有空格 贴着边边写!!!】

$content =

'2020年12月12号

126.2252,52.2221

河北省石家庄市友谊南大街18号

';


public function SCwaterYin($src,$content)

{

            $imagePath = $_SERVER["DOCUMENT_ROOT"] .$src;

           putenv('GDFONTPATH=' . realpath('.'));

           //获取文件类型

           $imageInfo = getimagesize($imagePath);

           $imageExtension = image_type_to_extension($imageInfo[2], false);

           //获取图片

           $func = 'imagecreatefrom' . $imageExtension;

           $image = $func($imagePath);

           //水印字体

           $font = './msyh.ttf';

           //水印颜色

           $color = imagecolorallocatealpha($image, 255, 255, 255, 30);

           //添加水印

           //获取图片维度

           if (!empty($imagePath) && file_exists($imagePath)) {

                      $ground_info = getimagesize($imagePath);

                      $ground_w = $ground_info[0]; //取得背景图片的宽

                      $ground_h = $ground_info[1]; //取得背景图片的高

                      switch ($ground_info[2]) {//取得背景图片的格式

                                 case 1:$ground_im = imagecreatefromgif($imagePath);

                                break;

                                 case 2:$ground_im = imagecreatefromjpeg($imagePath);

                                 break;

                                 case 3:$ground_im = imagecreatefrompng($imagePath);

                                 break;

                                 default:die($formatMsg);

                      }

           } else {

                      die("需要加水印的图片不存在!");

           }

           //水印位置

           putenv('GDFONTPATH=' . realpath('.'));

           $temp = imagettfbbox(ceil(5 * 5), 0, "./msyh.ttf", $content); //取得使用 TrueType 字体的文本的范围

           $w = $temp[2] - $temp[6];

           $h = $temp[3] - $temp[7];

           unset($temp);

           $posY = $ground_h - $h + 50;

           imagettftext($image, 15, 0, 10, $posY, $color, $font, $content);

           imagejpeg($image, '.'.$src, 95); //输出到目标文件

           imagedestroy($image); //销毁内存数据流

           echo "生成成功!";exit;

}


使用:

$imagePath = "/static/images/123.jpg";

$content =

'2020年12月12号

126.2252,52.2221

河北省石家庄市友谊南大街18号

';

$this->SCwaterYin($imagePath,$content);


你可能感兴趣的:(php 生成水印图片左下角(替换掉原图片))