-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1060 from Hlavtox/pr-36877
Migrate category images
- Loading branch information
Showing
2 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters