Operation pictures
<?php /*打开图片*/ //配置图片路径 $src = "https://img.php.cn/upload/course/000/000/004/581454f755fb1195.jpg"; //获取图片的基本信息 $info=getimagesize($src); //通过图像的编号来获取图片的类型 $type=image_type_to_extension($info[2],false); //内存中创建一个和我们图像类型一致的图像 $fun = "imagecreatefrom{$type}"; //把要操作的图片复制到内存中 $image=$fun($src); /*操作图片*/ //设置水印路径 $image_Mark = "https://img.php.cn/upload/course/000/000/004/5814594e3e7c9278.png"; //获取水印的基本信息 $info2=getimagesize($image_Mark); //通过水印的图像编号来获取水印的图片类型 $type2=image_type_to_extension($info2[2],false); //在内存中创建一个和水印图像一致的图像类型 $fun2="imagecreatefrom{$type2}"; //把水印复制到内存中 $water = $fun2($image_Mark); //合并图片 imagecopymerge($image,$water,60,40,0,0,$info2[0],$info2[1],30); //销毁水印图片 imagedestroy($water); ?>
Code explanation:
getimagesize — Get the image size
image_type_to_extension-Returns the suffix name.
Then store it in the memory, use the imagecopymerge function to merge the pictures and add watermarks
imagecopymerge — copy and merge part of the image
bool imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )
Start the coordinates in the src_im image from src_x, src_y , the width is src_w and the height is src_h and a part of it is copied to The coordinates in the dst_im image are dst_x and at the position of dst_y. The two images will be merged based on pct, which ranges from 0 to 100. When pct = 0, it actually does nothing, when it is 100 For paletted images, this function is exactly the same as imagecopy(), which implements alpha transparency for truecolor images.
Next Section