Text watermark
Text watermark is to add text on the picture, mainly using the Imagefttext method of GD library, and need font file. The effect chart is as follows:
The implementation code is as follows:
Copy Code code as follows:
$dst _path = ' dst.jpg ';
Create an instance of a picture
$DST = imagecreatefromstring (file_get_contents ($DST _path));
Make a text
$font = './SIMSUN.TTC ';//font
$black = Imagecolorallocate ($DST, 0x00, 0x00, 0x00);//Font Color
Imagefttext ($DST, 0, $black, $font, ' happy programming ');
Output picture
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);
Picture watermark
Image watermark is to add a picture on another picture, mainly using the GD library imagecopy and Imagecopymerge. The effect chart is as follows:
The implementation code is as follows:
Copy Code code as follows:
$dst _path = ' dst.jpg ';
$src _path = ' src.jpg ';
Create an instance of a picture
$DST = imagecreatefromstring (file_get_contents ($DST _path));
$SRC = imagecreatefromstring (file_get_contents ($src _path));
Get the height of the watermark picture
List ($src _w, $src _h) = getimagesize ($src _path);
Copy the watermark picture to the target picture, and the last argument 50 is to set the transparency, where the translucent effect is achieved
Imagecopymerge ($DST, $SRC, ten, 0, 0, $src _w, $src _h, 50);
If the watermark picture itself has a transparent color, use the Imagecopy method
Imagecopy ($DST, $SRC, ten, 0, 0, $src _w, $src _h);
Output picture
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);