PHP development picture watermark tutorial operation picture
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); //內(nèi)存中創(chuàng)建一個(gè)和我們圖像類型一致的圖像 $fun = "imagecreatefrom{$type}"; //把要操作的圖片復(fù)制到內(nèi)存中 $image=$fun($src); /*操作圖片*/ //設(shè)置水印路徑 $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); //在內(nèi)存中創(chuàng)建一個(gè)和水印圖像一致的圖像類型 $fun2="imagecreatefrom{$type2}"; //把水印復(fù)制到內(nèi)存中 $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.