img.PHP
img.PHP
php$img = '1.jpg';//获取图片信息$info = getimagesize($img);//获取图片类型$type = image_type_to_extension($info['2'],false);//在内容中创建一个和图片一模一样的图片$ext = "imagecreatefrom{$type}";//图片复制到内存中$image = $ext($img);$con = '笑鸽';$color = imagecolorallocatealpha($image,100,25,50,0);imagettftext($image,50,0,20,600,$color,'./GB2312.ttf',$con);header("content-type:" . $info['mime']);$func = "image{$type}";$func($image);
imagecolorallocatealpha函数用来设置图像透明度。
imagettftext函数表示使用想要的某类型字体,将指定的文字写入图像。
效果图:
字体:gb2312.zip
上传图片添加文字水印(图片上传之前添加水印)
public function waterMap($target){ //$target为图片地址 //添加水印 $im = imagecreatefromjpeg($target); //需要先开启gb库,才会有作用;关闭状态下程序进行到这里会结束,不会抛出错误也不会有任何返回值; if($im) { $textColor = imagecolorallocate($im, 111, 24, 110);//设定字体颜色 $font = ROOT_PATH.'includes/font/GB2312.ttf'; $text='ASianWish亚美汇国际'; imagettftext($im,36,-45,10,100,$textColor,$font,$text);//用 TrueType 字体向图像写入文本,可以输出中文 imagejpeg($im,$target);//生成jpg各式图片 imagedestroy($im);//释放与 image 关联的内存 }else{ echo '图片错误'; }}
imagettftext ( $image , $size , $angle , $x , $y , $color , $fontfile , $text );
上传图片添加图片水印
public function imgWaterMap($source){ $dst_path = $source;//目标图片 $src_path = ROOT_PATH."images/watermark.png";//水印图片 //创建图片的实例 $dst = imagecreatefromstring(file_get_contents($dst_path)); $src = imagecreatefromstring(file_get_contents($src_path)); //获取图片的宽高 list($dst_w, $dst_h) = getimagesize($dst_path); //获取水印图片的宽高 list($src_w, $src_h) = getimagesize($src_path); //将水印图片复制到目标图片中间位置上,最后个参数是设置透明度 imagecopymerge($dst, $src, ($dst_w/2-$src_w/2), ($dst_h/2-$src_h/2), 0, 0, $src_w, $src_h, 30); //如果水印图片本身带透明色,则使用imagecopy方法 //imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h); //输出图片 list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path); switch ($dst_type) { case 1://GIF header('Content-Type: image/gif'); //输出$dst到$target位置 imagegif($dst,$target); break; case 2://JPG header('Content-Type: image/jpeg'); imagejpeg($dst,$target); break; case 3://PNG header('Content-Type: image/png'); imagepng($dst,$target); break; default: break; } //释放内存 imagedestroy($dst); imagedestroy($src);}
imagecopymerge() 函数用于拷贝并合并图像的一部分,成功返回 TRUE ,否则返回 FALSE 。
来源地址:https://blog.csdn.net/weixin_42124384/article/details/127861375
--结束END--
本文标题: php-上传图片加水印(文字水印图片水印)
本文链接: https://www.lsjlt.com/news/410439.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0