mostly filebased Content Presentation System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 line
1.4KB

  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. }