1
0
mirror of https://github.com/e107inc/e107.git synced 2025-03-14 01:19:44 +01:00

Issue #4270 On-the-fly conversion to WebP format with fallback for older browsers when using toImage().

This commit is contained in:
Cameron 2020-12-09 10:54:15 -08:00
parent eb94769e1c
commit fb575c257c
5 changed files with 55 additions and 16 deletions

View File

@ -510,6 +510,10 @@ if(!e107::getConfig()->hasData())
//DEPRECATED, BC, call e107::getPref/findPref() instead
$pref = e107::getPref();
if(!empty($pref['thumb_to_webp']))
{
$tp->setConvertToWebP(true);
}
//this could be part of e107->init() method now, prefs will be auto-initialized
//when proper called (e107::getPref())
// $e107->set_base_path(); moved to init().

View File

@ -3843,9 +3843,9 @@ class e_parser
public $isHtml = false;
protected $bootstrap = null;
protected $fontawesome = null;
protected $convertToWebp = false;
protected $bootstrap = null;
protected $fontawesome = null;
protected $convertToWebP = false;
protected $removedList = array();
protected $nodesToDelete = array();
@ -3955,10 +3955,6 @@ class e_parser
$this->staticUrl = e_HTTP_STATIC;
}
// if(e107::pref('core', 'thumb_to_webp', false))
{
$this->convertToWebp = true;
}
}
@ -4059,9 +4055,9 @@ class e_parser
/**
* @param bool $bool
*/
public function setConvertToWebp($bool)
public function setConvertToWebP($bool)
{
$this->convertToWebp = (bool) $bool;
$this->convertToWebP = (bool) $bool;
}
@ -4772,7 +4768,31 @@ class e_parser
$height = "height=\"".$parm['height']."\" " ;
}
return "<img {$id}class='{$class}' src='".$path."' alt=\"".$alt."\" ".$srcset.$width.$height.$style.$loading.$title." />";
$html = '';
if($this->convertToWebP)
{
$parm['type'] = 'webp';
$source = $tp->thumbUrl($file,$parm);
$html = "<picture>\n";
$html .= '<source type="image/webp" srcset="'.$source.'">';
$html .= "\n";
if(!empty($parm['srcset']))
{
list($webPSourceSet,$webPSize) = explode(' ', $parm['srcset']);
$html .= '<source type="image/webp" srcset="'.$webPSourceSet.'&amp;type=webp '.$webPSize.'">';
$html .= "\n";
$html .= '<source type="image/'.str_replace('jpg', 'jpeg',$ext).'" srcset="'.$parm['srcset'].'">';
$html .= "\n";
$srcset = ''; // remove it from the img tag below.
}
}
$html .= "<img {$id}class=\"{$class}\" src=\"".$path."\" alt=\"".$alt."\" ".$srcset.$width.$height.$style.$loading.$title." />";
$html .= ($this->convertToWebP) ? "\n</picture>" : '';
return $html;
}

File diff suppressed because one or more lines are too long

View File

@ -986,6 +986,21 @@ while(&#036;row = &#036;sql-&gt;fetch())
$result5 = $this->tp->toImage($src, ['type'=>'webp']);
$this->assertStringContainsString('&amp;type=webp', $result5); // src
$this->tp->setConvertToWebP(true);
$result6 = $this->tp->toImage($src);
$tempDir = str_replace(['C:','\\'],['','/'],sys_get_temp_dir()).'/'; // FIXME
$expected = '<picture>
<source type="image/webp" srcset="'.$tempDir.'thumb.php?src=e_PLUGIN%2Fgallery%2Fimages%2Fbutterfly.jpg&amp;w=80&amp;h=80&amp;type=webp">
<source type="image/webp" srcset="'.$tempDir.'thumb.php?src=e_PLUGIN%2Fgallery%2Fimages%2Fbutterfly.jpg&amp;w=320&amp;h=320&amp;type=webp 4x">
<source type="image/jpeg" srcset="'.$tempDir.'thumb.php?src=e_PLUGIN%2Fgallery%2Fimages%2Fbutterfly.jpg&amp;w=320&amp;h=320 4x">
<img class="img-responsive img-fluid" src="'.$tempDir.'thumb.php?src=e_PLUGIN%2Fgallery%2Fimages%2Fbutterfly.jpg&amp;w=80&amp;h=80" alt="butterfly.jpg" width="80" height="80" />
</picture>';
$this->assertSame($expected,$result6);
$this->tp->setConvertToWebP(false);
}
public function testThumbSrcSet()

View File

@ -134,11 +134,11 @@
$tp = e107::getParser();
$result = $tp->parseTemplate("{BANNER=e107promo}",true);
$this->assertStringContainsString("<img class='e-banner img-responsive img-fluid'", $result);
$this->assertStringContainsString('<img class="e-banner img-responsive img-fluid"', $result);
$result = $tp->parseTemplate("{BANNER=e107promo}",false,
e107::getScBatch('banner', true));
$this->assertStringContainsString("<img class='e-banner img-responsive img-fluid'", $result);
$this->assertStringContainsString('<img class="e-banner img-responsive img-fluid"', $result);
$result = $tp->parseTemplate("{BANNER=e107promo}",false);
$this->assertEquals("", $result);