diff --git a/cgwebp.xml b/cgwebp.xml index 19f60bb..4b337d3 100644 --- a/cgwebp.xml +++ b/cgwebp.xml @@ -1,13 +1,13 @@ System - CG Webp - 2024-05-05 + 2024-08-07 ConseilGouz Copyright (C) 2024 ConseilGouz. All rights reserved. GNU/GPL pascal.leconte@conseilgouz.com https://www.conseilgouz.com - 1.2.4 + 1.2.5 PLG_SYSTEM_CGWEBP_DESCRIPTION Conseilgouz\Plugin\System\Cgwebp script.cgwebp.php diff --git a/plg_system_cgwebp_changelog.xml b/plg_system_cgwebp_changelog.xml index 1d7d0cf..45a656f 100644 --- a/plg_system_cgwebp_changelog.xml +++ b/plg_system_cgwebp_changelog.xml @@ -1,4 +1,19 @@ + + plg_system_cgwebp + plugin + system + 1.2.5 + + Update : 07/08/2024 + + + Undefined array key $_SERVER['HTTP_ACCEPT'] + + + Add New flag in debug data + + plg_system_cgwebp plugin diff --git a/src/Extension/Cgwebp.php b/src/Extension/Cgwebp.php index 378b26e..d4a4f43 100644 --- a/src/Extension/Cgwebp.php +++ b/src/Extension/Cgwebp.php @@ -107,12 +107,14 @@ private function gowebp($sHtml, $onefilter) '/' . $regexPath . '\/.*?(' . implode('|', $extensions) . ')(?=[\'"?#])|#joomlaImage.*?(' . implode('|', $extensions) . ').+?(?=\")\b/', function ($match) use ($quality, $stored_time, $excludedArr, &$debugTarget, $regexPath) { $img = $match[0]; - $newImg = $this->imgToWebp($img, $quality, $excludedArr, $stored_time, $regexPath, $match); - $debugTarget[] = array( - 'source' => $img, - 'target' => $newImg - ); - + $newImg = $this->imgToWebp($img, $quality, $excludedArr, $stored_time, $regexPath, $match,$debugTarget); + if (!$newImg) { // no conversion + $debugTarget[] = array( + 'source' => $image, + 'target' => $newImg, + 'new' => false + ); + } return $newImg ? $newImg : $img; }, $sHtml @@ -148,11 +150,12 @@ private function isExcludedDirectory($image, $excluded) return $exist; } - private function imgToWebp($image, $quality = 100, $excluded = array(), $stored_time = 5, $regexPath = '', $fullRegex = '') + private function imgToWebp($image, $quality = 100, $excluded = array(), $stored_time = 5, $regexPath = '', $fullRegex = '', &$debugTarget = []) { $imgPath = JPATH_ROOT . '/' . $image; $imgInfo = pathinfo($imgPath); $imgHash = md5($imgPath); + $bNew = false; if(!isset($imgInfo['extension']) || !$imgInfo['extension']) { return; @@ -201,6 +204,7 @@ private function imgToWebp($image, $quality = 100, $excluded = array(), $stored_ imagealphablending($img, true); imagesavealpha($img, true); imagewebp($img, $newImage, $quality); + $bNew = true; } catch (\Throwable $throwable) { return false; // conversion error :ignore image } @@ -209,6 +213,11 @@ private function imgToWebp($image, $quality = 100, $excluded = array(), $stored_ $newFile = str_replace(JPATH_ROOT . '/', "", $newImage)."?ver=".$imgHash; $this->_webps[$imgHash] = $newFile; } + $debugTarget[] = array( + 'source' => $image, + 'target' => $this->_webps[$imgHash], + 'new' => $bNew + ); return $this->_webps[$imgHash]; } } diff --git a/src/Helper/CgwebpHelper.php b/src/Helper/CgwebpHelper.php index ba518fb..922c722 100644 --- a/src/Helper/CgwebpHelper.php +++ b/src/Helper/CgwebpHelper.php @@ -17,30 +17,33 @@ class CgwebpHelper { public static function browserSupportWebp() { - $user_agent = $_SERVER['HTTP_USER_AGENT']; - if(!isset($_SERVER['HTTP_USER_AGENT'])) return false; + $user_agent = $_SERVER['HTTP_USER_AGENT']; // If browser is Internet Explorer - if (isset($_SERVER['HTTP_USER_AGENT']) && - (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)) return false; + if (strpos(user_agent, 'MSIE') !== false) return false; // If user browser is safari and is not Opera if( strpos($user_agent, 'Safari') && - !strpos( $_SERVER['HTTP_USER_AGENT'], ' Chrome/' ) && + !strpos( $user_agent, ' Chrome/' ) && !(strpos($user_agent, 'Edge')) ) return false; // If windows mobile - if(strpos($_SERVER['HTTP_USER_AGENT'], 'Windows Phone 8.1')) return false; + if(strpos($user_agent, 'Windows Phone 8.1')) return false; + // If browser doesnt support webp + if (isset($_SERVER['HTTP_ACCEPT'])) { + if (!strpos( $_SERVER['HTTP_ACCEPT'], 'image/webp' )) { + return false; + } + } // If browser doesnt support webp and is not chrome/firefox/opera/edge if( - !strpos( $_SERVER['HTTP_ACCEPT'], 'image/webp' ) && - !strpos( $_SERVER['HTTP_USER_AGENT'], ' Chrome/' ) && + !strpos($user_agent, ' Chrome/' ) && !strpos($user_agent, 'Firefox') && !(strpos($user_agent, 'Opera') || strpos($user_agent, 'OPR/')) && !(strpos($user_agent, 'Edge'))