Skip to content

Commit f7a99b4

Browse files
authored
Improved Mage_Catalog_Model_Product_Image::setSize() (#5080)
* Improved `Mage_Catalog_Model_Product_Image::setSize()` * update * phpcsfixer * phpcsfixer
1 parent 853103c commit f7a99b4

File tree

4 files changed

+177
-28
lines changed

4 files changed

+177
-28
lines changed

.phpstan.dist.baseline.neon

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,30 +1008,12 @@ parameters:
10081008
count: 1
10091009
path: app/code/core/Mage/Catalog/Model/Product/Flat/Indexer.php
10101010

1011-
-
1012-
rawMessage: 'Parameter #1 $height of method Mage_Catalog_Model_Product_Image::setHeight() expects int, string given.'
1013-
identifier: argument.type
1014-
count: 1
1015-
path: app/code/core/Mage/Catalog/Model/Product/Image.php
1016-
10171011
-
10181012
rawMessage: 'Parameter #1 $heigth of method Varien_Image::setWatermarkHeigth() expects int, string given.'
10191013
identifier: argument.type
10201014
count: 1
10211015
path: app/code/core/Mage/Catalog/Model/Product/Image.php
10221016

1023-
-
1024-
rawMessage: 'Parameter #1 $size of method Mage_Catalog_Model_Product_Image::setWatermarkSize() expects array, string given.'
1025-
identifier: argument.type
1026-
count: 1
1027-
path: app/code/core/Mage/Catalog/Model/Product/Image.php
1028-
1029-
-
1030-
rawMessage: 'Parameter #1 $width of method Mage_Catalog_Model_Product_Image::setWidth() expects int, string given.'
1031-
identifier: argument.type
1032-
count: 1
1033-
path: app/code/core/Mage/Catalog/Model/Product/Image.php
1034-
10351017
-
10361018
rawMessage: 'Cannot call method isIndexable() on Mage_Eav_Model_Entity_Attribute_Abstract|false.'
10371019
identifier: method.nonObject

app/code/core/Mage/Catalog/Model/Product/Image.php

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
class Mage_Catalog_Model_Product_Image extends Mage_Core_Model_Abstract
1818
{
19+
public const DIMENSIONS_SEPARATOR = 'x';
20+
1921
/**
2022
* Requested width for the scaled image
2123
* @var int
@@ -101,7 +103,7 @@ class Mage_Catalog_Model_Product_Image extends Mage_Core_Model_Abstract
101103
protected static $_baseMediaPath;
102104

103105
/**
104-
* @param int $width
106+
* @param null|int $width
105107
* @return $this
106108
*/
107109
public function setWidth($width)
@@ -119,7 +121,7 @@ public function getWidth()
119121
}
120122

121123
/**
122-
* @param int $height
124+
* @param null|int $height
123125
* @return $this
124126
*/
125127
public function setHeight($height)
@@ -209,18 +211,47 @@ public function setBackgroundColor(array $rgbArray)
209211
}
210212

211213
/**
212-
* @param string $size
214+
* @param string $size in format WIDTHxHEIGHT, WIDTHx, xHEIGHT or single numeric value
213215
* @return $this
214216
*/
215217
public function setSize($size)
216218
{
219+
if (is_numeric($size)) {
220+
$this->setWidth((int) $size)->setHeight((int) $size);
221+
return $this;
222+
}
223+
224+
$width = null;
225+
$height = null;
226+
227+
$endsWithSeparator = false;
228+
$startsWithSeparator = false;
229+
230+
$hasDimensions = str_contains($size, self::DIMENSIONS_SEPARATOR);
231+
232+
if ($hasDimensions) {
233+
$endsWithSeparator = str_ends_with($size, self::DIMENSIONS_SEPARATOR);
234+
$startsWithSeparator = str_starts_with($size, self::DIMENSIONS_SEPARATOR);
235+
}
236+
237+
if ($startsWithSeparator && !$endsWithSeparator) {
238+
$dimension = substr($size, 1);
239+
$height = $dimension ? (int) $dimension : null;
240+
}
241+
242+
if ($endsWithSeparator && !$startsWithSeparator) {
243+
$dimension = substr($size, 0, -1);
244+
$width = $dimension ? (int) $dimension : null;
245+
}
246+
217247
// determine width and height from string
218-
[$width, $height] = explode('x', strtolower($size), 2);
219-
foreach (['width', 'height'] as $wh) {
220-
${$wh} = (int) ${$wh};
221-
if (empty(${$wh})) {
222-
${$wh} = null;
223-
}
248+
if ($hasDimensions && (!$endsWithSeparator && !$startsWithSeparator)) {
249+
[$width, $height] = array_map(
250+
static function ($value) {
251+
return $value === '' ? null : (int) $value;
252+
},
253+
explode(self::DIMENSIONS_SEPARATOR, strtolower($size), 2),
254+
);
224255
}
225256

226257
// set sizes
@@ -327,6 +358,7 @@ protected function _rgbToString($rgbArray)
327358
*
328359
* @param string $file
329360
* @return $this
361+
* @throws Exception
330362
*/
331363
public function setBaseFile($file)
332364
{
@@ -512,11 +544,12 @@ public function setAngle($angle)
512544
*
513545
* @param string $file
514546
* @param string $position
515-
* @param string $size
547+
* @param array $size
516548
* @param int $width
517549
* @param int $heigth
518550
* @param int $imageOpacity
519551
* @return $this
552+
* @throws Exception
520553
*/
521554
public function setWatermark($file, $position = null, $size = null, $width = null, $heigth = null, $imageOpacity = null)
522555
{
@@ -643,6 +676,8 @@ public function getWatermarkFile()
643676
* or false if file not found
644677
*
645678
* @return bool|string
679+
* @throws Mage_Core_Exception
680+
* @throws Mage_Core_Model_Store_Exception
646681
*/
647682
protected function _getWatermarkFilePath()
648683
{
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* @copyright For copyright and license information, read the COPYING.txt file.
5+
* @link /COPYING.txt
6+
* @license Open Software License (OSL 3.0)
7+
* @package OpenMage_Tests
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace OpenMage\Tests\Unit\Mage\Catalog\Model\Product;
13+
14+
use Mage;
15+
use Mage_Catalog_Model_Product_Image as Subject;
16+
use OpenMage\Tests\Unit\OpenMageTest;
17+
use OpenMage\Tests\Unit\Traits\DataProvider\Mage\Catalog\Model\Product\ImageTrait;
18+
19+
final class ImageTest extends OpenMageTest
20+
{
21+
use ImageTrait;
22+
23+
private static Subject $subject;
24+
25+
public static function setUpBeforeClass(): void
26+
{
27+
parent::setUpBeforeClass();
28+
self::$subject = Mage::getModel('catalog/product_image');
29+
}
30+
31+
/**
32+
* @dataProvider provideSetSizeData
33+
* @group Model
34+
*/
35+
public function testSetSize(array $expected, string $value): void
36+
{
37+
self::assertInstanceOf(Subject::class, self::$subject->setSize($value));
38+
self::assertSame($expected['width'], self::$subject->getWidth(), 'Width does not match');
39+
self::assertSame($expected['height'], self::$subject->getHeight(), 'Height does not match');
40+
}
41+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
/**
4+
* @copyright For copyright and license information, read the COPYING.txt file.
5+
* @link /COPYING.txt
6+
* @license Open Software License (OSL 3.0)
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace OpenMage\Tests\Unit\Traits\DataProvider\Mage\Catalog\Model\Product;
12+
13+
use Generator;
14+
15+
trait ImageTrait
16+
{
17+
public function provideSetSizeData(): Generator
18+
{
19+
yield 'size height and width' => [
20+
[
21+
'width' => 200,
22+
'height' => 100,
23+
],
24+
'200x100',
25+
];
26+
27+
yield 'size width' => [
28+
[
29+
'width' => 200,
30+
'height' => null,
31+
],
32+
'200x',
33+
];
34+
35+
yield 'size height' => [
36+
[
37+
'width' => null,
38+
'height' => 100,
39+
],
40+
'x100',
41+
];
42+
43+
yield 'size value "x' => [
44+
[
45+
'width' => null,
46+
'height' => null,
47+
],
48+
'x',
49+
];
50+
51+
yield 'size value "0x' => [
52+
[
53+
'width' => null,
54+
'height' => null,
55+
],
56+
'0x',
57+
];
58+
59+
yield 'size value "x0' => [
60+
[
61+
'width' => null,
62+
'height' => null,
63+
],
64+
'x0',
65+
];
66+
67+
yield 'size value empty' => [
68+
[
69+
'width' => null,
70+
'height' => null,
71+
],
72+
'',
73+
];
74+
75+
yield 'size value numeric' => [
76+
[
77+
'width' => 300,
78+
'height' => 300,
79+
],
80+
'300',
81+
];
82+
83+
yield 'size value non-numeric' => [
84+
[
85+
'width' => null,
86+
'height' => null,
87+
],
88+
'abc',
89+
];
90+
}
91+
}

0 commit comments

Comments
 (0)