mostly filebased Content Presentation System

cachedimage.php 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace Modules;
  3. class CachedImage {
  4. public $original;
  5. public $cache_dir = "tmp/";
  6. public $default_width = 500;
  7. function __construct($path) {
  8. $this->original = $path;
  9. }
  10. function get_src($inwidth = 500) {
  11. $f3 = \Base::instance();
  12. if($this->is_image($this->original) && TRUE) {
  13. $info = pathinfo($this->original);
  14. $fn = basename($this->original,'.'.$info['extension']);
  15. $width = $inwidth ? $inwidth : $this->default_width;
  16. $name = md5($this->original.$width);
  17. $name = sprintf("%s_%s.jpg",$fn,$name);
  18. $out = $this->cache_dir.$name;
  19. //if(!file_exists($this->original)) { $this->original='rsc/img/default.png'; }
  20. if(!file_exists($out)) {
  21. $img1 = new \Image($this->original);
  22. $img1->resize($width);
  23. $f3->write($out,$img1->dump('jpeg',75));
  24. unset($img1);
  25. }
  26. } else {
  27. $out = $this->original;
  28. }
  29. return "/".$out;
  30. }
  31. function is_image($path) {
  32. // this protects from accidently calling this class on a wrong file
  33. // it doesn't ensure that the file actually contains valid image data
  34. $ex = explode('.',$path);
  35. $ext = array_pop($ex);
  36. return in_array(strtolower($ext),array( 'jpg', 'jpeg', 'png' ));
  37. }
  38. }