mirror of
https://github.com/e107inc/e107.git
synced 2025-08-03 05:07:27 +02:00
Issue #4270 On-the-fly conversion to WebP format with fallback for older browsers when using toImage().
This commit is contained in:
@@ -510,6 +510,10 @@ if(!e107::getConfig()->hasData())
|
|||||||
//DEPRECATED, BC, call e107::getPref/findPref() instead
|
//DEPRECATED, BC, call e107::getPref/findPref() instead
|
||||||
$pref = e107::getPref();
|
$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
|
//this could be part of e107->init() method now, prefs will be auto-initialized
|
||||||
//when proper called (e107::getPref())
|
//when proper called (e107::getPref())
|
||||||
// $e107->set_base_path(); moved to init().
|
// $e107->set_base_path(); moved to init().
|
||||||
|
@@ -3843,9 +3843,9 @@ class e_parser
|
|||||||
public $isHtml = false;
|
public $isHtml = false;
|
||||||
|
|
||||||
|
|
||||||
protected $bootstrap = null;
|
protected $bootstrap = null;
|
||||||
protected $fontawesome = null;
|
protected $fontawesome = null;
|
||||||
protected $convertToWebp = false;
|
protected $convertToWebP = false;
|
||||||
|
|
||||||
protected $removedList = array();
|
protected $removedList = array();
|
||||||
protected $nodesToDelete = array();
|
protected $nodesToDelete = array();
|
||||||
@@ -3955,10 +3955,6 @@ class e_parser
|
|||||||
$this->staticUrl = e_HTTP_STATIC;
|
$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
|
* @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']."\" " ;
|
$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.'&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
@@ -986,6 +986,21 @@ while($row = $sql->fetch())
|
|||||||
$result5 = $this->tp->toImage($src, ['type'=>'webp']);
|
$result5 = $this->tp->toImage($src, ['type'=>'webp']);
|
||||||
$this->assertStringContainsString('&type=webp', $result5); // src
|
$this->assertStringContainsString('&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&w=80&h=80&type=webp">
|
||||||
|
<source type="image/webp" srcset="'.$tempDir.'thumb.php?src=e_PLUGIN%2Fgallery%2Fimages%2Fbutterfly.jpg&w=320&h=320&type=webp 4x">
|
||||||
|
<source type="image/jpeg" srcset="'.$tempDir.'thumb.php?src=e_PLUGIN%2Fgallery%2Fimages%2Fbutterfly.jpg&w=320&h=320 4x">
|
||||||
|
<img class="img-responsive img-fluid" src="'.$tempDir.'thumb.php?src=e_PLUGIN%2Fgallery%2Fimages%2Fbutterfly.jpg&w=80&h=80" alt="butterfly.jpg" width="80" height="80" />
|
||||||
|
</picture>';
|
||||||
|
|
||||||
|
$this->assertSame($expected,$result6);
|
||||||
|
$this->tp->setConvertToWebP(false);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testThumbSrcSet()
|
public function testThumbSrcSet()
|
||||||
|
@@ -134,11 +134,11 @@
|
|||||||
$tp = e107::getParser();
|
$tp = e107::getParser();
|
||||||
|
|
||||||
$result = $tp->parseTemplate("{BANNER=e107promo}",true);
|
$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,
|
$result = $tp->parseTemplate("{BANNER=e107promo}",false,
|
||||||
e107::getScBatch('banner', true));
|
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);
|
$result = $tp->parseTemplate("{BANNER=e107promo}",false);
|
||||||
$this->assertEquals("", $result);
|
$this->assertEquals("", $result);
|
||||||
|
Reference in New Issue
Block a user