diff --git a/src/helper/favicon.php b/src/helper/favicon.php index 028eaf2..78d6269 100644 --- a/src/helper/favicon.php +++ b/src/helper/favicon.php @@ -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; } diff --git a/src/index.php b/src/index.php index 70db6f6..2c207cb 100644 --- a/src/index.php +++ b/src/index.php @@ -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';