PHP-watermark processing

1. Open the GIF, JPEG, PNG, WBMP format images existing in the server or network

  1. imagecreatefromjpeg()
  2. imagecreatefrompng()
  3. imagecreatefromgif()
  4. imagecreatefrombwmp()
<?php
header('content-type:image/jpeg');
$img= imgecreatefromjieg('images/zcx.jpg);		//打开图片,格式为'路径/全名',也可以放一张图片的URL地址

$color=imagecolorallocate($img,255,255,255);
imagettftext($img,25,0,100,100,$color,'font/sketchycomic.ttf','sifangku.com');
//设置字体的格式,大小,倾斜度,和颜色

imagejpef($img);			//输出图片
imagedestroy();

imagesx() output image width,
imagesy() output image height
getimagesize() get image size (only the path of the image can be passed in)
imagettfbbox() returns an array containing 8 units, representing the four corners of the text frame
Insert picture description here

<?php
header('content-type:image/jpeg');
$img= imgecreatefromjieg('images/zcx.jpg);	
$color=imagecolorallocate($img,255,255,255);

//var_dump(imagesx($img));			输出图像宽度,
//var_dump(imagesy($img));			输出图像高度
//var_dump(getimagesize('images/zcx.jpg);			直接输出图像的相关信息

$width=imagesx($img);
$height=imagesy($img);
imagettfbbox(20,0,'font/sketchycomic.ttf','sifangku.com');
$stringwidth=$position[2]-position[0];

imagettftext($img,25,0,$width-1-$stringwidth-($width/30),$height-1-($height/30),$color,'font/sketchycomic.ttf','sifangku.com');
imagejpef($img);		
imagedestroy();

Add picture watermark
imagecopy(); You can copy part of the picture to another picture
Parameter description

<?php
header('content-type:image/jpeg');
$img= imgecreatefromjieg('images/zcx.jpg);	
$color=imagecolorallocate($img,255,255,255);

$watermark = imagecreatefromgif($filename);

$width=imagesx($img);
$height=imagesy($img);
imagettfbbox(20,0,'font/sketchycomic.ttf','sifangku.com');
$stringwidth=$position[2]-position[0];
 
imagettftext($img,25,0,$width-1-$stringwidth-($width/30),$height-1-($height/30),$color,'font/sketchycomic.ttf','sifangku.com');

$watermark _width =imagesx($img);
$watermark _height=imagesy($img);

imagecopy($img,$watermark,100,100,0,0,$watermark _width,$watermark _height);

/*
imagecopy的参数说明:
$img :	目标图像资源
$watermark  :	水印的图像资源 
100,100 :水印图像在目标图像上,所处的X轴和Y轴的坐标(左上角坐标)
0,0	:水印图像从X坐标为0处,开始拷贝 ,从Y坐标为0处,开始拷贝
$watermark _width :所要拷贝水印图像的长度
$watermark _height :所要拷贝水印图像的高度
*/

imagejpef($img);		
imagedestroy();

If you want to put the watermark in the lower right corner

imagecopy($img,$watermark,$width-1-$watermark _width,$height-1-$watermark _height,0,0,$watermark _width,$watermark _height);

Add a transparent watermark

imagecopymerge($img,$watermark,$width-1-$watermark _width,$height-1-$watermark _height,0,0,$watermark _width,$watermark _height,50);
//最后一个参数50 ,表示透明度 ,参数越小越透明。0位全透,看不见。100为几乎不透明,和imagecopy函数一模一样

Guess you like

Origin blog.csdn.net/qq_41076531/article/details/100084060