88use Imponeer \Smarty \Extensions \Image \Exceptions \AttributeMustBeStringException ;
99use Imponeer \Smarty \Extensions \Image \Exceptions \BadFitValueException ;
1010use 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 ;
1314use Override ;
1415use Psr \Cache \CacheItemPoolInterface ;
1516use Psr \Cache \InvalidArgumentException ;
2324 */
2425class 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 /**
0 commit comments