Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 1.14 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Adding watermark to image in PHP
  2. <img src="watermark1.php?image=photo.jpg>
  3.        
  4. <?php
  5. // this tells the browser to render jpg image
  6. header('content-type: image/jpeg');
  7.  
  8. // getting the image name from GET variable
  9. $image = $_GET['image'];
  10.  
  11. // creating png image of watermark
  12. $watermark = imagecreatefrompng('watermark.png');  
  13.  
  14. // getting dimensions of watermark image
  15. $watermark_width = imagesx($watermark);
  16. $watermark_height = imagesy($watermark);  
  17.  
  18. // creating jpg from original image
  19. $image_path =  $image;
  20. $image = imagecreatefromjpeg($image_path);
  21. //something went wrong
  22. if ($image === false) {
  23.     return false;
  24. }
  25. // getting the dimensions of original image
  26. $size = getimagesize($image_path);
  27. // placing the watermark 5px from bottom and right
  28. $dest_x = $size[0] - $watermark_width - 5;
  29. $dest_y = $size[1] - $watermark_height - 5;
  30. // blending the images together
  31. imagealphablending($image, true);
  32. imagealphablending($watermark, true);
  33. // creating the new image
  34. imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
  35. imagejpeg($image);
  36. // destroying and freeing memory
  37. imagedestroy($image);
  38. imagedestroy($watermark);
  39. ?>