1515
1616use CodeIgniter \Images \Exceptions \ImageException ;
1717use Config \Images ;
18+ use GdImage ;
1819
1920/**
2021 * Image handler for GD package
22+ *
23+ * @extends BaseHandler<GdImage>
2124 */
2225class GDHandler extends BaseHandler
2326{
2427 /**
25- * Constructor.
26- *
2728 * @param Images|null $config
2829 *
2930 * @throws ImageException
@@ -43,17 +44,13 @@ public function __construct($config = null)
4344 */
4445 protected function _rotate (int $ angle ): bool
4546 {
46- // Create the image handle
4747 $ srcImg = $ this ->createImage ();
4848
49- // Set the background color
5049 // This won't work with transparent PNG files so we are
5150 // going to have to figure out how to determine the color
5251 // of the alpha channel in a future release.
53-
5452 $ white = imagecolorallocate ($ srcImg , 255 , 255 , 255 );
5553
56- // Rotate it!
5754 $ destImg = imagerotate ($ srcImg , $ angle , $ white );
5855
5956 $ this ->resource = $ destImg ;
@@ -110,23 +107,23 @@ protected function _flip(string $direction)
110107 /**
111108 * Get GD version
112109 *
113- * @return mixed
110+ * @return string
114111 */
115112 public function getVersion ()
116113 {
117- if (function_exists ('gd_info ' )) {
118- $ gdVersion = @gd_info ();
119-
120- return preg_replace ('/\D/ ' , '' , $ gdVersion ['GD Version ' ]);
114+ if (! function_exists ('gd_info ' )) {
115+ return '' ; // @codeCoverageIgnore
121116 }
122117
123- return false ;
118+ $ gdVersion = @gd_info ();
119+
120+ return preg_replace ('/\D/ ' , '' , $ gdVersion ['GD Version ' ]) ?? '' ;
124121 }
125122
126123 /**
127124 * Resizes the image.
128125 *
129- * @return GDHandler
126+ * @return $this
130127 */
131128 public function _resize (bool $ maintainRatio = false )
132129 {
@@ -136,7 +133,7 @@ public function _resize(bool $maintainRatio = false)
136133 /**
137134 * Crops the image.
138135 *
139- * @return GDHandler
136+ * @return $this
140137 */
141138 public function _crop ()
142139 {
@@ -154,7 +151,6 @@ protected function process(string $action)
154151 $ origHeight = $ this ->image ()->origHeight ;
155152
156153 if ($ action === 'crop ' ) {
157- // Reassign the source width/height if cropping
158154 $ origWidth = $ this ->width ;
159155 $ origHeight = $ this ->height ;
160156
@@ -165,7 +161,6 @@ protected function process(string $action)
165161 $ this ->image ()->origWidth = $ this ->width ;
166162 }
167163
168- // Create the image handle
169164 $ src = $ this ->createImage ();
170165
171166 if (function_exists ('imagecreatetruecolor ' )) {
@@ -178,7 +173,6 @@ protected function process(string $action)
178173
179174 $ dest = $ create ($ this ->width , $ this ->height );
180175
181- // for png and webp we can actually preserve transparency
182176 if (in_array ($ this ->image ()->imageType , $ this ->supportTransparency , true )) {
183177 imagealphablending ($ dest , false );
184178 imagesavealpha ($ dest , true );
@@ -209,7 +203,7 @@ public function save(?string $target = null, int $quality = 90): bool
209203
210204 // If no new resource has been created, then we're
211205 // simply copy the existing one.
212- if (empty ( $ this ->resource ) && $ quality === 100 ) {
206+ if ($ this ->resource === null && $ quality === 100 ) {
213207 if ($ original === null ) {
214208 return true ;
215209 }
@@ -222,7 +216,6 @@ public function save(?string $target = null, int $quality = 90): bool
222216
223217 $ this ->ensureResource ();
224218
225- // for png and webp we can actually preserve transparency
226219 if (in_array ($ this ->image ()->imageType , $ this ->supportTransparency , true )) {
227220 imagepalettetotruecolor ($ this ->resource );
228221 imagealphablending ($ this ->resource , false );
@@ -284,10 +277,7 @@ public function save(?string $target = null, int $quality = 90): bool
284277 /**
285278 * Create Image Resource
286279 *
287- * This simply creates an image resource handle
288- * based on the type of image being processed
289- *
290- * @return bool|resource
280+ * @return GdImage
291281 */
292282 protected function createImage (string $ path = '' , string $ imageType = '' )
293283 {
@@ -312,7 +302,6 @@ protected function createImage(string $path = '', string $imageType = '')
312302 protected function ensureResource ()
313303 {
314304 if ($ this ->resource === null ) {
315- // if valid image type, make corresponding image resource
316305 $ this ->resource = $ this ->getImageResource (
317306 $ this ->image ()->getPathname (),
318307 $ this ->image ()->imageType ,
@@ -326,7 +315,7 @@ protected function ensureResource()
326315 * @param string $path Image path
327316 * @param int $imageType Image type
328317 *
329- * @return bool|resource
318+ * @return GdImage
330319 *
331320 * @throws ImageException
332321 */
@@ -338,32 +327,42 @@ protected function getImageResource(string $path, int $imageType)
338327 throw ImageException ::forInvalidImageCreate (lang ('Images.gifNotSupported ' ));
339328 }
340329
341- return imagecreatefromgif ($ path );
330+ $ resource = imagecreatefromgif ($ path );
331+ break ;
342332
343333 case IMAGETYPE_JPEG :
344334 if (! function_exists ('imagecreatefromjpeg ' )) {
345335 throw ImageException ::forInvalidImageCreate (lang ('Images.jpgNotSupported ' ));
346336 }
347337
348- return imagecreatefromjpeg ($ path );
338+ $ resource = imagecreatefromjpeg ($ path );
339+ break ;
349340
350341 case IMAGETYPE_PNG :
351342 if (! function_exists ('imagecreatefrompng ' )) {
352343 throw ImageException ::forInvalidImageCreate (lang ('Images.pngNotSupported ' ));
353344 }
354345
355- return @imagecreatefrompng ($ path );
346+ $ resource = @imagecreatefrompng ($ path );
347+ break ;
356348
357349 case IMAGETYPE_WEBP :
358350 if (! function_exists ('imagecreatefromwebp ' )) {
359351 throw ImageException ::forInvalidImageCreate (lang ('Images.webpNotSupported ' ));
360352 }
361353
362- return imagecreatefromwebp ($ path );
354+ $ resource = imagecreatefromwebp ($ path );
355+ break ;
363356
364357 default :
365358 throw ImageException ::forInvalidImageCreate ('Ima ' );
366359 }
360+
361+ if ($ resource === false ) {
362+ throw ImageException ::forInvalidImageCreate ($ path ); // @codeCoverageIgnore
363+ }
364+
365+ return $ resource ;
367366 }
368367
369368 /**
@@ -386,10 +385,7 @@ protected function _text(string $text, array $options = [])
386385 $ options ['hOffset ' ] *= -1 ;
387386 }
388387
389- // Set font width and height
390- // These are calculated differently depending on
391- // whether we are using the true type font or not
392- if (! empty ($ options ['fontPath ' ])) {
388+ if (($ options ['fontPath ' ] ?? '' ) !== '' ) {
393389 if (function_exists ('imagettfbbox ' )) {
394390 $ temp = imagettfbbox ($ options ['fontSize ' ], 0 , $ options ['fontPath ' ], $ text );
395391 $ temp = $ temp [2 ] - $ temp [0 ];
@@ -408,19 +404,16 @@ protected function _text(string $text, array $options = [])
408404 $ options ['fontheight ' ] = $ fontheight ;
409405 $ options ['fontwidth ' ] = $ fontwidth ;
410406
411- // Set base X and Y axis values
412407 $ xAxis = $ options ['hOffset ' ] + $ options ['padding ' ];
413408 $ yAxis = $ options ['vOffset ' ] + $ options ['padding ' ];
414409
415- // Set vertical alignment
416410 if ($ options ['vAlign ' ] === 'middle ' ) {
417411 // Don't apply padding when you're in the middle of the image.
418412 $ yAxis += ($ this ->image ()->origHeight / 2 ) + ($ fontheight / 2 ) - $ options ['padding ' ] - $ fontheight - $ options ['shadowOffset ' ];
419413 } elseif ($ options ['vAlign ' ] === 'bottom ' ) {
420414 $ yAxis = ($ this ->image ()->origHeight - $ fontheight - $ options ['shadowOffset ' ] - ($ fontheight / 2 )) - $ yAxis ;
421415 }
422416
423- // Set horizontal alignment
424417 if ($ options ['hAlign ' ] === 'right ' ) {
425418 $ xAxis += ($ this ->image ()->origWidth - ($ fontwidth * strlen ($ text )) - $ options ['shadowOffset ' ]) - (2 * $ options ['padding ' ]);
426419 } elseif ($ options ['hAlign ' ] === 'center ' ) {
@@ -431,7 +424,6 @@ protected function _text(string $text, array $options = [])
431424 $ options ['yAxis ' ] = $ yAxis ;
432425
433426 if ($ options ['withShadow ' ]) {
434- // Offset from text
435427 $ options ['xShadow ' ] = $ xAxis + $ options ['shadowOffset ' ];
436428 $ options ['yShadow ' ] = $ yAxis + $ options ['shadowOffset ' ];
437429
@@ -444,17 +436,15 @@ protected function _text(string $text, array $options = [])
444436 /**
445437 * Handler-specific method for overlaying text on an image.
446438 *
447- * @param bool $isShadow Whether we are drawing the dropshadow or actual text
439+ * @param array<string, mixed> $options
440+ * @param bool $isShadow Whether we are drawing the dropshadow or actual text
441+ *
442+ * @return void
448443 */
449444 protected function textOverlay (string $ text , array $ options = [], bool $ isShadow = false )
450445 {
451446 $ src = $ this ->createImage ();
452447
453- /* Set RGB values for shadow
454- *
455- * Get the rest of the string and split it into 2-length
456- * hex values:
457- */
458448 $ opacity = (int ) ($ options ['opacity ' ] * 127 );
459449
460450 // Allow opacity to be applied to the text
@@ -473,8 +463,7 @@ protected function textOverlay(string $text, array $options = [], bool $isShadow
473463 $ xAxis = $ isShadow ? $ options ['xShadow ' ] : $ options ['xAxis ' ];
474464 $ yAxis = $ isShadow ? $ options ['yShadow ' ] : $ options ['yAxis ' ];
475465
476- // Add the shadow to the source image
477- if (! empty ($ options ['fontPath ' ])) {
466+ if (($ options ['fontPath ' ] ?? '' ) !== '' ) {
478467 // We have to add fontheight because imagettftext locates the bottom left corner, not top-left corner.
479468 imagettftext ($ src , $ options ['fontSize ' ], 0 , (int ) $ xAxis , (int ) ($ yAxis + $ options ['fontheight ' ]), $ color , $ options ['fontPath ' ], $ text );
480469 } else {
0 commit comments