diff --git a/e107_handlers/e_parse_class.php b/e107_handlers/e_parse_class.php index 0d2a5cbb7..1d6e611b7 100644 --- a/e107_handlers/e_parse_class.php +++ b/e107_handlers/e_parse_class.php @@ -36,17 +36,17 @@ class e_parse extends e_parser protected $e_bb; // Profanity filter - protected $e_pf; + public $e_pf; // Emote filter - protected $e_emote; + public $e_emote; // 'Hooked' parsers (array) protected $e_hook; - protected $search = array(''', ''', ''', '"', 'onerror', '>', '"', ' & '); + public $search = array(''', ''', ''', '"', 'onerror', '>', '"', ' & '); - protected $replace = array("'", "'", "'", '"', 'onerror', '>', '"', ' & '); + public $replace = array("'", "'", "'", '"', 'onerror', '>', '"', ' & '); // Set to TRUE or FALSE once it has been calculated protected $e_highlighting; @@ -2609,6 +2609,7 @@ class e_parse extends e_parser * Generated a Thumb Cache File Name from path and options. * @param string $path * @param array $options + * @param string $log (optional) - log file name * @return null|string */ public function thumbCacheFile($path, $options=null, $log=null) @@ -2630,8 +2631,7 @@ class e_parse extends e_parser $tmp = explode('.',$filename); $ext = end($tmp); $len = strlen($ext) + 1; - $start = substr($filename,0,- $len); - + $start = substr($filename,0, - $len); // cleanup. $newOpts = array( @@ -2639,16 +2639,12 @@ class e_parse extends e_parser 'h' => (string) intval($options['h']), 'aw' => (string) intval($options['aw']), 'ah' => (string) intval($options['ah']), - 'c' => strtoupper(vartrue($options['c'],'0')) + 'c' => strtoupper(vartrue($options['c'],'0')), ); - if($log !== null) + if(!empty($options['type'])) { - 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); + $ext = $newOpts['type'] = $options['type']; } @@ -2682,6 +2678,19 @@ class e_parse extends e_parser $fname = $pre.strtolower($start.'_'.$cache_str.'_'.$size.'.'.$ext).$post; + + if($log !== null) + { + file_put_contents(e_LOG.$log, "\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n", FILE_APPEND); + $message = "Name: ".$fname."\n"; + $message .= $path."\n".var_export($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); + } + + + return $fname; } @@ -2797,12 +2806,12 @@ class e_parse extends e_parser * Generate an auto-sized Image URL. * @param $url - path to image or leave blank for a placeholder. eg. {e_MEDIA}folder/my-image.jpg * @param array $options - width and height, but leaving this empty and using $this->thumbWidth() and $this->thumbHeight() is preferred. ie. {SETWIDTH: w=x&y=x} - * @param int $options ['w'] width (optional) - * @param int $options ['h'] height (optional) + * @param int $options['w'] width (optional) + * @param int $options['h'] height (optional) * @param bool|string $options ['crop'] true/false or A(auto) or T(op) or B(ottom) or C(enter) or L(eft) or R(right) - * @param string $options ['scale'] '2x' (optional) - * @param bool $options ['x'] encode/mask the url parms (optional) - * @param bool $options ['nosef'] when set to true disabled SEF Url being returned (optional) + * @param string $options['scale'] '2x' (optional) + * @param bool $options['x'] encode/mask the url parms (optional) + * @param bool $options['nosef'] when set to true disabled SEF Url being returned (optional) * @param bool $raw set to true when the $url does not being with an e107 variable ie. "{e_XXXX}" eg. {e_MEDIA} (optional) * @param bool $full when true returns full http:// url. (optional) * @return string @@ -2839,8 +2848,6 @@ class e_parse extends e_parser } - - if(strstr($url,e_MEDIA) || strstr($url,e_SYSTEM)) // prevent disclosure of 'hashed' path. { @@ -2905,6 +2912,11 @@ class e_parse extends e_parser } + if(!empty($options['type']) && ($options['type'] === 'webp')) + { + $thurl .= '&type=webp'; + } + if(defined('e_MEDIA_STATIC')) // experimental - subject to change. { @@ -3092,6 +3104,10 @@ class e_parse extends e_parser $parms = array('w'=>$width,'h'=>$height,'crop'=> $parm['crop'],'x'=>$parm['x'], 'aw'=>$parm['aw'],'ah'=>$parm['ah']); + if(!empty($parm['type'])) + { + $parms['type'] = $parm['type']; + } // $parms = !empty($this->thumbCrop) ? array('aw' => $width, 'ah' => $height, 'x'=>$encode) : array('w' => $width, 'h' => $height, 'x'=>$encode ); // $parms['x'] = $encode; @@ -4606,6 +4622,7 @@ class e_parser * @param string $file * @param array $parm keys: legacy|w|h|alt|class|id|crop|loading * @param array $parm['legacy'] Usually a legacy path like {e_FILE} + * @param array $parm['type'] Force the returned image to be a jpg, webp etc. * @return string * @example $tp->toImage('welcome.png', array('legacy'=>{e_IMAGE}newspost_images/','w'=>200)); */ diff --git a/e107_handlers/e_thumbnail_class.php b/e107_handlers/e_thumbnail_class.php index d761ae499..cd1ac1bc6 100644 --- a/e107_handlers/e_thumbnail_class.php +++ b/e107_handlers/e_thumbnail_class.php @@ -154,6 +154,18 @@ class e_thumbnail $this->_request = (array) $array; } + private function getImageInfo() + { + $thumbnfo = pathinfo($this->_src_path); + + if(!empty($this->_request['type']) && $this->_request['type'] == 'webp') + { + $thumbnfo['extension'] = 'webp'; + } + + return $thumbnfo; + } + /** * Validate and Sanitize the Request. * @return bool true when request is okay. @@ -223,7 +235,7 @@ class e_thumbnail return $this; } - $thumbnfo = pathinfo($this->_src_path); + $thumbnfo = $this->getImageInfo(); $options = $this->getRequestOptions(); $fname = e107::getParser()->thumbCacheFile($this->_src_path, $options); $cache_filename = e_CACHE_IMAGE . $fname; @@ -479,6 +491,11 @@ class e_thumbnail $ret['c'] = 'T'; // default is 'Top'; } + if(!empty($this->_request['type'])) + { + $ret['type'] = $this->_request['type']; + } + return $ret; } diff --git a/e107_tests/tests/_data/thumbnailTest/image_16.webp b/e107_tests/tests/_data/thumbnailTest/image_16.webp new file mode 100644 index 000000000..c3906cb07 Binary files /dev/null and b/e107_tests/tests/_data/thumbnailTest/image_16.webp differ diff --git a/e107_tests/tests/_data/thumbnailTest/image_17.jpg b/e107_tests/tests/_data/thumbnailTest/image_17.jpg new file mode 100644 index 000000000..a6f663242 Binary files /dev/null and b/e107_tests/tests/_data/thumbnailTest/image_17.jpg differ diff --git a/e107_tests/tests/unit/e_parseTest.php b/e107_tests/tests/unit/e_parseTest.php index a54eaa760..0e37de82e 100644 --- a/e107_tests/tests/unit/e_parseTest.php +++ b/e107_tests/tests/unit/e_parseTest.php @@ -522,14 +522,27 @@ while($row = $sql->fetch()) public function testThumbUrl() { $urls = array( - array('path' => '{e_PLUGIN}gallery/images/butterfly.jpg', 'expected'=>'/thumb.php?src=e_PLUGIN%2Fgallery%2Fimages%2Fbutterfly.jpg&w=300&h=200'), - array('path' => '{e_PLUGIN}dummy/Freesample.svg', 'expected'=>'/e107_plugins/dummy/Freesample.svg'), + 0 => array( + 'path' => '{e_PLUGIN}gallery/images/butterfly.jpg', + 'options' => array('w'=>300, 'h'=>200), + 'expected' =>'/thumb.php?src=e_PLUGIN%2Fgallery%2Fimages%2Fbutterfly.jpg&w=300&h=200' + ), + 1 => array( + 'path' => '{e_PLUGIN}dummy/Freesample.svg', + 'options' => array('w'=>300, 'h'=>200), + 'expected' =>'/e107_plugins/dummy/Freesample.svg' + ), + 2 => array( + 'path' => '{e_PLUGIN}gallery/images/butterfly.jpg', + 'options' => array('w'=>300, 'h'=>200, 'type'=>'webp'), + 'expected' =>'/thumb.php?src=e_PLUGIN%2Fgallery%2Fimages%2Fbutterfly.jpg&w=300&h=200&type=webp' + ), ); foreach($urls as $val) { - $actual = $this->tp->thumbUrl($val['path'], array('w'=>300, 'h'=>200)); + $actual = $this->tp->thumbUrl($val['path'],$val['options']); $this->assertStringContainsString($val['expected'], $actual); //echo $$actual."\n\n"; @@ -562,12 +575,38 @@ while($row = $sql->fetch()) { } - +*/ public function testThumbCacheFile() { + $tests = array( + 0 => array( + 'file' => 'e107_plugins/gallery/images/butterfly.jpg', + 'options' => array ('w' => 222, 'h' => 272, 'aw' => 0, 'ah' => 0, 'c' => 0,), + 'expected' => array('prefix'=>'thumb_butterfly_', 'suffix'=>'.jpg.cache.bin'), + ), + 1 => array( + 'file' => 'e107_plugins/gallery/images/butterfly.jpg', + 'options' => array ('w' => 222, 'h' => 272, 'aw' => 0, 'ah' => 0, 'c' => 0, 'type'=>'webp'), + 'expected' => array('prefix'=>'thumb_butterfly_', 'suffix'=>'.webp.cache.bin'), + ), + + ); + + foreach($tests as $var) + { + + $result = $this->tp->thumbCacheFile($var['file'], $var['options']); + + $this->assertStringStartsWith($var['expected']['prefix'], $result); + $this->assertStringEndsWith($var['expected']['suffix'], $result); + + } + + + } - +/* public function testText_truncate() { @@ -944,6 +983,9 @@ while($row = $sql->fetch()) $result4 = $this->tp->toImage($src, ['loading'=>'lazy']); $this->assertStringContainsString('loading="lazy"', $result4); // src + $result5 = $this->tp->toImage($src, ['type'=>'webp']); + $this->assertStringContainsString('&type=webp', $result5); // src + } public function testThumbSrcSet() diff --git a/e107_tests/tests/unit/e_thumbnailTest.php b/e107_tests/tests/unit/e_thumbnailTest.php index b4fc59c11..264801584 100644 --- a/e107_tests/tests/unit/e_thumbnailTest.php +++ b/e107_tests/tests/unit/e_thumbnailTest.php @@ -27,7 +27,7 @@ } catch(Exception $e) { - $this->assertTrue(false, "Couldn't load e_thumbnail object"); + $this->assertTrue(false, $e->getMessage()); } $this->thm->setCache(false); @@ -157,7 +157,13 @@ ), - + // Test Converting JPEG to WebP and resize. (Stored index file is saved with a .jpg extension but encoded as WebP) + 17 => array ( + 'src' => 'e_PLUGIN/gallery/images/butterfly.jpg', + 'w' => 222, + 'h' => 272, + 'type'=>'webp' + ), );