Skip to content

Commit f44d6b6

Browse files
authored
Merge pull request #49 from MekDrop/upgrade-intervention-image-to-3
Bump intervention/image to ^3
2 parents 5d0118c + b651210 commit f44d6b6

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
"require": {
66
"php": "^8.3",
77
"smarty/smarty": "^4.0|^5.0",
8-
"intervention/image": "^2.5",
8+
"intervention/image": "^3",
99
"psr/cache": "^1.0|^2.0|^3.0",
10-
"ext-json": "*"
10+
"ext-json": "*",
11+
"ext-gd": "*"
1112
},
1213
"license": "MIT",
1314
"authors": [

src/Functions/ResizeImageFunction.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
use Imponeer\Smarty\Extensions\Image\Exceptions\AttributeMustBeStringException;
99
use Imponeer\Smarty\Extensions\Image\Exceptions\BadFitValueException;
1010
use Imponeer\Smarty\Extensions\Image\Exceptions\RequiredArgumentException;
11-
use Intervention\Image\Image as SingleImage;
12-
use Intervention\Image\ImageManagerStatic as Image;
11+
use Intervention\Image\ImageManager;
12+
use Intervention\Image\Drivers\Gd\Driver;
13+
use Intervention\Image\Interfaces\ImageInterface;
1314
use Override;
1415
use Psr\Cache\CacheItemPoolInterface;
1516
use Psr\Cache\InvalidArgumentException;
@@ -23,14 +24,20 @@
2324
*/
2425
class ResizeImageFunction implements FunctionHandlerInterface
2526
{
27+
private readonly ImageManager $imageManager;
28+
2629
/**
2730
* ResizeImageFunction constructor.
2831
*
29-
* @param CacheItemPoolInterface $cache
32+
* @param CacheItemPoolInterface $cache Cache pool to use for caching images
33+
* @param ImageManager|null $imageManager If custom image manager is needed, it can be specified here, otherwise
34+
* default GD based will be used
3035
*/
3136
public function __construct(
32-
private readonly CacheItemPoolInterface $cache
37+
private readonly CacheItemPoolInterface $cache,
38+
?ImageManager $imageManager = null,
3339
) {
40+
$this->imageManager = $imageManager ?? new ImageManager(new Driver());
3441
}
3542

3643
/**
@@ -75,19 +82,19 @@ protected function fixOtherParams(array &$params): void
7582
* Renders output string
7683
*
7784
* @param string $return Return format
78-
* @param SingleImage $image Image for the output
85+
* @param ImageInterface $image Image for the output
7986
* @param array<string, mixed> $otherParams Other params
8087
*
8188
* @return string
8289
*/
83-
protected function renderOutput(string $return, SingleImage $image, array $otherParams): string
90+
protected function renderOutput(string $return, ImageInterface $image, array $otherParams): string
8491
{
8592
if ($return === 'image') {
8693
return $this->renderImageTag($image, $otherParams);
8794
}
8895

8996
if ($return === 'url') {
90-
return (string) $image->encode('data-url');
97+
return $image->encode()->toDataUri();
9198
}
9299

93100
return '???';
@@ -96,12 +103,12 @@ protected function renderOutput(string $return, SingleImage $image, array $other
96103
/**
97104
* Renders HTML IMG tag for the image
98105
*
99-
* @param SingleImage $image Image to be returned for output
106+
* @param ImageInterface $image Image to be returned for output
100107
* @param array<string, mixed> $otherParams Some params
101108
*
102109
* @return string
103110
*/
104-
protected function renderImageTag(SingleImage $image, array $otherParams): string
111+
protected function renderImageTag(ImageInterface $image, array $otherParams): string
105112
{
106113
$ret = '';
107114

@@ -111,7 +118,7 @@ protected function renderImageTag(SingleImage $image, array $otherParams): strin
111118

112119
$allAttributes = $otherParams + [
113120
'alt' => '',
114-
'src' => (string) $image->encode('data-url')
121+
'src' => $image->encode()->toDataUri()
115122
];
116123

117124
if (isset($allAttributes['link'])) {
@@ -167,35 +174,32 @@ private function buildHTMLTag(string $name, array $attributes, bool $quickCloseT
167174
* @param int|null $width New image width
168175
* @param int|null $height New image height
169176
*
170-
* @return SingleImage
177+
* @return ImageInterface
171178
*/
172-
protected function doResize(string $method, string $file, ?int $width, ?int $height): SingleImage
179+
protected function doResize(string $method, string $file, ?int $width, ?int $height): ImageInterface
173180
{
174-
$image = Image::make($file);
181+
$image = $this->imageManager->read($file);
175182

176183
switch ($method) {
177184
case 'fill':
178185
return $image->resize($width, $height);
179186
case 'inside':
180187
if ($width && $height) {
181188
if ($image->width() > $image->height()) {
182-
$width = null;
183-
} else {
184-
$height = null;
189+
return $image->scale(height: $height);
185190
}
191+
192+
return $image->scale(width: $width);
186193
}
187-
return $image->resize($width, $height, function ($constraint) {
188-
$constraint->aspectRatio();
189-
});
194+
return $image->scale(width: $width, height: $height);
190195
case 'outside':
191-
// For 'outside' fit, we need both width and height
192-
// If one is missing, use the image's original dimensions
193-
$finalWidth = $width ?? $image->width();
194-
$finalHeight = $height ?? $image->height();
195-
return $image->fit($finalWidth, $finalHeight);
196+
return $image->cover(
197+
$width ?? $image->width(),
198+
$height ?? $image->height()
199+
);
200+
default:
201+
return $image;
196202
}
197-
198-
return $image;
199203
}
200204

201205
/**

tests/ResizeImageFunctionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Imponeer\Smarty\Extensions\Image\Exceptions\BadFitValueException;
99
use Imponeer\Smarty\Extensions\Image\Exceptions\RequiredArgumentException;
1010
use Imponeer\Smarty\Extensions\Image\SmartyImageExtension;
11-
use Intervention\Image\Exception\NotReadableException;
11+
use Intervention\Image\Exceptions\DecoderException;
1212
use PHPUnit\Framework\Attributes\DataProvider;
1313
use PHPUnit\Framework\TestCase;
1414
use PHPUnit\Runner\FileDoesNotExistException;
@@ -173,7 +173,7 @@ public function testInvoke(array $attrs): void
173173
($attrs['basedir'] ?? $_SERVER['DOCUMENT_ROOT']) . DIRECTORY_SEPARATOR . $attrs['file']
174174
)
175175
) {
176-
$this->expectException(NotReadableException::class);
176+
$this->expectException(DecoderException::class);
177177
}
178178

179179
$ret = $this->smarty->fetch('eval:urlencode:' . $src);

0 commit comments

Comments
 (0)