From 5bd714b6fb156e8abc9a948811e1ce506669a96b Mon Sep 17 00:00:00 2001 From: Hlavtox Date: Tue, 3 Dec 2024 12:48:13 +0100 Subject: [PATCH] Migrate category images --- .../php/ps_900_migrate_category_images.php | 107 ++++++++++++++++++ upgrade/sql/9.0.0.sql | 4 + 2 files changed, 111 insertions(+) create mode 100644 upgrade/php/ps_900_migrate_category_images.php diff --git a/upgrade/php/ps_900_migrate_category_images.php b/upgrade/php/ps_900_migrate_category_images.php new file mode 100644 index 0000000000..b86a460ea5 --- /dev/null +++ b/upgrade/php/ps_900_migrate_category_images.php @@ -0,0 +1,107 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + */ +function ps_900_migrate_category_images() +{ + /* + * Pre v9 worked like this: + * There could be 123.jpg, a cover image. + * There could be 123_thumb.jpg, a category miniature. + * + * If 123_thumb.jpg existed, it took it and generated category thumbnails from it. + * But, overwriting thumbnails of 123.jpg. + * + * So you suddenly got one thumbnail that was from a different image. + * Even worse, frontoffice and backoffice generated it as a different thumbnail, + * one as small, one as medium. + */ + $formattedNameSmall = ImageType::getFormattedName('small'); + $formattedNameMedium = ImageType::getFormattedName('medium'); + + // Get categories on shop + $categoryIds = Db::getInstance()->executeS('SELECT id_category FROM `' . _DB_PREFIX_ . 'category`'); + if (empty($categoryIds)) { + return; + } + + foreach ($categoryIds as $row) { + // Get the ID + $categoryId = (int) $row['id_category']; + + /* + * Define paths to original files. + * Define paths to possible broken thumbnails. + */ + $categoryCoverPath = _PS_CAT_IMG_DIR_ . $categoryId . '.jpg'; + $categoryMiniaturePath = _PS_CAT_IMG_DIR_ . $categoryId . '_thumb.jpg'; + $categorySmallPath =_PS_CAT_IMG_DIR_ . $categoryId . '-' . $formattedNameSmall; + $categoryMediumPath = _PS_CAT_IMG_DIR_ . $categoryId . '-' . $formattedNameMedium; + + // Check what images exist + $hasCover = file_exists($categoryCoverPath); + $hasMiniature = file_exists($categoryMiniaturePath); + + /* + * If both exist, generated thumbnails can be wrong. + * We delete them and we are finished. + */ + if ($hasCover && $hasMiniature) { + deleteAllThumbnailsIfExist($categorySmallPath); + deleteAllThumbnailsIfExist($categoryMediumPath); + continue; + } + + /* + * If it has a cover but no thumbnail, we don't want people + * to end up with no thumbnail. We will copy the cover + * as the thumbnail. If the destination file already exists, + * it will be overwritten. + */ + if ($hasCover && !$hasMiniature) { + @copy($categoryCoverPath, $categoryMiniaturePath); + continue; + } + + /* + * If it has a thumbnail but no cover, we will delete thumbnails, + * because they have a wrong name and we don't want them to make + * issues. + */ + if (!$hasCover && $hasMiniature) { + deleteAllThumbnailsIfExist($categorySmallPath); + deleteAllThumbnailsIfExist($categoryMediumPath); + } + } +} + +function deleteAllThumbnailsIfExist($pathWithNoExtension) +{ + foreach (['jpg', 'png', 'webp', 'avif'] as $extension) { + $path = $pathWithNoExtension . '.' . $extension; + if (file_exists($path)) { + @unlink($path); + } + } +} diff --git a/upgrade/sql/9.0.0.sql b/upgrade/sql/9.0.0.sql index d6e4bd9f0e..2e9922f82e 100644 --- a/upgrade/sql/9.0.0.sql +++ b/upgrade/sql/9.0.0.sql @@ -197,3 +197,7 @@ ALTER TABLE `PREFIX_shop_url` CHANGE `id_shop` `id_shop` int(11) unsigned NOT NU ALTER TABLE `PREFIX_shop_url` ADD UNIQUE KEY `full_shop_url` (`domain`,`physical_uri`,`virtual_uri`); ALTER TABLE `PREFIX_shop_url` ADD UNIQUE KEY `full_shop_url_ssl` (`domain_ssl`,`physical_uri`,`virtual_uri`); ALTER TABLE `PREFIX_shop_url` ADD KEY `id_shop` (`id_shop`,`main`); + +/* Fix category thumbnail images */ +/* https://github.com/PrestaShop/PrestaShop/pull/36877 */ +/* PHP:ps_900_migrate_category_images(); */;