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

- 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:
Jonathan Miller
2026-04-18 12:24:42 -05:00
parent 326d1c6a6f
commit 2ad624a48c
2 changed files with 33 additions and 3 deletions

View File

@@ -14,6 +14,8 @@
defined('_JEXEC') or die;
use Joomla\CMS\Log\Log;
class MokoFaviconHelper
{
/**
@@ -39,7 +41,13 @@ class MokoFaviconHelper
*/
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;
}
@@ -55,8 +63,24 @@ class MokoFaviconHelper
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) {
Log::add('Favicon: unsupported image type (' . ($imageInfo['mime'] ?? 'unknown') . ') at ' . $sourcePath, Log::WARNING, 'mokocassiopeia');
return false;
}

View File

@@ -71,7 +71,13 @@ $templatePath = 'media/templates/site/mokocassiopeia';
$faviconHeadTags = '';
if ($params_favicon_source) {
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';
$faviconUrlBase = Uri::root(true) . '/images/favicons';