Skip to content

Commit ab93d52

Browse files
authored
ENGCOM-7260: Fixes phpcs errors and warnings for Magento\Framework\Image #26036
2 parents ed07e69 + bd3d7eb commit ab93d52

File tree

12 files changed

+141
-58
lines changed

12 files changed

+141
-58
lines changed

dev/tests/static/testsuite/Magento/Test/Php/_files/phpstan/blacklist/common.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# dev/tests/static/framework/bootstrap.php
66
lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php
77
lib/internal/Magento/Framework/Cache/Backend/Eaccelerator.php
8+
lib/internal/Magento/Framework/Image/Adapter/ImageMagick.php
89
dev/tests/integration/framework/deployTestModules.php
910
dev/tests/integration/testsuite/Magento/Framework/Session/ConfigTest.php
1011
dev/tests/integration/testsuite/Magento/Framework/Session/SessionManagerTest.php

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ abstract class AbstractAdapter implements AdapterInterface
2424
*/
2525
public $imageBackgroundColor = 0;
2626

27-
/**
28-
* Position constants
27+
/**#@+
28+
* Position constants.
29+
* Used mainly for watermarks
2930
*/
3031
const POSITION_TOP_LEFT = 'top-left';
3132

@@ -40,9 +41,10 @@ abstract class AbstractAdapter implements AdapterInterface
4041
const POSITION_TILE = 'tile';
4142

4243
const POSITION_CENTER = 'center';
44+
/**#@-*/
4345

4446
/**
45-
* Default font size
47+
* The size of the font to use as default
4648
*/
4749
const DEFAULT_FONT_SIZE = 15;
4850

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/Config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ class Config implements ConfigInterface, UploadConfigInterface
1818

1919
/**
2020
* Config path for the maximal image width value
21-
* @deprecated
21+
* @deprecated Used in a method that is deprecated
2222
*/
2323
const XML_PATH_MAX_WIDTH_IMAGE = 'system/upload_configuration/max_width';
2424

2525
/**
2626
* Config path for the maximal image height value
27-
* @deprecated
27+
* @deprecated Used in a method that is deprecated
2828
*/
2929
const XML_PATH_MAX_HEIGHT_IMAGE = 'system/upload_configuration/max_height';
3030

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@
1212
interface ConfigInterface
1313
{
1414
/**
15+
* Get adapter alias
16+
*
1517
* @return string
1618
*/
1719
public function getAdapterAlias();
1820

1921
/**
22+
* Get adapters
23+
*
2024
* @return array
2125
*/
2226
public function getAdapters();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
1313
*/
14-
class Gd2 extends \Magento\Framework\Image\Adapter\AbstractAdapter
14+
class Gd2 extends AbstractAdapter
1515
{
1616
/**
1717
* Required extensions

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

Lines changed: 114 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
*/
66
namespace Magento\Framework\Image\Adapter;
77

8-
class ImageMagick extends \Magento\Framework\Image\Adapter\AbstractAdapter
8+
use Magento\Framework\Exception\LocalizedException;
9+
10+
/**
11+
* Image adapter from ImageMagick
12+
*/
13+
class ImageMagick extends AbstractAdapter
914
{
1015
/**
1116
* The blur factor where > 1 is blurry, < 1 is sharp
@@ -66,7 +71,7 @@ public function backgroundColor($color = null)
6671
*
6772
* @param string $filename
6873
* @return void
69-
* @throws \Exception
74+
* @throws \Magento\Framework\Exception\LocalizedException
7075
*/
7176
public function open($filename)
7277
{
@@ -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 LocalizedException(
86+
__('Unsupported image format. File: %1', $this->_fileName),
87+
$e,
88+
$e->getCode()
89+
);
8190
}
8291

8392
$this->backgroundColor();
@@ -86,12 +95,13 @@ 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
92102
* @param null|string $newName
93103
* @return void
94-
* @throws \Exception If destination path is not writable
104+
* @throws \Magento\Framework\Exception\LocalizedException If destination path is not writable
95105
*/
96106
public function save($destination = null, $newName = null)
97107
{
@@ -124,6 +134,8 @@ protected function _applyOptions()
124134
}
125135

126136
/**
137+
* Render image and return its binary contents
138+
*
127139
* @see \Magento\Framework\Image\Adapter\AbstractAdapter::getImage
128140
* @return string
129141
*/
@@ -241,7 +253,7 @@ public function crop($top = 0, $left = 0, $right = 0, $bottom = 0)
241253
* @param bool $tile
242254
* @return void
243255
* @throws \LogicException
244-
* @throws \Exception
256+
* @throws \Magento\Framework\Exception\LocalizedException
245257
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
246258
* @SuppressWarnings(PHPMD.NPathComplexity)
247259
*/
@@ -253,28 +265,12 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
253265
$this->_checkCanProcess();
254266

255267
$opacity = $this->getWatermarkImageOpacity() ? $this->getWatermarkImageOpacity() : $opacity;
256-
257268
$opacity = (double)number_format($opacity / 100, 1);
258-
$watermark = new \Imagick($imagePath);
259269

260-
if ($this->getWatermarkWidth() &&
261-
$this->getWatermarkHeight() &&
262-
$this->getWatermarkPosition() != self::POSITION_STRETCH
263-
) {
264-
$watermark->resizeImage(
265-
$this->getWatermarkWidth(),
266-
$this->getWatermarkHeight(),
267-
\Imagick::FILTER_CUBIC,
268-
self::BLUR_FACTOR
269-
);
270-
}
270+
$watermark = new \Imagick($imagePath);
271271

272-
if (method_exists($watermark, 'getImageAlphaChannel')) {
273-
// available from imagick 6.4.0
274-
if ($watermark->getImageAlphaChannel() == 0) {
275-
$watermark->setImageAlphaChannel(\Imagick::ALPHACHANNEL_OPAQUE);
276-
}
277-
}
272+
$this->resizeWatermark($watermark);
273+
$this->handleWatermarkAlphaChannel($watermark);
278274

279275
$compositeChannels = \Imagick::CHANNEL_ALL;
280276
$watermark->evaluateImage(\Imagick::EVALUATE_MULTIPLY, $opacity, \Imagick::CHANNEL_OPACITY);
@@ -307,33 +303,16 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
307303

308304
try {
309305
if ($tile) {
310-
$offsetX = $positionX;
311-
$offsetY = $positionY;
312-
while ($offsetY <= $this->_imageSrcHeight + $watermark->getImageHeight()) {
313-
while ($offsetX <= $this->_imageSrcWidth + $watermark->getImageWidth()) {
314-
$this->_imageHandler->compositeImage(
315-
$watermark,
316-
\Imagick::COMPOSITE_OVER,
317-
$offsetX,
318-
$offsetY,
319-
$compositeChannels
320-
);
321-
$offsetX += $watermark->getImageWidth();
322-
}
323-
$offsetX = $positionX;
324-
$offsetY += $watermark->getImageHeight();
325-
}
306+
$this->addTiledWatermark($positionX, $positionY, $watermark, $compositeChannels);
326307
} else {
327-
$this->_imageHandler->compositeImage(
328-
$watermark,
329-
\Imagick::COMPOSITE_OVER,
330-
$positionX,
331-
$positionY,
332-
$compositeChannels
333-
);
308+
$this->addSingleWatermark($positionX, $positionY, $watermark, $compositeChannels);
334309
}
335310
} catch (\ImagickException $e) {
336-
throw new \Exception('Unable to create watermark.', $e->getCode(), $e);
311+
throw new LocalizedException(
312+
__('Unable to create watermark.'),
313+
$e,
314+
$e->getCode()
315+
);
337316
}
338317

339318
// merge layers
@@ -346,12 +325,14 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
346325
* Checks required dependencies
347326
*
348327
* @return void
349-
* @throws \Exception If some of dependencies are missing
328+
* @throws \Magento\Framework\Exception\LocalizedException If some of dependencies are missing
350329
*/
351330
public function checkDependencies()
352331
{
353-
if (!class_exists('\Imagick', false)) {
354-
throw new \Exception("Required PHP extension 'Imagick' was not loaded.");
332+
if (!class_exists('Imagick', false)) {
333+
throw new LocalizedException(
334+
__('Required PHP extension \'Imagick\' was not loaded.')
335+
);
355336
}
356337
}
357338

@@ -499,4 +480,86 @@ protected function _getImagickPixelObject($color = null)
499480
{
500481
return new \ImagickPixel($color);
501482
}
483+
484+
/**
485+
* Resizes watermark to desired size, when it is not stretched
486+
*
487+
* @param \Imagick $watermark
488+
*/
489+
private function resizeWatermark(\Imagick $watermark): void
490+
{
491+
if ($this->getWatermarkWidth() &&
492+
$this->getWatermarkHeight() &&
493+
$this->getWatermarkPosition() != self::POSITION_STRETCH
494+
) {
495+
$watermark->resizeImage(
496+
$this->getWatermarkWidth(),
497+
$this->getWatermarkHeight(),
498+
\Imagick::FILTER_CUBIC,
499+
self::BLUR_FACTOR
500+
);
501+
}
502+
}
503+
504+
/**
505+
* Keeps transparenty if watermark is transparent
506+
*
507+
* @param \Imagick $watermark
508+
*/
509+
private function handleWatermarkAlphaChannel(\Imagick $watermark): void
510+
{
511+
if (method_exists($watermark, 'getImageAlphaChannel')) {
512+
// available from imagick 6.4.0
513+
if ($watermark->getImageAlphaChannel() == 0) {
514+
$watermark->setImageAlphaChannel(\Imagick::ALPHACHANNEL_OPAQUE);
515+
}
516+
}
517+
}
518+
519+
/**
520+
* Add tiled watermark at starting given X,Y position
521+
*
522+
* @param int $positionX
523+
* @param int $positionY
524+
* @param \Imagick $watermark
525+
* @param bool $compositeChannels
526+
*/
527+
private function addTiledWatermark($positionX, $positionY, \Imagick $watermark, $compositeChannels): void
528+
{
529+
$offsetX = $positionX;
530+
$offsetY = $positionY;
531+
while ($offsetY <= $this->_imageSrcHeight + $watermark->getImageHeight()) {
532+
while ($offsetX <= $this->_imageSrcWidth + $watermark->getImageWidth()) {
533+
$this->_imageHandler->compositeImage(
534+
$watermark,
535+
\Imagick::COMPOSITE_OVER,
536+
$offsetX,
537+
$offsetY,
538+
$compositeChannels
539+
);
540+
$offsetX += $watermark->getImageWidth();
541+
}
542+
$offsetX = $positionX;
543+
$offsetY += $watermark->getImageHeight();
544+
}
545+
}
546+
547+
/**
548+
* Add watermark at given X,Y position
549+
*
550+
* @param int $positionX
551+
* @param int $positionY
552+
* @param \Imagick $watermark
553+
* @param bool $compositeChannels
554+
*/
555+
private function addSingleWatermark($positionX, int $positionY, \Imagick $watermark, bool $compositeChannels): void
556+
{
557+
$this->_imageHandler->compositeImage(
558+
$watermark,
559+
\Imagick::COMPOSITE_OVER,
560+
$positionX,
561+
$positionY,
562+
$compositeChannels
563+
);
564+
}
502565
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
/**
1111
* Interface UploadConfigInterface
12+
*
1213
* @deprecated moved to proper namespace and extended
1314
* @see \Magento\Backend\Model\Image\UploadResizeConfigInterface;
1415
*/

lib/internal/Magento/Framework/Image/AdapterFactory.php

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

8+
/**
9+
* Factory for Adapters that Image Library is using to process images
10+
*/
811
class AdapterFactory
912
{
1013
/**

lib/internal/Magento/Framework/Image/Factory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
use Magento\Framework\ObjectManagerInterface;
1010

11+
/**
12+
* Factory for object to manipulate images
13+
*/
1114
class Factory
1215
{
1316
/**

0 commit comments

Comments
 (0)