Text watermark
Text watermark is to add text to the image. It mainly uses the imagefttext method of the gd library and requires a font file. The rendering is as follows:
The implementation code is as follows:
//Create image instance
$dst = imagecreatefromstring(file_get_contents($dst_path));
//Type text
$font = './simsun.ttc';//Font
$black = imagecolorallocate($dst, 0x00, 0x00, 0x00);//Font color
imagefttext ($dst, 13, 0, 20, 20, $black, $font, 'Happy Programming');
//Output images
list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
switch ($dst_type) {
case 1://GIF
header ('Content-Type: image/gif');
imagegif($dst);
break;
case 2://JPG
header('Content-Type: image/jpeg') ;
imagejpeg($dst);
break;
case 3://PNG
header('Content-Type: image/png');
imagepng($dst);
break;
default:
break;
}
imagedestroy($dst);
Image watermark
Image watermarking is to add one image to another image, mainly using the imagecopy and imagecopymerge of the gd library. The rendering is as follows:
The implementation code is as follows:
//Instance of creating an image
$dst = imagecreatefromstring(file_get_contents($dst_path));
$src = imagecreatefromstring(file_get_contents($src_path));
//Get the width and height of the watermark image
list($src_w, $src_h) = getimagesize($src_path);
//Copy the watermark image to the target image. The last parameter 50 is to set the transparency. Here, the translucent effect is achieved
imagecopymerge($dst, $src, 10, 10, 0, 0, $src_w, $ src_h, 50);
//If the watermark image itself has a transparent color, use the imagecopy method
//imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h);
//Output images
list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
switch ($dst_type) {
case 1://GIF
header ('Content-Type: image/gif');
imagegif($dst);
break;
case 2://JPG
header('Content-Type: image/jpeg') ;
imagejpeg($dst);
break;
case 3://PNG
header('Content-Type: image/png');
imagepng($dst);
break;
default:
break;
}
imagedestroy($dst);
imagedestroy($src);