Skip to content

Commit

Permalink
Merge pull request PrestaShop#21510 from zuk3975/m/product/image-add
Browse files Browse the repository at this point in the history
Introduce ProductImageUploader and AddProductImageCommand [product page migration]
  • Loading branch information
jolelievre authored Nov 20, 2020
2 parents 07aecb4 + cfd4504 commit b4d32d6
Show file tree
Hide file tree
Showing 63 changed files with 2,206 additions and 86 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ tests-legacy/resources/modules/ps_categorytree
tests-legacy/resources/modules/ps_facetedsearch
tests-legacy/resources/modules/ps_sharebuttons

tests/Resources/img/l/*
!tests/Resources/img/l/.gitkeep
tests/Resources/img/p/*
!tests/Resources/img/p/.gitkeep
tests/Resources/img/os/*
!tests/Resources/img/os/.gitkeep
tests/Resources/img/genders/*
!tests/Resources/img/genders/.gitkeep
tests/Resources/img/c/*
!tests/Resources/img/c/.gitkeep
tests/Resources/img/tmp/*
tests/Resources/img/tmp/.gitkeep

/admin-dev/autoupgrade/*
!/admin-dev/autoupgrade/index.php
!/admin-dev/autoupgrade/backup/index.php
Expand Down
8 changes: 4 additions & 4 deletions classes/ImageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ class ImageTypeCore extends ObjectModel
/** @var bool Apply to products */
public $products;

/** @var int Apply to categories */
/** @var bool Apply to categories */
public $categories;

/** @var int Apply to manufacturers */
/** @var bool Apply to manufacturers */
public $manufacturers;

/** @var int Apply to suppliers */
/** @var bool Apply to suppliers */
public $suppliers;

/** @var int Apply to store */
/** @var bool Apply to store */
public $stores;

/**
Expand Down
5 changes: 4 additions & 1 deletion config/defines.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,12 @@
define('_PS_TAASC_PATH_', _PS_TOOL_DIR_.'taasc/');
define('_PS_TCPDF_PATH_', _PS_TOOL_DIR_.'tcpdf/');

define('_PS_IMG_SOURCE_DIR_', _PS_ROOT_DIR_.'/img/');
if (!defined('_PS_IMG_DIR_')) {
define('_PS_IMG_DIR_', _PS_ROOT_DIR_.'/img/');
$dir = (defined('_PS_IN_TEST_') && _PS_IN_TEST_) ? '/tests/Resources/img/' : '/img/';
define('_PS_IMG_DIR_', _PS_ROOT_DIR_.$dir);
}

if (!defined('_PS_HOST_MODE_')) {
define('_PS_CORE_IMG_DIR_', _PS_CORE_DIR_.'/img/');
} else {
Expand Down
107 changes: 107 additions & 0 deletions src/Adapter/Image/ImageGenerator.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)
*/

declare(strict_types=1);

namespace PrestaShop\PrestaShop\Adapter\Image;

use Configuration;
use ImageManager;
use ImageType;
use PrestaShop\PrestaShop\Core\Image\Exception\ImageOptimizationException;
use PrestaShop\PrestaShop\Core\Image\Uploader\Exception\ImageUploadException;
use PrestaShopException;

/**
* Responsible for resizing images based on provided types
*/
class ImageGenerator
{
/**
* @param string $imagePath
* @param ImageType[] $imageTypes
*
* @return bool
*
* @throws ImageOptimizationException
* @throws ImageUploadException
*/
public function generateImagesByTypes(string $imagePath, array $imageTypes): bool
{
$resized = true;

try {
foreach ($imageTypes as $imageType) {
$resized &= $this->resize($imagePath, $imageType);
}
} catch (PrestaShopException $e) {
throw new ImageOptimizationException('Unable to resize one or more of your pictures.');
}

if (!$resized) {
throw new ImageOptimizationException('Unable to resize one or more of your pictures.');
}

return (bool) $resized;
}

/**
* Resizes the image depending on its type
*
* @param string $filePath
* @param ImageType $imageType
*
* @return bool
*/
protected function resize(string $filePath, ImageType $imageType): bool
{
$fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);

if (!is_file($filePath)) {
throw new ImageUploadException(sprintf('File "%s" does not exist', $filePath));
}

//@todo: hardcoded extension as it was in legacy code. Changing it would be a huge BC break.
//@todo: in future we should consider using extension by mimeType
$destinationExtension = '.jpg';
$width = $imageType->width;
$height = $imageType->height;

if (Configuration::get('PS_HIGHT_DPI')) {
$destinationExtension = '2x' . $destinationExtension;
$width *= 2;
$height *= 2;
}

return ImageManager::resize(
$filePath,
sprintf('%s-%s%s', rtrim($filePath, '.' . $fileExtension), stripslashes($imageType->name), $destinationExtension),
$width,
$height,
trim(mime_content_type($filePath), 'image/')
);
}
}
98 changes: 98 additions & 0 deletions src/Adapter/Image/ImageValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?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)
*/

declare(strict_types=1);

namespace PrestaShop\PrestaShop\Adapter\Image;

use ImageManager;
use ImageManagerCore;
use PrestaShop\PrestaShop\Core\Image\Uploader\Exception\ImageFileNotFoundException;
use PrestaShop\PrestaShop\Core\Image\Uploader\Exception\ImageUploadException;
use PrestaShop\PrestaShop\Core\Image\Uploader\Exception\MemoryLimitException;
use PrestaShop\PrestaShop\Core\Image\Uploader\Exception\UploadedImageConstraintException;
use Tools;

/**
* Responsible for validating image before upload
*/
class ImageValidator
{
/**
* @var int
*/
private $maxUploadSize;

/**
* @param int|null $maxUploadSize
*/
public function __construct(?int $maxUploadSize = null)
{
$this->maxUploadSize = $maxUploadSize ?: Tools::getMaxUploadSize();
}

/**
* @param string $filePath
*
* @throws ImageUploadException
* @throws UploadedImageConstraintException
*/
public function assertFileUploadLimits(string $filePath): void
{
$size = filesize($filePath);

if ($this->maxUploadSize > 0 && $size > $this->maxUploadSize) {
throw new UploadedImageConstraintException(sprintf('Max file size allowed is "%s" bytes. Uploaded image size is "%s".', $this->maxUploadSize, $size), UploadedImageConstraintException::EXCEEDED_SIZE);
}

if (!ImageManager::checkImageMemoryLimit($filePath)) {
throw new MemoryLimitException('Cannot upload image due to memory restrictions');
}
}

/**
* @param string $filePath
* @param array $allowedMimeTypes
*
* @throws ImageUploadException
* @throws UploadedImageConstraintException
*/
public function assertIsValidImageType(string $filePath, array $allowedMimeTypes = null): void
{
if (!$allowedMimeTypes) {
$allowedMimeTypes = ImageManagerCore::MIME_TYPE_SUPPORTED;
}

if (!is_file($filePath)) {
throw new ImageFileNotFoundException(sprintf('Image file "%s" not found', $filePath));
}

$mime = mime_content_type($filePath);
if (!ImageManager::isRealImage($filePath, $mime, $allowedMimeTypes)) {
throw new UploadedImageConstraintException(sprintf('Image type "%s" is not allowed, allowed types are: %s', $mime, implode(',', $allowedMimeTypes)), UploadedImageConstraintException::UNRECOGNIZED_FORMAT);
}
}
}
10 changes: 3 additions & 7 deletions src/Adapter/Image/Uploader/AbstractImageUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@
use Configuration;
use ImageManager;
use ImageType;
use PrestaShop\PrestaShop\Core\Image\Uploader\Exception\ImageOptimizationException;
use PrestaShop\PrestaShop\Core\Image\Exception\ImageOptimizationException;
use PrestaShop\PrestaShop\Core\Image\Uploader\Exception\ImageUploadException;
use PrestaShop\PrestaShop\Core\Image\Uploader\Exception\MemoryLimitException;
use PrestaShop\PrestaShop\Core\Image\Uploader\Exception\UploadedImageConstraintException;
use PrestaShop\PrestaShop\Core\Image\Uploader\ImageUploaderInterface;
use PrestaShopException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Tools;
Expand All @@ -43,11 +42,9 @@
*
* @internal
*/
abstract class AbstractImageUploader implements ImageUploaderInterface
abstract class AbstractImageUploader
{
/**
* Check if image is allowed to be uploaded.
*
* @param UploadedFile $image
*
* @throws UploadedImageConstraintException
Expand Down Expand Up @@ -134,7 +131,6 @@ protected function generateDifferentSize($id, $imageDir, $belongsTo)
} catch (PrestaShopException $e) {
throw new ImageOptimizationException('Unable to resize one or more of your pictures.');
}

if (!$resized) {
throw new ImageOptimizationException('Unable to resize one or more of your pictures.');
}
Expand All @@ -143,7 +139,7 @@ protected function generateDifferentSize($id, $imageDir, $belongsTo)
}

/**
* Resizes the image depending from its type
* Resizes the image depending on its type
*
* @param int $id
* @param string $imageDir
Expand Down
5 changes: 3 additions & 2 deletions src/Adapter/Image/Uploader/CategoryCoverImageUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,19 @@
use Category;
use ImageManager;
use ImageType;
use PrestaShop\PrestaShop\Core\Image\Uploader\Exception\ImageOptimizationException;
use PrestaShop\PrestaShop\Core\Image\Exception\ImageOptimizationException;
use PrestaShop\PrestaShop\Core\Image\Uploader\Exception\ImageUploadException;
use PrestaShop\PrestaShop\Core\Image\Uploader\Exception\MemoryLimitException;
use PrestaShop\PrestaShop\Core\Image\Uploader\Exception\UploadedImageConstraintException;
use PrestaShop\PrestaShop\Core\Image\Uploader\ImageUploaderInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* Class CategoryCoverImageUploader.
*
* @internal
*/
final class CategoryCoverImageUploader extends AbstractImageUploader
final class CategoryCoverImageUploader extends AbstractImageUploader implements ImageUploaderInterface
{
/**
* {@inheritdoc}
Expand Down
5 changes: 3 additions & 2 deletions src/Adapter/Image/Uploader/CategoryThumbnailImageUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@

use ImageManager;
use ImageType;
use PrestaShop\PrestaShop\Core\Image\Uploader\Exception\ImageOptimizationException;
use PrestaShop\PrestaShop\Core\Image\Exception\ImageOptimizationException;
use PrestaShop\PrestaShop\Core\Image\Uploader\Exception\ImageUploadException;
use PrestaShop\PrestaShop\Core\Image\Uploader\Exception\UploadedImageConstraintException;
use PrestaShop\PrestaShop\Core\Image\Uploader\ImageUploaderInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* Class CategoryThumbnailImageUploader.
*/
final class CategoryThumbnailImageUploader extends AbstractImageUploader
final class CategoryThumbnailImageUploader extends AbstractImageUploader implements ImageUploaderInterface
{
/**
* {@inheritdoc}
Expand Down
3 changes: 2 additions & 1 deletion src/Adapter/Image/Uploader/EmployeeImageUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
namespace PrestaShop\PrestaShop\Adapter\Image\Uploader;

use Employee;
use PrestaShop\PrestaShop\Core\Image\Uploader\ImageUploaderInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* Uploads employee logo image
*/
final class EmployeeImageUploader extends AbstractImageUploader
final class EmployeeImageUploader extends AbstractImageUploader implements ImageUploaderInterface
{
/**
* @var string
Expand Down
5 changes: 3 additions & 2 deletions src/Adapter/Image/Uploader/ManufacturerImageUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@
use Configuration;
use ImageManager;
use ImageType;
use PrestaShop\PrestaShop\Core\Image\Uploader\Exception\ImageOptimizationException;
use PrestaShop\PrestaShop\Core\Image\Exception\ImageOptimizationException;
use PrestaShop\PrestaShop\Core\Image\Uploader\Exception\ImageUploadException;
use PrestaShop\PrestaShop\Core\Image\Uploader\Exception\MemoryLimitException;
use PrestaShop\PrestaShop\Core\Image\Uploader\ImageUploaderInterface;
use PrestaShopException;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* Uploads manufacturer logo image
*/
final class ManufacturerImageUploader extends AbstractImageUploader
final class ManufacturerImageUploader extends AbstractImageUploader implements ImageUploaderInterface
{
/**
* {@inheritdoc}
Expand Down
Loading

0 comments on commit b4d32d6

Please sign in to comment.