10
02月
2023
imagecopymerge(目标图片资源,水印图片资源,水印图片在目标图标上的X轴位置 ,水印图片在目标图标上的Y轴位置,水印图片右下角X轴位置 ,水印图片右下角Y轴位置,水印图片宽度 ,水印图片高度 ,透明度)
调用方法
/**
* @description: 图片打水印,合并图片
* @return {*}
* @author:
*/
public function watermark(){
$img1='./111.png';//目标图片
$img2='./222.png';//水印图片
$dst = imagecreatefrompng($img1);//获取目标图片资源
$src = imagecreatefrompng($img2);//获取水印图片资源
$dst_info = getimagesize($img1);//获取目标图片信息
$src_info = getimagesize($img2);//获取水印图片信息
$dst_x = $dst_info[0] - $src_info[0];//水印图片在目标图标上的X轴位置
$dst_y = $dst_info[1] - $src_info[1];//水印图片在目标图标上的Y轴位置
//要将图片加在右下脚
$src_x=0;//水印图片右下角X轴位置
$src_y=0;//水印图片右下角Y轴位置
$width=$src_info[0];//水印图片宽度
$height=$src_info[1];//水印图片高度
$transparency=50;//透明度
imagecopymerge($dst, $src, $dst_x, $dst_y, $src_x, $src_y,$width, $height, $transparency);
header('Content-type:image/png');
imagepng($dst,'./result.png');//将合成的图片保存为result.png
imagedestroy($dst);
imagedestroy($src);
}
* @description: 图片打水印,合并图片
* @return {*}
* @author:
*/
public function watermark(){
$img1='./111.png';//目标图片
$img2='./222.png';//水印图片
$dst = imagecreatefrompng($img1);//获取目标图片资源
$src = imagecreatefrompng($img2);//获取水印图片资源
$dst_info = getimagesize($img1);//获取目标图片信息
$src_info = getimagesize($img2);//获取水印图片信息
$dst_x = $dst_info[0] - $src_info[0];//水印图片在目标图标上的X轴位置
$dst_y = $dst_info[1] - $src_info[1];//水印图片在目标图标上的Y轴位置
//要将图片加在右下脚
$src_x=0;//水印图片右下角X轴位置
$src_y=0;//水印图片右下角Y轴位置
$width=$src_info[0];//水印图片宽度
$height=$src_info[1];//水印图片高度
$transparency=50;//透明度
imagecopymerge($dst, $src, $dst_x, $dst_y, $src_x, $src_y,$width, $height, $transparency);
header('Content-type:image/png');
imagepng($dst,'./result.png');//将合成的图片保存为result.png
imagedestroy($dst);
imagedestroy($src);
}
文本写入图像函数 imagefttext
imagefttext(目标图片资源,字体大小,角度 ,X轴位置,Y轴位置 ,字体颜色,文本内容 ,扩展信息)
调用方法
/**
* @description: 文字水印
* @return {*}
* @author:
*/
public function textwatermark(){
$img1='./111.png';//目标图片
$dst_info = getimagesize($img1);//获取目标图片信息
//判断图片格式
$typearr=['gif','jpeg','png'];
if(empty($typearr[$dst_info[2]-1])) exit('不支持的目标图片文件格式');
$dstfun='imagecreatefrom'.$typearr[$dst_info[2]-1];
$dst = $dstfun($img1);//获取目标图片资源
$font = realpath('./wryh.ttf');//字体文件,绝对路径
$black = imagecolorallocate($dst, 10, 10, 10);//字体颜色
$text='文本内容';
imagefttext($dst, 50, 0, 50, 50, $black, $font, $text);
header('Content-type:image/png');
imagepng($dst,'./result.png');//将合成的图片保存为result.png
imagedestroy($dst);
}
* @description: 文字水印
* @return {*}
* @author:
*/
public function textwatermark(){
$img1='./111.png';//目标图片
$dst_info = getimagesize($img1);//获取目标图片信息
//判断图片格式
$typearr=['gif','jpeg','png'];
if(empty($typearr[$dst_info[2]-1])) exit('不支持的目标图片文件格式');
$dstfun='imagecreatefrom'.$typearr[$dst_info[2]-1];
$dst = $dstfun($img1);//获取目标图片资源
$font = realpath('./wryh.ttf');//字体文件,绝对路径
$black = imagecolorallocate($dst, 10, 10, 10);//字体颜色
$text='文本内容';
imagefttext($dst, 50, 0, 50, 50, $black, $font, $text);
header('Content-type:image/png');
imagepng($dst,'./result.png');//将合成的图片保存为result.png
imagedestroy($dst);
}
非特殊说明,本文版权归 Code. 所有,转载请注明出处.
本文标题: php图片打水印
延伸阅读
使用phpqrcode生成二维码 php图片打水印 linux+rabbitmq+thinkphp最新|与我有关