From a2d49a1ce5d40b29772ff3659d59273324f7481c Mon Sep 17 00:00:00 2001 From: Cameron Date: Wed, 25 Oct 2017 15:17:53 -0700 Subject: [PATCH] Experimental Image-Caching performance enhancement. --- e107_handlers/cache_handler.php | 2 +- e107_handlers/e107_class.php | 21 ++++++++++- e107_handlers/e_parse_class.php | 62 ++++++++++++++++++++++++++++++--- 3 files changed, 79 insertions(+), 6 deletions(-) diff --git a/e107_handlers/cache_handler.php b/e107_handlers/cache_handler.php index 4c2ad3482..b19696e75 100644 --- a/e107_handlers/cache_handler.php +++ b/e107_handlers/cache_handler.php @@ -344,7 +344,7 @@ class ecache { if($type === 'image') { $path = e_CACHE_IMAGE; - $mask = ($mask == null) ? '.*\.cache\.bin' : $mask; + $mask = ($mask == null) ? '.*(\.cache\.bin|\.jpg|\.jpeg|\.png|\.gif)' : $mask; } if($type === 'js') diff --git a/e107_handlers/e107_class.php b/e107_handlers/e107_class.php index b155ae2fc..2eb7f7c30 100644 --- a/e107_handlers/e107_class.php +++ b/e107_handlers/e107_class.php @@ -416,6 +416,11 @@ class e107 mkdir(e_SYSTEM, 0755); } + if(!is_dir(e_CACHE_IMAGE)) + { + mkdir(e_CACHE_IMAGE, 0755); + } + // Prepare essential directories. $this->prepareDirs(); } @@ -571,7 +576,16 @@ class e107 $ret['CACHE_DIRECTORY'] = $ret['SYSTEM_DIRECTORY'].'cache/'; $ret['CACHE_CONTENT_DIRECTORY'] = $ret['CACHE_DIRECTORY'].'content/'; - $ret['CACHE_IMAGE_DIRECTORY'] = $ret['CACHE_DIRECTORY'].'images/'; //TODO Change to MEDIA Directory. + + if(defined('e_MEDIA_STATIC')) // experimental - subject to change. + { + $ret['CACHE_IMAGE_DIRECTORY'] = $ret['MEDIA_IMAGES_DIRECTORY'].'cache/'; + } + else + { + $ret['CACHE_IMAGE_DIRECTORY'] = $ret['CACHE_DIRECTORY'].'images/'; + } + $ret['CACHE_DB_DIRECTORY'] = $ret['CACHE_DIRECTORY'].'db/'; $ret['CACHE_URL_DIRECTORY'] = $ret['CACHE_DIRECTORY'].'url/'; @@ -4212,6 +4226,11 @@ class e107 define('e_AVATAR_ABS', $this->get_override_http('AVATARS')); define('e_AVATAR_UPLOAD_ABS', $this->get_override_http('AVATARS_UPLOAD')); define('e_AVATAR_DEFAULT_ABS', $this->get_override_http('AVATARS_DEFAULT')); + + if(defined('e_MEDIA_STATIC')) // experimental - subject to change. + { + define('e_CACHE_IMAGE_ABS', $this->get_override_http('CACHE_IMAGE')); + } // Special diff --git a/e107_handlers/e_parse_class.php b/e107_handlers/e_parse_class.php index 88d4c7803..0e937b8c7 100644 --- a/e107_handlers/e_parse_class.php +++ b/e107_handlers/e_parse_class.php @@ -2493,19 +2493,47 @@ class e_parse extends e_parser * @param array $options * @return null|string */ - public function thumbCacheFile($path, $options=array()) + public function thumbCacheFile($path, $options=null, $log=null) { if(empty($path)) { return null; } + if(is_string($options)) + { + parse_str($options,$options); + } + + $path = str_replace($this->getUrlConstants('raw'), $this->getUrlConstants('sc'), $path); + $path = $this->replaceConstants(str_replace('..', '', $path)); + $filename = basename($path); $tmp = explode('.',$filename); $ext = end($tmp); $len = strlen($ext) + 1; $start = substr($filename,0,- $len); + + // cleanup. + $newOpts = array( + 'w' => (string) intval($options['w']), + 'h' => (string) intval($options['h']), + 'aw' => (string) intval($options['aw']), + 'ah' => (string) intval($options['ah']), + 'c' => strtoupper(vartrue($options['c'],'0')) + ); + + if($log !== null) + { + file_put_contents(e_LOG.$log, "\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n", FILE_APPEND); + $message = $path."\n".print_r($newOpts,true)."\n\n\n"; + file_put_contents(e_LOG.$log, $message, FILE_APPEND); + + // file_put_contents(e_LOG.$log, "\t\tFOUND!!\n\n\n", FILE_APPEND); + } + + if(!empty($options['aw'])) { $options['w'] = $options['aw']; @@ -2516,16 +2544,23 @@ class e_parse extends e_parser $options['h'] = $options['ah']; } + $size = varset($options['w'],0).'x'.varset($options['h'],0); $thumbQuality = e107::getPref('thumbnail_quality',65); - $cache_str = md5(serialize($options).$path. $thumbQuality); + $cache_str = md5(serialize($newOpts).$path. $thumbQuality); - // TODO Remove these. $pre = 'thumb_'; $post = '.cache.bin'; - // $post = ''; + + // $cache_str = http_build_query($newOpts,null,'_'); // testing files. + + if(defined('e_MEDIA_STATIC')) // experimental - subject to change. + { + $pre = ''; + $post = ''; + } $fname = $pre.strtolower($start.'_'.$cache_str.'_'.$size.'.'.$ext).$post; @@ -2635,6 +2670,23 @@ class e_parse extends e_parser } + if(defined('e_MEDIA_STATIC')) // experimental - subject to change. + { + $opts = str_replace('&', '&', $thurl); + + $staticFile = $this->thumbCacheFile($url, $opts); + + if(!empty($staticFile) && is_readable(e_CACHE_IMAGE.$staticFile)) + { + //file_put_contents(e_LOG."thumbCache.log",$staticFile."\n",FILE_APPEND); + return e_CACHE_IMAGE_ABS.$staticFile; + } + + $options['nosef'] = true; + // file_put_contents(e_LOG."thumb.log", "\n++++++++++++++++++++++++++++++++++\n\n", FILE_APPEND); + } + + if(e_MOD_REWRITE_MEDIA == true && empty($options['nosef']) )// Experimental SEF URL support. { $options['full'] = $full; @@ -2657,6 +2709,8 @@ class e_parse extends e_parser } + + /** * Experimental: Generate a Thumb URL for use in the img srcset attribute. * @param string $src eg. {e_MEDIA_IMAGE}myimage.jpg