Skip to content

Commit

Permalink
Migrate category images
Browse files Browse the repository at this point in the history
  • Loading branch information
Hlavtox committed Dec 3, 2024
1 parent 20e6b51 commit 5bd714b
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
107 changes: 107 additions & 0 deletions upgrade/php/ps_900_migrate_category_images.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @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);
}
}
}
4 changes: 4 additions & 0 deletions upgrade/sql/9.0.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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(); */;

0 comments on commit 5bd714b

Please sign in to comment.