-
- $dst_path = 'dst.jpg';
- //Create an instance of the image
- $dst = imagecreatefromstring(file_get_contents($dst_path));
- //Place 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);
Copy code
Example 2, php image watermark
Image watermark, add one image to another image, mainly use the imagecopy and imagecopymerge of the gd library.
Rendering:
Code:
-
- $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 Set transparency, here to achieve translucent effect
- 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 image
- 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);
Copy code
|