Skip to content

Commit 9ebd3e0

Browse files
committed
ECP-202 Deprecate Rotation Support in Magento
- Fixed static tests
1 parent 3826de0 commit 9ebd3e0

File tree

5 files changed

+46
-14
lines changed

5 files changed

+46
-14
lines changed

app/code/Magento/Catalog/Model/Product/Image.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
use Magento\Catalog\Model\View\Asset\PlaceholderFactory;
1111
use Magento\Framework\App\Filesystem\DirectoryList;
1212
use Magento\Framework\App\ObjectManager;
13+
use Magento\Framework\Exception\FileSystemException;
1314
use Magento\Framework\Image as MagentoImage;
1415
use Magento\Framework\Serialize\SerializerInterface;
1516
use Magento\Catalog\Model\Product\Image\ParamsBuilder;
17+
use Magento\Framework\Filesystem\Driver\File as FilesystemDriver;
1618

1719
/**
1820
* Image operations
@@ -200,6 +202,11 @@ class Image extends \Magento\Framework\Model\AbstractModel
200202
*/
201203
private $serializer;
202204

205+
/**
206+
* @var FilesystemDriver
207+
*/
208+
private $filesystemDriver;
209+
203210
/**
204211
* Constructor
205212
*
@@ -220,6 +227,8 @@ class Image extends \Magento\Framework\Model\AbstractModel
220227
* @param array $data
221228
* @param SerializerInterface $serializer
222229
* @param ParamsBuilder $paramsBuilder
230+
* @param FilesystemDriver $filesystemDriver
231+
* @throws \Magento\Framework\Exception\FileSystemException
223232
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
224233
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
225234
*/
@@ -240,7 +249,8 @@ public function __construct(
240249
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
241250
array $data = [],
242251
SerializerInterface $serializer = null,
243-
ParamsBuilder $paramsBuilder = null
252+
ParamsBuilder $paramsBuilder = null,
253+
FilesystemDriver $filesystemDriver = null
244254
) {
245255
$this->_storeManager = $storeManager;
246256
$this->_catalogProductMediaConfig = $catalogProductMediaConfig;
@@ -255,6 +265,7 @@ public function __construct(
255265
$this->viewAssetPlaceholderFactory = $viewAssetPlaceholderFactory;
256266
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
257267
$this->paramsBuilder = $paramsBuilder ?: ObjectManager::getInstance()->get(ParamsBuilder::class);
268+
$this->filesystemDriver = $filesystemDriver ?: ObjectManager::getInstance()->get(FilesystemDriver::class);
258269
}
259270

260271
/**
@@ -666,7 +677,12 @@ public function getDestinationSubdir()
666677
public function isCached()
667678
{
668679
$path = $this->imageAsset->getPath();
669-
return is_array($this->loadImageInfoFromCache($path)) || file_exists($path);
680+
try {
681+
$isCached = is_array($this->loadImageInfoFromCache($path)) || $this->filesystemDriver->isExists($path);
682+
} catch (FileSystemException $e) {
683+
$isCached = false;
684+
}
685+
return $isCached;
670686
}
671687

672688
/**

lib/internal/Magento/Framework/Image.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function open()
4949
$this->_adapter->checkDependencies();
5050

5151
if (!file_exists($this->_fileName)) {
52-
throw new \Exception("File '{$this->_fileName}' does not exist.");
52+
throw new \RuntimeException("File '{$this->_fileName}' does not exist.");
5353
}
5454

5555
$this->_adapter->open($this->_fileName);
@@ -95,7 +95,7 @@ public function rotate($angle)
9595
/**
9696
* Crop an image.
9797
*
98-
* @param int $top Default value is 0
98+
* @param int $top Default value is 0
9999
* @param int $left Default value is 0
100100
* @param int $right Default value is 0
101101
* @param int $bottom Default value is 0
@@ -195,7 +195,7 @@ public function quality($value)
195195
* @param int $watermarkImageOpacity Watermark image opacity.
196196
* @param bool $repeat Enable or disable watermark brick.
197197
* @access public
198-
* @throws \Exception
198+
* @throws \RuntimeException
199199
* @return void
200200
*/
201201
public function watermark(
@@ -206,7 +206,7 @@ public function watermark(
206206
$repeat = false
207207
) {
208208
if (!file_exists($watermarkImage)) {
209-
throw new \Exception("Required file '{$watermarkImage}' does not exists.");
209+
throw new \RuntimeException("Required file '{$watermarkImage}' does not exists.");
210210
}
211211
$this->_adapter->watermark($watermarkImage, $positionX, $positionY, $watermarkImageOpacity, $repeat);
212212
}
@@ -233,16 +233,19 @@ public function getImageType()
233233
return $this->_adapter->getImageType();
234234
}
235235

236+
// phpcs:disable Magento2.CodeAnalysis.EmptyBlock
236237
/**
237238
* Process
238239
*
239-
* @access public
240+
* @access public,
240241
* @return void
241242
*/
242243
public function process()
243244
{
244245
}
246+
// phpcs:enable Magento2.CodeAnalysis.EmptyBlock
245247

248+
// phpcs:disable Magento2.CodeAnalysis.EmptyBlock
246249
/**
247250
* Instruction
248251
*
@@ -252,6 +255,7 @@ public function process()
252255
public function instruction()
253256
{
254257
}
258+
// phpcs:enable Magento2.CodeAnalysis.EmptyBlock
255259

256260
/**
257261
* Set image background color

lib/internal/Magento/Framework/Image/Adapter/AbstractAdapter.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ abstract class AbstractAdapter implements AdapterInterface
4141

4242
const POSITION_CENTER = 'center';
4343

44-
/**
45-
* Default font size
46-
*/
4744
const DEFAULT_FONT_SIZE = 15;
4845

4946
/**

lib/internal/Magento/Framework/Image/Adapter/AdapterInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ interface AdapterInterface
2828
public function getColorAt($x, $y);
2929

3030
/**
31+
* Render image and return its binary contents
32+
*
3133
* @see \Magento\Framework\Image\Adapter\AbstractAdapter::getImage
3234
* @return string
3335
*/
@@ -99,6 +101,7 @@ public function crop($top = 0, $left = 0, $right = 0, $bottom = 0);
99101

100102
/**
101103
* Save image to specific path.
104+
*
102105
* If some folders of path does not exist they will be created
103106
*
104107
* @param null|string $destination

lib/internal/Magento/Framework/Image/Adapter/ImageMagick.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
*/
66
namespace Magento\Framework\Image\Adapter;
77

8+
/**
9+
* Wrapper for Imagick image processing PHP Extension.
10+
*
11+
* @link https://www.php.net/manual/en/book.imagick.php
12+
*/
813
class ImageMagick extends \Magento\Framework\Image\Adapter\AbstractAdapter
914
{
1015
/**
@@ -77,7 +82,11 @@ public function open($filename)
7782
try {
7883
$this->_imageHandler = new \Imagick($this->_fileName);
7984
} catch (\ImagickException $e) {
80-
throw new \Exception(sprintf('Unsupported image format. File: %s', $this->_fileName), $e->getCode(), $e);
85+
throw new \RuntimeException(
86+
sprintf('Unsupported image format. File: %s', $this->_fileName),
87+
$e->getCode(),
88+
$e
89+
);
8190
}
8291

8392
$this->backgroundColor();
@@ -86,6 +95,7 @@ public function open($filename)
8695

8796
/**
8897
* Save image to specific path.
98+
*
8999
* If some folders of path does not exist they will be created
90100
*
91101
* @param null|string $destination
@@ -124,6 +134,8 @@ protected function _applyOptions()
124134
}
125135

126136
/**
137+
* Render image binary content and return it.
138+
*
127139
* @see \Magento\Framework\Image\Adapter\AbstractAdapter::getImage
128140
* @return string
129141
*/
@@ -334,7 +346,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
334346
);
335347
}
336348
} catch (\ImagickException $e) {
337-
throw new \Exception('Unable to create watermark.', $e->getCode(), $e);
349+
throw new \RuntimeException('Unable to create watermark.', $e->getCode(), $e);
338350
}
339351

340352
// merge layers
@@ -347,12 +359,12 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
347359
* Checks required dependencies
348360
*
349361
* @return void
350-
* @throws \Exception If some of dependencies are missing
362+
* @throws \RuntimeException If some of dependencies are missing
351363
*/
352364
public function checkDependencies()
353365
{
354366
if (!class_exists('\Imagick', false)) {
355-
throw new \Exception("Required PHP extension 'Imagick' was not loaded.");
367+
throw new \RuntimeException("Required PHP extension 'Imagick' was not loaded.");
356368
}
357369
}
358370

0 commit comments

Comments
 (0)