PHP image watermarker with file system cache


/ Published in: PHP
Save to your folder(s)

Image Watermarker with file system cache


Copy this code and paste it in your HTML
  1. <?php
  2. //ini_set('max_execution_time', 300);
  3. header('content-type: image/jpeg');
  4. header('Cache-control: max-age='.(60*60*24*365));
  5. header('Expires: '.gmdate(DATE_RFC1123,time()+60*60*24*365));
  6. $cache_file = "./img/". hash('md5', $_GET['src']).".jpg";
  7.  
  8. if (file_exists($cache_file)) { //CACHE HIT
  9. header('Last-Modified: '.gmdate(DATE_RFC1123,filemtime($cache_file)));
  10. echo(file_get_contents($cache_file));
  11.  
  12. } else { //CACHE MISS
  13.  
  14. $watermark = imagecreatefrompng("watermarkd.png");
  15. $watermark_width = imagesx($watermark);
  16. $watermark_height = imagesy($watermark);
  17. $image = imagecreatetruecolor($watermark_width, $watermark_height);
  18. $image = imagecreatefromjpeg($_GET['src']);
  19. $size = getimagesize($_GET['src']);
  20. $dest_x = $size[0] - $watermark_width - 5;
  21. $dest_y = $size[1] - $watermark_height - 5;
  22. imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
  23. imageinterlace($image, true);
  24. imagejpeg($image,$cache_file,100);
  25. echo(file_get_contents($cache_file));
  26. imagedestroy($watermark);
  27. imagedestroy($image);
  28. }
  29. exit();
  30. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.