Skip to content

Commit

Permalink
1.0.3
Browse files Browse the repository at this point in the history
Handle image exceptions
  • Loading branch information
conseilgouz committed Apr 14, 2024
1 parent 4208a42 commit 56fe0ff
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 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-04-13</creationDate>
<creationDate>2024-04-14</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.0.2</version>
<version>1.0.3</version>
<description>PLG_SYSTEM_CGWEBP_DESCRIPTION</description>
<namespace path="src">Conseilgouz\Plugin\System\Cgwebp</namespace>
<scriptfile>script.cgwebp.php</scriptfile>
Expand Down
12 changes: 12 additions & 0 deletions plg_system_cgwebp_changelog.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
<changelogs>
<changelog>
<element>plg_system_cgwebp</element>
<type>plugin</type>
<group>system</group>
<version>1.0.3</version>
<note>
<item>Update : 14/04/2024</item>
</note>
<fix>
<item>Handle image exceptions</item>
</fix>
</changelog>
<changelog>
<element>plg_system_cgwebp</element>
<type>plugin</type>
Expand Down
39 changes: 22 additions & 17 deletions src/Extension/Cgwebp.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* @version 1.0.2
* @version 1.0.3
* @package CGWebp system plugin
* @author ConseilGouz
* @copyright Copyright (C) 2024 ConseilGouz. All rights reserved.
Expand All @@ -17,6 +17,7 @@
use Joomla\Event\SubscriberInterface;
use Joomla\Filesystem\File;
use Joomla\Filesystem\Folder;
use Joomla\CMS\Log\Log;
use Conseilgouz\Plugin\System\Cgwebp\Helper\CgwebpHelper;

final class Cgwebp extends CMSPlugin implements SubscriberInterface
Expand Down Expand Up @@ -176,7 +177,7 @@ private function imgToWebp($image, $quality = 100, $excluded = array(), $stored_

if(is_file($imgPath)) {
if (!isset($this->_webps[$imgHash])) {
if ($this->params->get('storage','same') == "same") { // same as original image
if ($this->params->get('storage', 'same') == "same") { // same as original image
$newImagePath = $imgInfo['dirname'] . '/';
} else { // in media/plg_system_webp directory
$newImagePath = JPATH_ROOT .'/media/plg_system_cgwebp/'.pathinfo($image)['dirname'].'/';
Expand All @@ -193,21 +194,26 @@ private function imgToWebp($image, $quality = 100, $excluded = array(), $stored_
}
if (!is_file($newImage)) {
if (is_file($imgPath)) {
switch (strtolower($imgInfo['extension'])) {
case 'png':
$img = imagecreatefrompng($imgPath);
break;
case 'jpg':
case 'jpeg':
$img = imagecreatefromjpeg($imgPath);
}
if (!is_dir($newImagePath)) {
Folder::create($newImagePath);
try {
switch (strtolower($imgInfo['extension'])) {
case 'png':
$img = imagecreatefrompng($imgPath);
break;
case 'jpg':
case 'jpeg':
$img = imagecreatefromjpeg($imgPath);
}
if (!is_dir($newImagePath)) {
Folder::create($newImagePath);
}
imagepalettetotruecolor($img);
imagealphablending($img, true);
imagesavealpha($img, true);
imagewebp($img, $newImage, $quality);
} catch (\Throwable $throwable) {
Log::add('unable to enable plugin bday', Log::ERROR, 'jerror');
return false;
}
imagepalettetotruecolor($img);
imagealphablending($img, true);
imagesavealpha($img, true);
imagewebp($img, $newImage, $quality);
}
}
$newFile = str_replace(JPATH_ROOT . '/', "", $newImage);
Expand All @@ -216,5 +222,4 @@ private function imgToWebp($image, $quality = 100, $excluded = array(), $stored_
return $this->_webps[$imgHash];
}
}

}

0 comments on commit 56fe0ff

Please sign in to comment.