Skip to content

Commit e66b7ed

Browse files
committed
MAGETWO-63599: [GitHub] catalog:images:resize = getimagesize(): Read error! in vendor/magento/module-catalog/Model/Product/Image.php on line 410 if an image is 0 bytes #8204
- Added checks for zero byte files
1 parent 4a28abb commit e66b7ed

File tree

4 files changed

+64
-19
lines changed

4 files changed

+64
-19
lines changed

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Framework\Image\Adapter;
710

811
use Magento\Framework\App\Filesystem\DirectoryList;
912

1013
/**
14+
* Image abstract adapter
15+
*
1116
* @file Abstract.php
1217
* @author Magento Core Team <[email protected]>
1318
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -169,6 +174,7 @@ abstract public function open($fileName);
169174

170175
/**
171176
* Save image to specific path.
177+
*
172178
* If some folders of path does not exist they will be created
173179
*
174180
* @param null|string $destination
@@ -301,7 +307,7 @@ public function getImageType()
301307
if ($this->_fileType) {
302308
return $this->_fileType;
303309
} else {
304-
if ($this->_canProcess()) {
310+
if ($this->_canProcess() && filesize($this->_fileName) > 0) {
305311
list($this->_imageSrcWidth, $this->_imageSrcHeight, $this->_fileType) = getimagesize($this->_fileName);
306312
return $this->_fileType;
307313
}
@@ -620,7 +626,7 @@ protected function _checkDimensions($frameWidth, $frameHeight)
620626
$frameHeight !== null && $frameHeight <= 0 ||
621627
empty($frameWidth) && empty($frameHeight)
622628
) {
623-
throw new \Exception('Invalid image dimensions.');
629+
throw new \InvalidArgumentException('Invalid image dimensions.');
624630
}
625631
}
626632

@@ -687,7 +693,9 @@ protected function _prepareDestination($destination = null, $newName = null)
687693
$this->directoryWrite->create($this->directoryWrite->getRelativePath($destination));
688694
} catch (\Magento\Framework\Exception\FileSystemException $e) {
689695
$this->logger->critical($e);
690-
throw new \Exception('Unable to write file into directory ' . $destination . '. Access forbidden.');
696+
throw new \DomainException(
697+
'Unable to write file into directory ' . $destination . '. Access forbidden.'
698+
);
691699
}
692700
}
693701

@@ -710,12 +718,19 @@ protected function _canProcess()
710718
* @param string $filePath
711719
* @return bool
712720
* @throws \InvalidArgumentException
721+
* @throws \DomainException
722+
* @throws \BadFunctionCallException
723+
* @throws \RuntimeException
724+
* @throws \OverflowException
713725
*/
714726
public function validateUploadFile($filePath)
715727
{
716728
if (!file_exists($filePath)) {
717729
throw new \InvalidArgumentException("File '{$filePath}' does not exists.");
718730
}
731+
if (filesize($filePath) === 0) {
732+
throw new \InvalidArgumentException('Wrong file size.');
733+
}
719734
if (!getimagesize($filePath)) {
720735
throw new \InvalidArgumentException('Disallowed file type.');
721736
}

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Framework\Image\Adapter;
710

811
/**
@@ -59,6 +62,9 @@ protected function _reset()
5962
*/
6063
public function open($filename)
6164
{
65+
if (filesize($filename) === 0) {
66+
throw new \InvalidArgumentException("Wrong file size: '{$filename}'.");
67+
}
6268
$this->_fileName = $filename;
6369
$this->_reset();
6470
$this->getMimeType();
@@ -225,18 +231,19 @@ public function getImage()
225231
* @param null|int $fileType
226232
* @param string $unsupportedText
227233
* @return string
228-
* @throws \Exception
234+
* @throws \InvalidArgumentException
235+
* @throws \BadFunctionCallException
229236
*/
230237
private function _getCallback($callbackType, $fileType = null, $unsupportedText = 'Unsupported image format.')
231238
{
232239
if (null === $fileType) {
233240
$fileType = $this->_fileType;
234241
}
235242
if (empty(self::$_callbacks[$fileType])) {
236-
throw new \Exception($unsupportedText);
243+
throw new \InvalidArgumentException($unsupportedText);
237244
}
238245
if (empty(self::$_callbacks[$fileType][$callbackType])) {
239-
throw new \Exception('Callback not found.');
246+
throw new \BadFunctionCallException('Callback not found.');
240247
}
241248
return self::$_callbacks[$fileType][$callbackType];
242249
}
@@ -248,7 +255,7 @@ private function _getCallback($callbackType, $fileType = null, $unsupportedText
248255
*
249256
* @param resource &$imageResourceTo
250257
* @return int
251-
* @throws \Exception
258+
* @throws \InvalidArgumentException
252259
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
253260
*/
254261
private function _fillBackgroundColor(&$imageResourceTo)
@@ -261,17 +268,17 @@ private function _fillBackgroundColor(&$imageResourceTo)
261268
// fill truecolor png with alpha transparency
262269
if ($isAlpha) {
263270
if (!imagealphablending($imageResourceTo, false)) {
264-
throw new \Exception('Failed to set alpha blending for PNG image.');
271+
throw new \InvalidArgumentException('Failed to set alpha blending for PNG image.');
265272
}
266273
$transparentAlphaColor = imagecolorallocatealpha($imageResourceTo, 0, 0, 0, 127);
267274
if (false === $transparentAlphaColor) {
268-
throw new \Exception('Failed to allocate alpha transparency for PNG image.');
275+
throw new \InvalidArgumentException('Failed to allocate alpha transparency for PNG image.');
269276
}
270277
if (!imagefill($imageResourceTo, 0, 0, $transparentAlphaColor)) {
271-
throw new \Exception('Failed to fill PNG image with alpha transparency.');
278+
throw new \InvalidArgumentException('Failed to fill PNG image with alpha transparency.');
272279
}
273280
if (!imagesavealpha($imageResourceTo, true)) {
274-
throw new \Exception('Failed to save alpha transparency into PNG image.');
281+
throw new \InvalidArgumentException('Failed to save alpha transparency into PNG image.');
275282
}
276283

277284
return $transparentAlphaColor;
@@ -283,22 +290,22 @@ private function _fillBackgroundColor(&$imageResourceTo)
283290
$transparentColor = imagecolorallocate($imageResourceTo, $r, $g, $b);
284291
}
285292
if (false === $transparentColor) {
286-
throw new \Exception('Failed to allocate transparent color for image.');
293+
throw new \InvalidArgumentException('Failed to allocate transparent color for image.');
287294
}
288295
if (!imagefill($imageResourceTo, 0, 0, $transparentColor)) {
289-
throw new \Exception('Failed to fill image with transparency.');
296+
throw new \InvalidArgumentException('Failed to fill image with transparency.');
290297
}
291298
imagecolortransparent($imageResourceTo, $transparentColor);
292299
return $transparentColor;
293300
}
294301
} catch (\Exception $e) {
295-
// fallback to default background color
302+
throw new \DomainException('Failed to fill image.');
296303
}
297304
}
298305
list($r, $g, $b) = $this->_backgroundColor;
299306
$color = imagecolorallocate($imageResourceTo, $r, $g, $b);
300307
if (!imagefill($imageResourceTo, 0, 0, $color)) {
301-
throw new \Exception("Failed to fill image background with color {$r} {$g} {$b}.");
308+
throw new \InvalidArgumentException("Failed to fill image background with color {$r} {$g} {$b}.");
302309
}
303310

304311
return $color;
@@ -637,13 +644,13 @@ public function crop($top = 0, $left = 0, $right = 0, $bottom = 0)
637644
* Checks required dependencies
638645
*
639646
* @return void
640-
* @throws \Exception If some of dependencies are missing
647+
* @throws \RuntimeException If some of dependencies are missing
641648
*/
642649
public function checkDependencies()
643650
{
644651
foreach ($this->_requiredExtensions as $value) {
645652
if (!extension_loaded($value)) {
646-
throw new \Exception("Required PHP extension '{$value}' was not loaded.");
653+
throw new \RuntimeException("Required PHP extension '{$value}' was not loaded.");
647654
}
648655
}
649656
}
@@ -755,7 +762,7 @@ protected function _createImageFromText($text)
755762
* @param string $text
756763
* @param string $font
757764
* @return void
758-
* @throws \Exception
765+
* @throws \InvalidArgumentException
759766
*/
760767
protected function _createImageFromTtfText($text, $font)
761768
{
@@ -777,7 +784,7 @@ protected function _createImageFromTtfText($text, $font)
777784
$text
778785
);
779786
if ($result === false) {
780-
throw new \Exception('Unable to create TTF text');
787+
throw new \InvalidArgumentException('Unable to create TTF text');
781788
}
782789
}
783790

lib/internal/Magento/Framework/Image/Test/Unit/Adapter/Gd2Test.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Framework\Image\Test\Unit\Adapter;
710

811
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
@@ -24,6 +27,13 @@ class Gd2Test extends \PHPUnit\Framework\TestCase
2427
*/
2528
public static $imageData = [];
2629

30+
/**
31+
* Simulation of filesize() function
32+
*
33+
* @var int
34+
*/
35+
public static $imageSize = 1;
36+
2737
/**
2838
* Adapter for testing
2939
* @var \Magento\Framework\Image\Adapter\Gd2

lib/internal/Magento/Framework/Image/Test/Unit/Adapter/_files/global_php_mock.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Framework\Image\Adapter;
710

811
use Magento\Framework\Image\Test\Unit\Adapter\Gd2Test;
@@ -35,6 +38,16 @@ function getimagesize($file)
3538
return Gd2Test::$imageData;
3639
}
3740

41+
/**
42+
* @param $file
43+
* @return mixed
44+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
45+
*/
46+
function filesize($file)
47+
{
48+
return Gd2Test::$imageSize;
49+
}
50+
3851
/**
3952
* @param $real
4053
* @return int

0 commit comments

Comments
 (0)