Home  >  Article  >  Backend Development  >  PHP text watermark and PHP image watermark code examples

PHP text watermark and PHP image watermark code examples

WBOY
WBOYOriginal
2016-07-25 08:51:51965browse
  1. $dst_path = 'dst.jpg';
  2. //Create an instance of the image
  3. $dst = imagecreatefromstring(file_get_contents($dst_path));
  4. //Place text
  5. $font = './simsun.ttc' ;//Font
  6. $black = imagecolorallocate($dst, 0x00, 0x00, 0x00);//Font color
  7. imagefttext($dst, 13, 0, 20, 20, $black, $font, 'Happy Programming');
  8. //Output images
  9. list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
  10. switch ($dst_type) {
  11. case 1://GIF
  12. header('Content-Type: image/gif' );
  13. imagegif($dst);
  14. break;
  15. case 2://JPG
  16. header('Content-Type: image/jpeg');
  17. imagejpeg($dst);
  18. break;
  19. case 3://PNG
  20. header('Content-Type: image/png');
  21. imagepng($dst);
  22. break;
  23. default:
  24. break;
  25. }
  26. 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: PHP text watermark and PHP image watermark code examples

Code:

  1. $dst_path = 'dst.jpg';
  2. $src_path = 'src.jpg';
  3. //Instance of creating an image
  4. $dst = imagecreatefromstring(file_get_contents($dst_path));
  5. $src = imagecreatefromstring (file_get_contents($src_path));
  6. //Get the width and height of the watermark image
  7. list($src_w, $src_h) = getimagesize($src_path);
  8. //Copy the watermark image to the target image, the last parameter 50 is Set transparency, here to achieve translucent effect
  9. imagecopymerge($dst, $src, 10, 10, 0, 0, $src_w, $src_h, 50);
  10. //If the watermark image itself has a transparent color, use the imagecopy method
  11. //imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h);
  12. //Output image
  13. list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
  14. switch ($dst_type) {
  15. case 1://GIF
  16. header('Content-Type: image/gif');
  17. imagegif($dst);
  18. break;
  19. case 2://JPG
  20. header('Content- Type: image/jpeg');
  21. imagejpeg($dst);
  22. break;
  23. case 3://PNG
  24. header('Content-Type: image/png');
  25. imagepng($dst);
  26. break;
  27. default:
  28. break;
  29. }
  30. imagedestroy($dst);
  31. imagedestroy($src);
Copy 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