From fb575c257c1ae15b203734b94d2ff14919e72f7b Mon Sep 17 00:00:00 2001 From: Cameron Date: Wed, 9 Dec 2020 10:54:15 -0800 Subject: [PATCH] Issue #4270 On-the-fly conversion to WebP format with fallback for older browsers when using toImage(). --- class2.php | 4 +++ e107_handlers/e_parse_class.php | 40 ++++++++++++++++++++------- e107_tests/tests/unit/e_formTest.php | 8 +++--- e107_tests/tests/unit/e_parseTest.php | 15 ++++++++++ e107_tests/tests/unit/pluginsTest.php | 4 +-- 5 files changed, 55 insertions(+), 16 deletions(-) diff --git a/class2.php b/class2.php index 112d69bab..1093b9ba9 100755 --- a/class2.php +++ b/class2.php @@ -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(). diff --git a/e107_handlers/e_parse_class.php b/e107_handlers/e_parse_class.php index 1253fd86e..736729014 100644 --- a/e107_handlers/e_parse_class.php +++ b/e107_handlers/e_parse_class.php @@ -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 "\"".$alt."\""; + $html = ''; + + if($this->convertToWebP) + { + $parm['type'] = 'webp'; + $source = $tp->thumbUrl($file,$parm); + $html = "\n"; + $html .= ''; + $html .= "\n"; + if(!empty($parm['srcset'])) + { + list($webPSourceSet,$webPSize) = explode(' ', $parm['srcset']); + $html .= ''; + $html .= "\n"; + $html .= ''; + $html .= "\n"; + $srcset = ''; // remove it from the img tag below. + } + } + + $html .= "\"".$alt."\""; + + $html .= ($this->convertToWebP) ? "\n" : ''; + + return $html; } diff --git a/e107_tests/tests/unit/e_formTest.php b/e107_tests/tests/unit/e_formTest.php index 6f2b74a6f..4da546ad6 100644 --- a/e107_tests/tests/unit/e_formTest.php +++ b/e107_tests/tests/unit/e_formTest.php @@ -832,8 +832,8 @@ class e_formTest extends \Codeception\Test\Unit 'layout_001' => 'default', 'layout_002' => 'default', - 'image_001' => "\"e107_adminlogo.png\"", - 'image_002' => "\"butterfly.jpg\"", + 'image_001' => "\"e107_adminlogo.png\"", + 'image_002' => "\"butterfly.jpg\"", 'checkboxes_001' => 'Check Opt 2, Check Opt 3', @@ -946,8 +946,8 @@ class e_formTest extends \Codeception\Test\Unit 'layout_001' => "", 'layout_002' => "", - 'image_001' => "\"e107_adminlogo.png\"", - 'image_002' => "\"butterfly.jpg\"", + 'image_001' => "\"e107_adminlogo.png\"", + 'image_002' => "\"butterfly.jpg\"", 'checkboxes_001' => "
", 'country_001' => "", diff --git a/e107_tests/tests/unit/e_parseTest.php b/e107_tests/tests/unit/e_parseTest.php index 0e37de82e..be57a1dfb 100644 --- a/e107_tests/tests/unit/e_parseTest.php +++ b/e107_tests/tests/unit/e_parseTest.php @@ -986,6 +986,21 @@ while($row = $sql->fetch()) $result5 = $this->tp->toImage($src, ['type'=>'webp']); $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 = ' + + + +butterfly.jpg +'; + + $this->assertSame($expected,$result6); + $this->tp->setConvertToWebP(false); + + } public function testThumbSrcSet() diff --git a/e107_tests/tests/unit/pluginsTest.php b/e107_tests/tests/unit/pluginsTest.php index dcefad969..b73b7b83b 100644 --- a/e107_tests/tests/unit/pluginsTest.php +++ b/e107_tests/tests/unit/pluginsTest.php @@ -134,11 +134,11 @@ $tp = e107::getParser(); $result = $tp->parseTemplate("{BANNER=e107promo}",true); - $this->assertStringContainsString("assertStringContainsString('parseTemplate("{BANNER=e107promo}",false, e107::getScBatch('banner', true)); - $this->assertStringContainsString("assertStringContainsString('parseTemplate("{BANNER=e107promo}",false); $this->assertEquals("", $result);