Fix favicon: support JPG/WebP/GIF, add logging, fix path resolution
Some checks failed
Repo Health / Access control (push) Successful in 1s
Auto-Update SHA Hash / Update SHA-256 Hash in updates.xml (release) Failing after 5s
Repo Health / Scripts governance (push) Successful in 3s
Repo Health / Release configuration (push) Failing after 3s
Repo Health / Repository health (push) Failing after 3s
Some checks failed
Repo Health / Access control (push) Successful in 1s
Auto-Update SHA Hash / Update SHA-256 Hash in updates.xml (release) Failing after 5s
Repo Health / Scripts governance (push) Successful in 3s
Repo Health / Release configuration (push) Failing after 3s
Repo Health / Repository health (push) Failing after 3s
- Support all common image formats (PNG, JPEG, GIF, WebP, BMP) not just PNG — uses getimagesize() to detect type - Add Log::add() warnings when generation fails (GD missing, file not found, unsupported format) - Fix source path: try both direct path and images/ prefix to handle Joomla media field variations Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -14,6 +14,8 @@
|
|||||||
|
|
||||||
defined('_JEXEC') or die;
|
defined('_JEXEC') or die;
|
||||||
|
|
||||||
|
use Joomla\CMS\Log\Log;
|
||||||
|
|
||||||
class MokoFaviconHelper
|
class MokoFaviconHelper
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@@ -39,7 +41,13 @@ class MokoFaviconHelper
|
|||||||
*/
|
*/
|
||||||
public static function generate(string $sourcePath, string $outputDir): bool
|
public static function generate(string $sourcePath, string $outputDir): bool
|
||||||
{
|
{
|
||||||
if (!is_file($sourcePath) || !extension_loaded('gd')) {
|
if (!extension_loaded('gd')) {
|
||||||
|
Log::add('Favicon: GD extension not loaded', Log::WARNING, 'mokocassiopeia');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_file($sourcePath)) {
|
||||||
|
Log::add('Favicon: source file not found: ' . $sourcePath, Log::WARNING, 'mokocassiopeia');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,8 +63,24 @@ class MokoFaviconHelper
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$source = imagecreatefrompng($sourcePath);
|
// Detect image type and load accordingly
|
||||||
|
$imageInfo = @getimagesize($sourcePath);
|
||||||
|
if ($imageInfo === false) {
|
||||||
|
Log::add('Favicon: cannot read image info from ' . $sourcePath, Log::WARNING, 'mokocassiopeia');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$source = match ($imageInfo[2]) {
|
||||||
|
IMAGETYPE_PNG => @imagecreatefrompng($sourcePath),
|
||||||
|
IMAGETYPE_JPEG => @imagecreatefromjpeg($sourcePath),
|
||||||
|
IMAGETYPE_GIF => @imagecreatefromgif($sourcePath),
|
||||||
|
IMAGETYPE_WEBP => function_exists('imagecreatefromwebp') ? @imagecreatefromwebp($sourcePath) : false,
|
||||||
|
IMAGETYPE_BMP => function_exists('imagecreatefrombmp') ? @imagecreatefrombmp($sourcePath) : false,
|
||||||
|
default => false,
|
||||||
|
};
|
||||||
|
|
||||||
if (!$source) {
|
if (!$source) {
|
||||||
|
Log::add('Favicon: unsupported image type (' . ($imageInfo['mime'] ?? 'unknown') . ') at ' . $sourcePath, Log::WARNING, 'mokocassiopeia');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -71,7 +71,13 @@ $templatePath = 'media/templates/site/mokocassiopeia';
|
|||||||
$faviconHeadTags = '';
|
$faviconHeadTags = '';
|
||||||
if ($params_favicon_source) {
|
if ($params_favicon_source) {
|
||||||
require_once __DIR__ . '/helper/favicon.php';
|
require_once __DIR__ . '/helper/favicon.php';
|
||||||
$faviconSourceAbs = JPATH_ROOT . '/' . ltrim($params_favicon_source, '/');
|
// Joomla's media field may return 'images/file.png' or just 'file.png'
|
||||||
|
$faviconSourceRel = ltrim($params_favicon_source, '/');
|
||||||
|
$faviconSourceAbs = JPATH_ROOT . '/' . $faviconSourceRel;
|
||||||
|
// If not found, try prepending images/
|
||||||
|
if (!is_file($faviconSourceAbs) && !str_starts_with($faviconSourceRel, 'images/')) {
|
||||||
|
$faviconSourceAbs = JPATH_ROOT . '/images/' . $faviconSourceRel;
|
||||||
|
}
|
||||||
$faviconOutputDir = JPATH_ROOT . '/images/favicons';
|
$faviconOutputDir = JPATH_ROOT . '/images/favicons';
|
||||||
$faviconUrlBase = Uri::root(true) . '/images/favicons';
|
$faviconUrlBase = Uri::root(true) . '/images/favicons';
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user