Skip to content

Commit

Permalink
1.2.5
Browse files Browse the repository at this point in the history
Undefined array key  $_SERVER['HTTP_ACCEPT']
Add New flag in debug data
  • Loading branch information
conseilgouz committed Aug 7, 2024
1 parent aa30dc1 commit 9ccaedb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
4 changes: 2 additions & 2 deletions cgwebp.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<extension type="plugin" version="4.0" group="system" method="upgrade">
<name>System - CG Webp</name>
<creationDate>2024-05-05</creationDate>
<creationDate>2024-08-07</creationDate>
<author>ConseilGouz</author>
<copyright>Copyright (C) 2024 ConseilGouz. All rights reserved.</copyright>
<license>GNU/GPL</license>
<authorEmail>pascal.leconte@conseilgouz.com</authorEmail>
<authorUrl>https://www.conseilgouz.com</authorUrl>
<version>1.2.4</version>
<version>1.2.5</version>
<description>PLG_SYSTEM_CGWEBP_DESCRIPTION</description>
<namespace path="src">Conseilgouz\Plugin\System\Cgwebp</namespace>
<scriptfile>script.cgwebp.php</scriptfile>
Expand Down
15 changes: 15 additions & 0 deletions plg_system_cgwebp_changelog.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
<changelogs>
<changelog>
<element>plg_system_cgwebp</element>
<type>plugin</type>
<group>system</group>
<version>1.2.5</version>
<note>
<item>Update : 07/08/2024</item>
</note>
<fix>
<item>Undefined array key $_SERVER['HTTP_ACCEPT']</item>
</fix>
<addition>
<item>Add New flag in debug data</item>
</addition>
</changelog>
<changelog>
<element>plg_system_cgwebp</element>
<type>plugin</type>
Expand Down
23 changes: 16 additions & 7 deletions src/Extension/Cgwebp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
Expand All @@ -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];
}
}
Expand Down
19 changes: 11 additions & 8 deletions src/Helper/CgwebpHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down

0 comments on commit 9ccaedb

Please sign in to comment.