Posted By

Tamedo on 03/06/09


Tagged


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

Bluewall
jitendraweb
vali29
silentbob84
aleksanderek


php dynamic image watermark


 / Published in: PHP
 

Usage: watermark.php?path=imagePath.ext&watermark=watermarkPath.gif or watermark.php?path=imagePath.ext&watermark=watermarkPath.png

  1. <?php
  2. $imagesource = $_GET['path'];
  3. $watermarkPath = $_GET['watermark'];
  4. $filetype = substr($imagesource,strlen($imagesource)-4,4);
  5. $filetype = strtolower($filetype);
  6. $watermarkType = substr($watermarkPath,strlen($watermarkPath)-4,4);
  7. $watermarkType = strtolower($watermarkType);
  8.  
  9. if($filetype == ".gif")
  10. $image = @imagecreatefromgif($imagesource);
  11. else
  12. if($filetype == ".jpg" || $filetype == "jpeg")
  13. $image = @imagecreatefromjpeg($imagesource);
  14. else
  15. if($filetype == ".png")
  16. $image = @imagecreatefrompng($imagesource);
  17. else
  18. die();
  19.  
  20. if(!$image)
  21. die();
  22.  
  23. if($watermarkType == ".gif")
  24. $watermark = @imagecreatefromgif($watermarkPath);
  25. else
  26. if($watermarkType == ".png")
  27. $watermark = @imagecreatefrompng($watermarkPath);
  28. else
  29. die();
  30.  
  31. if(!$watermark)
  32. die();
  33.  
  34. $imagewidth = imagesx($image);
  35. $imageheight = imagesy($image);
  36. $watermarkwidth = imagesx($watermark);
  37. $watermarkheight = imagesy($watermark);
  38. $startwidth = (($imagewidth - $watermarkwidth)/2);
  39. $startheight = (($imageheight - $watermarkheight)/2);
  40. imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
  41. imagejpeg($image);
  42. imagedestroy($image);
  43. imagedestroy($watermark);
  44. ?>

Report this snippet  

You need to login to post a comment.