Home >Backend Development >PHP Tutorial >PHP text watermark and PHP image watermark implementation code (two watermarking methods)_PHP tutorial

PHP text watermark and PHP image watermark implementation code (two watermarking methods)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:18:271206browse

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:

PHP text watermark and PHP image watermark implementation code (two watermarking methods)_PHP tutorial

The implementation code is as follows:

Copy code The code is as follows:

$dst_path = 'dst.jpg';

//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:

PHP text watermark and PHP image watermark implementation code (two watermarking methods)_PHP tutorial

The implementation code is as follows:

Copy code The code is as follows:

$dst_path = 'dst.jpg';
$src_path = 'src. jpg';

//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);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/621661.htmlTechArticleText watermark Text watermark is to add text to the picture. It mainly uses the imagefttext method of the gd library and requires a font file. . The effect diagram is as follows: The implementation code is as follows: Copy code code...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn