Skip to content

Commit 7f52175

Browse files
committed
remove multiple cloning in Image::resize()
1 parent 81e0a74 commit 7f52175

1 file changed

Lines changed: 10 additions & 28 deletions

File tree

src/Image.php

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace TraderInteractive\Util;
44

5+
use Imagick;
56
use InvalidArgumentException;
67
use TraderInteractive\Util;
78

@@ -56,9 +57,16 @@ final class Image
5657
];
5758

5859
/**
59-
* Calls @see resizeMulti() with $boxWidth and $boxHeight as a single element in $boxSizes
60+
* @param Imagick $source The image magick object to resize
61+
* @param int $boxWidth The final width of the image.
62+
* @param int $boxHeight The final height of the image.
63+
* @param array $options Options for the resize operation.
64+
*
65+
* @return Imagick
66+
*
67+
* @throws \Exception Thrown if options are invalid.
6068
*/
61-
public static function resize(\Imagick $source, int $boxWidth, int $boxHeight, array $options = []) : \Imagick
69+
public static function resize(Imagick $source, int $boxWidth, int $boxHeight, array $options = []) : Imagick
6270
{
6371
$options += self::DEFAULT_OPTIONS;
6472

@@ -111,8 +119,6 @@ public static function resize(\Imagick $source, int $boxWidth, int $boxHeight, a
111119
throw new InvalidArgumentException('a $boxSizes height was not between 0 and $options["maxHeight"]');
112120
}
113121

114-
$results = [];
115-
$cloneCache = [];
116122
$clone = clone $source;
117123

118124
self::rotateImage($clone);
@@ -153,47 +159,31 @@ public static function resize(\Imagick $source, int $boxWidth, int $boxHeight, a
153159
//width and height
154160
while (true) {
155161
$widthReduced = false;
156-
$widthIsHalf = false;
157162
if ($width > $targetWidth) {
158163
$width = (int)($width / 2);
159164
$widthReduced = true;
160-
$widthIsHalf = true;
161165
if ($width < $targetWidth) {
162166
$width = $targetWidth;
163-
$widthIsHalf = false;
164167
}
165168
}
166169

167170
$heightReduced = false;
168-
$heightIsHalf = false;
169171
if ($height > $targetHeight) {
170172
$height = (int)($height / 2);
171173
$heightReduced = true;
172-
$heightIsHalf = true;
173174
if ($height < $targetHeight) {
174175
$height = $targetHeight;
175-
$heightIsHalf = false;
176176
}
177177
}
178178

179179
if (!$widthReduced && !$heightReduced) {
180180
break;
181181
}
182182

183-
$cacheKey = "{$width}x{$height}";
184-
if (isset($cloneCache[$cacheKey])) {
185-
$clone = clone $cloneCache[$cacheKey];
186-
continue;
187-
}
188-
189183
if ($clone->resizeImage($width, $height, \Imagick::FILTER_BOX, 1.0) !== true) {
190184
//cumbersome to test
191185
throw new \Exception('Imagick::resizeImage() did not return true');//@codeCoverageIgnore
192186
}
193-
194-
if ($widthIsHalf && $heightIsHalf) {
195-
$cloneCache[$cacheKey] = clone $clone;
196-
}
197187
}
198188

199189
if ($upsize && ($width < $targetWidth || $height < $targetHeight)) {
@@ -204,10 +194,6 @@ public static function resize(\Imagick $source, int $boxWidth, int $boxHeight, a
204194
}
205195

206196
if ($clone->getImageHeight() === $boxHeight && $clone->getImageWidth() === $boxWidth) {
207-
foreach ($cloneCache as $cachedClone) {
208-
$cachedClone->destroy();
209-
}
210-
211197
return $clone;
212198
}
213199

@@ -221,10 +207,6 @@ public static function resize(\Imagick $source, int $boxWidth, int $boxHeight, a
221207
//reason we are not supporting the options in self::write() here is because format, and strip headers are
222208
//only relevant once written Imagick::stripImage() doesnt even have an effect until written
223209
//also the user can just call that function with the resultant $canvas
224-
foreach ($cloneCache as $clone) {
225-
$clone->destroy();
226-
}
227-
228210
return $canvas;
229211
}
230212

0 commit comments

Comments
 (0)