|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- <?php
-
- namespace Modules;
-
- class CachedImage {
-
- public $original;
- public $cache_dir = "tmp/";
- public $default_width = 500;
-
- function __construct($path) {
- $this->original = $path;
- }
-
- function get_src($inwidth = 500) {
- $f3 = \Base::instance();
- if($this->is_image($this->original) && TRUE) {
- $info = pathinfo($this->original);
- $fn = basename($this->original,'.'.$info['extension']);
- $width = $inwidth ? $inwidth : $this->default_width;
- $name = md5($this->original.$width);
-
- $name = sprintf("%s_%s.jpg",$fn,$name);
-
- $out = $this->cache_dir.$name;
-
-
-
- if(!file_exists($out)) {
- $img1 = new \Image($this->original);
- $img1->resize($width);
- $f3->write($out,$img1->dump('jpeg',75));
- unset($img1);
- }
- } else {
- $out = $this->original;
- }
- return "/".$out;
- }
-
- function is_image($path) {
-
-
- $ex = explode('.',$path);
- $ext = array_pop($ex);
- return in_array(strtolower($ext),array( 'jpg', 'jpeg', 'png' ));
- }
- }
|