1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-06 14:46:56 +02:00

Thumbnailer: Added option for on-the-fly conversion to WebP format. Example: toImage($src, ['w'=>'type'=>'webp']);

This commit is contained in:
Cameron
2020-12-08 09:10:26 -08:00
parent 005bff7e59
commit a094a8fb73
6 changed files with 110 additions and 28 deletions

View File

@@ -36,17 +36,17 @@ class e_parse extends e_parser
protected $e_bb; protected $e_bb;
// Profanity filter // Profanity filter
protected $e_pf; public $e_pf;
// Emote filter // Emote filter
protected $e_emote; public $e_emote;
// 'Hooked' parsers (array) // 'Hooked' parsers (array)
protected $e_hook; protected $e_hook;
protected $search = array(''', ''', ''', '"', 'onerror', '>', '"', ' & '); public $search = array(''', ''', ''', '"', 'onerror', '>', '"', ' & ');
protected $replace = array("'", "'", "'", '"', 'one<i></i>rror', '>', '"', ' &amp; '); public $replace = array("'", "'", "'", '"', 'one<i></i>rror', '>', '"', ' &amp; ');
// Set to TRUE or FALSE once it has been calculated // Set to TRUE or FALSE once it has been calculated
protected $e_highlighting; protected $e_highlighting;
@@ -2609,6 +2609,7 @@ class e_parse extends e_parser
* Generated a Thumb Cache File Name from path and options. * Generated a Thumb Cache File Name from path and options.
* @param string $path * @param string $path
* @param array $options * @param array $options
* @param string $log (optional) - log file name
* @return null|string * @return null|string
*/ */
public function thumbCacheFile($path, $options=null, $log=null) public function thumbCacheFile($path, $options=null, $log=null)
@@ -2630,8 +2631,7 @@ class e_parse extends e_parser
$tmp = explode('.',$filename); $tmp = explode('.',$filename);
$ext = end($tmp); $ext = end($tmp);
$len = strlen($ext) + 1; $len = strlen($ext) + 1;
$start = substr($filename,0,- $len); $start = substr($filename,0, - $len);
// cleanup. // cleanup.
$newOpts = array( $newOpts = array(
@@ -2639,16 +2639,12 @@ class e_parse extends e_parser
'h' => (string) intval($options['h']), 'h' => (string) intval($options['h']),
'aw' => (string) intval($options['aw']), 'aw' => (string) intval($options['aw']),
'ah' => (string) intval($options['ah']), '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); $ext = $newOpts['type'] = $options['type'];
$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);
} }
@@ -2682,6 +2678,19 @@ class e_parse extends e_parser
$fname = $pre.strtolower($start.'_'.$cache_str.'_'.$size.'.'.$ext).$post; $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; return $fname;
} }
@@ -2797,12 +2806,12 @@ class e_parse extends e_parser
* Generate an auto-sized Image URL. * 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 $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 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['w'] width (optional)
* @param int $options ['h'] height (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 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 string $options['scale'] '2x' (optional)
* @param bool $options ['x'] encode/mask the url parms (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 $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 $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) * @param bool $full when true returns full http:// url. (optional)
* @return string * @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. 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 .= '&amp;type=webp';
}
if(defined('e_MEDIA_STATIC')) // experimental - subject to change. 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']); $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 = !empty($this->thumbCrop) ? array('aw' => $width, 'ah' => $height, 'x'=>$encode) : array('w' => $width, 'h' => $height, 'x'=>$encode );
// $parms['x'] = $encode; // $parms['x'] = $encode;
@@ -4606,6 +4622,7 @@ class e_parser
* @param string $file * @param string $file
* @param array $parm keys: legacy|w|h|alt|class|id|crop|loading * @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['legacy'] Usually a legacy path like {e_FILE}
* @param array $parm['type'] Force the returned image to be a jpg, webp etc.
* @return string * @return string
* @example $tp->toImage('welcome.png', array('legacy'=>{e_IMAGE}newspost_images/','w'=>200)); * @example $tp->toImage('welcome.png', array('legacy'=>{e_IMAGE}newspost_images/','w'=>200));
*/ */

View File

@@ -154,6 +154,18 @@ class e_thumbnail
$this->_request = (array) $array; $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. * Validate and Sanitize the Request.
* @return bool true when request is okay. * @return bool true when request is okay.
@@ -223,7 +235,7 @@ class e_thumbnail
return $this; return $this;
} }
$thumbnfo = pathinfo($this->_src_path); $thumbnfo = $this->getImageInfo();
$options = $this->getRequestOptions(); $options = $this->getRequestOptions();
$fname = e107::getParser()->thumbCacheFile($this->_src_path, $options); $fname = e107::getParser()->thumbCacheFile($this->_src_path, $options);
$cache_filename = e_CACHE_IMAGE . $fname; $cache_filename = e_CACHE_IMAGE . $fname;
@@ -479,6 +491,11 @@ class e_thumbnail
$ret['c'] = 'T'; // default is 'Top'; $ret['c'] = 'T'; // default is 'Top';
} }
if(!empty($this->_request['type']))
{
$ret['type'] = $this->_request['type'];
}
return $ret; return $ret;
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

View File

@@ -522,14 +522,27 @@ while(&#036;row = &#036;sql-&gt;fetch())
public function testThumbUrl() public function testThumbUrl()
{ {
$urls = array( $urls = array(
array('path' => '{e_PLUGIN}gallery/images/butterfly.jpg', 'expected'=>'/thumb.php?src=e_PLUGIN%2Fgallery%2Fimages%2Fbutterfly.jpg&amp;w=300&amp;h=200'), 0 => array(
array('path' => '{e_PLUGIN}dummy/Freesample.svg', 'expected'=>'/e107_plugins/dummy/Freesample.svg'), 'path' => '{e_PLUGIN}gallery/images/butterfly.jpg',
'options' => array('w'=>300, 'h'=>200),
'expected' =>'/thumb.php?src=e_PLUGIN%2Fgallery%2Fimages%2Fbutterfly.jpg&amp;w=300&amp;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&amp;w=300&amp;h=200&amp;type=webp'
),
); );
foreach($urls as $val) 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); $this->assertStringContainsString($val['expected'], $actual);
//echo $$actual."\n\n"; //echo $$actual."\n\n";
@@ -562,12 +575,38 @@ while(&#036;row = &#036;sql-&gt;fetch())
{ {
} }
*/
public function testThumbCacheFile() 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() public function testText_truncate()
{ {
@@ -944,6 +983,9 @@ while(&#036;row = &#036;sql-&gt;fetch())
$result4 = $this->tp->toImage($src, ['loading'=>'lazy']); $result4 = $this->tp->toImage($src, ['loading'=>'lazy']);
$this->assertStringContainsString('loading="lazy"', $result4); // src $this->assertStringContainsString('loading="lazy"', $result4); // src
$result5 = $this->tp->toImage($src, ['type'=>'webp']);
$this->assertStringContainsString('&amp;type=webp', $result5); // src
} }
public function testThumbSrcSet() public function testThumbSrcSet()

View File

@@ -27,7 +27,7 @@
} }
catch(Exception $e) catch(Exception $e)
{ {
$this->assertTrue(false, "Couldn't load e_thumbnail object"); $this->assertTrue(false, $e->getMessage());
} }
$this->thm->setCache(false); $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'
),
); );