Snippets - GD

Snippets index page

Watermark an image - Anthony 14/6/2008

Watermark an image; if you want to simply add text to an image check out the link below.

  1. <?php 
  2. // Create the canvas
  3. $canvas imagecreate200100 ); 
  4. // Set the colours to use
  5. $black imagecolorallocate$canvas00); 
  6. $white imagecolorallocate$canvas255255255 );
  7. // Create a rectangle 200 x 100 and fill it white 
  8. imagefilledrectangle$canvas00200100$white ); 
  9. // Set the path to the font to use
  10. $font "../fonts/verdana.ttf"
  11. // Set the text to use
  12. $text "Sunflower"
  13. // Set the font size
  14. $size "30"
  15. // Set the angle to rotate the font
  16. // In this case the font was not rotated - I must need savin to a tempory image and rotating 
  17. $degrees "30"
  18. // Set the image to watermark
  19. $photo"../original_images/sunflower.jpg";
  20. // Find teh size of a bounding box for the text
  21. // If php has not been setup with the correct ttf support this will not work
  22. $box imagettfbbox$size0$font$text ); 
  23. // Find the value to use for the position in x - placing central on the image
  24. $x = (200 - ($box[2] - $box[0])) / 2
  25. // Setup the position for the y
  26. $y = (100 - ($box[1] - $box[7])) / 2
  27. $y -= $box[7]; 
  28. // Create the box with the text
  29. imageTTFText$canvas$size0$x$y$black$font$text ); 
  30. // Change the white to transparent
  31. imagecolortransparent $canvas$white ); 
  32. // Rotate the text - didn't work
  33. imagerotate$canvas$degrees0); 
  34. // Save the tempory image
  35. imagepng$canvas"temp.png" ); 
  36. // Clear the image from the memory
  37. ImageDestroy$canvas ); 
  38. // Start a new image
  39. $watermark imagecreatefrompng('temp.png');   
  40. // Setup the image width and height
  41. $watermark_width imagesx$watermark );   
  42. $watermark_height imagesy$watermark );   
  43. $image imagecreatetruecolor$watermark_width$watermark_height );   
  44. $image imagecreatefromjpeg$photo ); 
  45. // Get the size of the photo to be watermarked  
  46. $size getimagesize$photo );   
  47. // Calculate the position of the watermark
  48. $dest_x $size[0] - $watermark_width 100;   
  49. $dest_y $size[1] - $watermark_height 200;   
  50. // Put the watermark onto the photo
  51. imagecopymerge$image$watermark$dest_x$dest_y00$watermark_width$watermark_height50 );
  52. // Save the watermarked photo   
  53. imagejpeg$image'watermark_GD.jpg' );  
  54. // Clear the images from the memory
  55. imagedestroy$image );   
  56. imagedestroy$watermark );   
  57. // Delete the tempory image
  58. unlink'temp.png'); 
  59. ?>  
http://www.rubblewebs.co.uk/imagemagick/snippets/GD_snippets_f_1.html

For more examples and information check out the main Imagemagick section.

Change a png to a jpg View

Create some text as an image View

Crop an image View

Display image without saving View

Grayscale image View

Resize an image View

Rotate an image View

Watermark an image View