Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image resize issue #31588

Open
wants to merge 5 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Backend\Model\Image\UploadResizeConfigInterface;

/**
* Class Upload
Expand All @@ -27,6 +28,16 @@ class Upload extends \Magento\Backend\App\Action implements HttpPostActionInterf
*/
protected $resultRawFactory;

/**
* @var \Magento\MediaStorage\Model\File\UploaderFactory
*/
private $uploaderFactory;

/**
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
*/
protected $mediaDirectory;

/**
* @var array
*/
Expand All @@ -52,28 +63,39 @@ class Upload extends \Magento\Backend\App\Action implements HttpPostActionInterf
*/
private $productMediaConfig;

/**
* @var \Magento\Backend\Model\Image\UploadResizeConfigInterface
*/
private $imageUploadConfig;

/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
* @param \Magento\Framework\Image\AdapterFactory $adapterFactory
* @param \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\Framework\Image\AdapterFactory $adapterFactory
* @param \Magento\Catalog\Model\Product\Media\Config $productMediaConfig
* @param UploadResizeConfigInterface $imageUploadConfig
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
\Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
\Magento\Framework\Filesystem $filesystem,
\Magento\Framework\Image\AdapterFactory $adapterFactory = null,
\Magento\Framework\Filesystem $filesystem = null,
\Magento\Catalog\Model\Product\Media\Config $productMediaConfig = null
\Magento\Catalog\Model\Product\Media\Config $productMediaConfig = null,
UploadResizeConfigInterface $imageUploadConfig = null
) {
parent::__construct($context);
$this->resultRawFactory = $resultRawFactory;
$this->uploaderFactory = $uploaderFactory;
$this->mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
$this->adapterFactory = $adapterFactory ?: ObjectManager::getInstance()
->get(\Magento\Framework\Image\AdapterFactory::class);
$this->filesystem = $filesystem ?: ObjectManager::getInstance()
->get(\Magento\Framework\Filesystem::class);
$this->productMediaConfig = $productMediaConfig ?: ObjectManager::getInstance()
->get(\Magento\Catalog\Model\Product\Media\Config::class);
$this->imageUploadConfig = $imageUploadConfig ?: ObjectManager::getInstance()
->get(UploadResizeConfigInterface::class);
}

/**
Expand All @@ -84,20 +106,16 @@ public function __construct(
public function execute()
{
try {
$uploader = $this->_objectManager->create(
\Magento\MediaStorage\Model\File\Uploader::class,
['fileId' => 'image']
);
$uploader = $this->uploaderFactory->create(['fileId' => 'image']);
$uploader->setAllowedExtensions($this->getAllowedExtensions());
$imageAdapter = $this->adapterFactory->create();
$uploader->addValidateCallback('catalog_product_image', $imageAdapter, 'validateUploadFile');
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
$mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
$result = $uploader->save(
$mediaDirectory->getAbsolutePath($this->productMediaConfig->getBaseTmpMediaPath())
$this->mediaDirectory->getAbsolutePath($this->productMediaConfig->getBaseTmpMediaPath())
);

$this->resizeImage($imageAdapter, $result['path'], $result['file']);
$this->_eventManager->dispatch(
'catalog_product_gallery_upload_image_after',
['result' => $result, 'action' => $this]
Expand Down Expand Up @@ -128,4 +146,32 @@ private function getAllowedExtensions()
{
return array_keys($this->allowedMimeTypes);
}

/**
* Resize the image
*
* @param \Magento\Framework\Image\AdapterFactory $imageAdapter
* @param string $path
* @param string $file
* @return bool
*/
private function resizeImage($imageAdapter, $path, $file)
{
try {
# Get width and height of the uploaded image
list($imageWidth, $imageHeight) = getimagesize($path . $file);
if ($this->imageUploadConfig->isResizeEnabled()
&& $imageHeight > $this->imageUploadConfig->getMaxHeight()
|| $imageWidth > $this->imageUploadConfig->getMaxWidth()
) {
$imageAdapter->open($path . $file);
$imageAdapter->keepAspectRatio(true);
$imageAdapter->resize($this->imageUploadConfig->getMaxWidth(), $this->imageUploadConfig->getMaxHeight());
$imageAdapter->save();
}
return true;
} catch (\Exception $e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product\Gallery;

class UploadTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Framework\Controller\Result\RawFactory|MockObject
*/
protected $resultRawFactory;

/**
* @var \Magento\MediaStorage\Model\File\UploaderFactory|MockObject
*/
private $uploaderFactory;

/**
* @var \Magento\Framework\Filesystem\Directory\WriteInterface|MockObject
*/
protected $mediaDirectory;

/**
* @var \Magento\Framework\Image\AdapterFactory|MockObject
*/
private $adapterFactory;

/**
* @var \Magento\Catalog\Model\Product\Media\Config|MockObject
*/
private $productMediaConfig;

/**
* @var \Magento\Backend\Model\Image\UploadResizeConfigInterface|MockObject
*/
private $imageUploadConfig;

protected function setUp(): void
{
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->resultRawFactory = $this->createMock(
\Magento\Framework\Controller\Result\RawFactory::class
);
$this->uploaderFactory = $this->createMock(
\Magento\MediaStorage\Model\File\UploaderFactory::class
);
$filesystem = $this->createMock(
\Magento\Framework\Filesystem::class
);
$this->mediaDirectory = $this->createMock(
\Magento\Framework\Filesystem\Directory\WriteInterface::class
);
$filesystem->expects($this->any())->method('getDirectoryWrite')->willReturn(
$this->mediaDirectory
);
$this->adapterFactory = $this->createMock(
\Magento\Framework\Image\AdapterFactory::class
);
$this->productMediaConfig = $this->createMock(
\Magento\Catalog\Model\Product\Media\Config::class
);
$this->imageUploadConfig = $this->createMock(
\Magento\Backend\Model\Image\UploadResizeConfigInterface::class
);
$this->baseTmpPath = 'base/tmp/';
$this->basePath = 'base/real/';
$this->url = 'http://local.magento2.com/media/tmp/catalog/product/f/i/file.png';
$this->allowedExtensions = [
'jpg' => 'image/jpg',
'jpeg' => 'image/jpeg',
'gif' => 'image/png',
'png' => 'image/gif'
];
$this->result= [
'file' => 'file.png.tmp',
'url' => $this->url
];
$this->fileId = 'file.png';
$this->upload = $objectManager->getObject(
\Magento\Catalog\Controller\Adminhtml\Product\Gallery\Upload::class,
[
'resultRawFactory' => $this->resultRawFactory,
'uploaderFactory' => $this->uploaderFactory,
'mediaDirectory' => $this->mediaDirectory,
'adapterFactory' => $this->adapterFactory,
'productMediaConfig' => $this->productMediaConfig,
'imageUploadConfig' => $this->imageUploadConfig
]
);
}

public function testExecute()
{
$uploader = $this->createMock(\Magento\MediaStorage\Model\File\Uploader::class);
$this->uploaderFactory->expects($this->once())->method('create')->willReturn($uploader);
$uploader->expects($this->once())->method('setAllowedExtensions')->with(array_keys($this->allowedExtensions));
$uploader->expects($this->once())->method('setAllowRenameFiles')->with(true);
$this->productMediaConfig->expects($this->once())->method('getBaseTmpMediaPath')->willReturn($this->baseTmpPath);
$this->mediaDirectory->expects($this->once())->method('getAbsolutePath')->with($this->baseTmpPath)
->willReturn($this->basePath);
$uploader->expects($this->once())->method('save')->with($this->basePath)
->willReturn(['tmp_name' => $this->baseTmpPath, 'file' => $this->fileId, 'path' => $this->basePath]);
$this->productMediaConfig->expects($this->once())->method('getTmpMediaUrl')->willReturn($this->url);
$response = $this->createMock(\Magento\Framework\Controller\Result\Raw::class);
$this->resultRawFactory->expects($this->once())->method('create')->willReturn($response);
$response->expects($this->once())->method('setHeader')->with('Content-type', 'text/plain');
$response->expects($this->once())->method('setContents')->with(json_encode($this->result));
$this->upload->execute();
}
}