mostly filebased Content Presentation System
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

cachedimage.php 1.9KB

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