mostly filebased Content Presentation System
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cachedimage.php 2.3KB

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