Skip to content

Commit

Permalink
Merge pull request #1060 from Hlavtox/pr-36877
Browse files Browse the repository at this point in the history
Migrate category images
  • Loading branch information
Hlavtox authored Dec 18, 2024
2 parents d51ab52 + d9e0e00 commit d2cead5
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
115 changes: 115 additions & 0 deletions upgrade/php/ps_900_migrate_category_images.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?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 license@prestashop.com 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 <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

use PrestaShop\Module\AutoUpgrade\DbWrapper;

/**
* @return void
*
* @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException
*/
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 = DbWrapper::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 miniature, we don't want people
* to end up with no miniature. We will copy the cover
* as the miniature. If the destination file already exists,
* it will be overwritten.
*/
if ($hasCover && !$hasMiniature) {
@copy($categoryCoverPath, $categoryMiniaturePath);
continue;
}

/*
* If it has a miniature 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);
}
}
}
6 changes: 5 additions & 1 deletion upgrade/sql/9.0.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,11 @@ DELETE FROM `PREFIX_hook_module_exceptions` WHERE `id_hook` NOT IN (SELECT id_ho
/* PHP:add_column('feature_value', 'position', 'int(10) unsigned NOT NULL DEFAULT \'0\''); */;
INSERT INTO `PREFIX_configuration` (`name`, `value`, `date_add`, `date_upd`) VALUES ('PS_FEATURE_VALUES_ORDER', 'name', NOW(), NOW());

/* Upgrade attachment champs length */
/* Upgrade attachment names length */
/* https://github.com/PrestaShop/PrestaShop/pull/37598 */
ALTER TABLE `PREFIX_attachment` MODIFY COLUMN `file_name` varchar(255) NOT NULL;
ALTER TABLE `PREFIX_attachment_lang` MODIFY COLUMN `name` varchar(255) DEFAULT NULL;

/* Fix category thumbnail images */
/* https://github.com/PrestaShop/PrestaShop/pull/36877 */
/* PHP:ps_900_migrate_category_images(); */;

0 comments on commit d2cead5

Please sign in to comment.