Skip to content

Commit 5d37684

Browse files
committed
refactor: cleanup the Images library
1 parent 29d26fe commit 5d37684

9 files changed

Lines changed: 49 additions & 131 deletions

File tree

‎phpstan.dist.neon‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ parameters:
1818
- app/Views/errors/html/*
1919
- system/Commands/Generators/Views/*
2020
- system/Debug/Toolbar/Views/toolbar.tpl.php
21-
- system/Images/Handlers/GDHandler.php
2221
- system/Test/Mock/MockCommon.php
2322
- system/ThirdParty/*
2423
- system/Validation/Views/single.php

‎system/Images/Handlers/BaseHandler.php‎

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
use Config\Images;
2121

2222
/**
23-
* Base image handling implementation
23+
* Base image handling implementation.
24+
*
25+
* @template T of object
2426
*/
2527
abstract class BaseHandler implements ImageHandlerInterface
2628
{
@@ -90,7 +92,7 @@ abstract class BaseHandler implements ImageHandlerInterface
9092
/**
9193
* Default options for text watermarking.
9294
*
93-
* @var array
95+
* @var array<string, mixed>
9496
*/
9597
protected $textDefaults = [
9698
'fontPath' => null,
@@ -110,7 +112,7 @@ abstract class BaseHandler implements ImageHandlerInterface
110112
/**
111113
* Image types with support for transparency.
112114
*
113-
* @var array
115+
* @var list<int>
114116
*/
115117
protected $supportTransparency = [
116118
IMAGETYPE_PNG,
@@ -120,7 +122,7 @@ abstract class BaseHandler implements ImageHandlerInterface
120122
/**
121123
* Temporary image used by the different engines.
122124
*
123-
* @var resource|null
125+
* @var T|null
124126
*/
125127
protected $resource;
126128

@@ -193,7 +195,6 @@ protected function image(): Image
193195
throw ImageException::forMissingImage();
194196
}
195197

196-
// Verify the loaded image is an Image instance
197198
if (! $this->image instanceof Image) {
198199
throw ImageException::forInvalidPath();
199200
}
@@ -203,7 +204,6 @@ protected function image(): Image
203204
throw ImageException::forFileNotSupported();
204205
}
205206

206-
// Note that the image has been verified
207207
$this->verified = true;
208208

209209
return $this->image;
@@ -214,7 +214,7 @@ protected function image(): Image
214214
* Good for extending the system or doing things this library
215215
* is not intended to do.
216216
*
217-
* @return resource
217+
* @return T
218218
*/
219219
public function getResource()
220220
{
@@ -246,7 +246,6 @@ public function withResource()
246246
*/
247247
public function resize(int $width, int $height, bool $maintainRatio = false, string $masterDim = 'auto')
248248
{
249-
// If the target width/height match the source, then we have nothing to do here.
250249
if ($this->image()->origWidth === $width && $this->image()->origHeight === $height) {
251250
return $this;
252251
}
@@ -316,28 +315,20 @@ public function convert(int $imageType)
316315
*/
317316
public function rotate(float $angle)
318317
{
319-
// Allowed rotation values
320-
$degs = [
321-
90.0,
322-
180.0,
323-
270.0,
324-
];
318+
$degs = [90.0, 180.0, 270.0];
325319

326320
if (! in_array($angle, $degs, true)) {
327321
throw ImageException::forMissingAngle();
328322
}
329323

330-
// cast angle as an int, for our use
331324
$angle = (int) $angle;
332325

333-
// Reassign the width and height
334326
if ($angle === 90 || $angle === 270) {
335327
$temp = $this->height;
336328
$this->width = $this->height;
337329
$this->height = $temp;
338330
}
339331

340-
// Call the Handler-specific version.
341332
$this->_rotate($angle);
342333

343334
return $this;
@@ -568,8 +559,6 @@ protected function calcAspectRatio($width, $height = null, $origWidth = 0, $orig
568559
throw new InvalidArgumentException('You must supply the parameters: origWidth, origHeight.');
569560
}
570561

571-
// If $height is null, then we have it easy.
572-
// Calc based on full image size and be done.
573562
if ($height === null) {
574563
$height = ($width / $origWidth) * $origHeight;
575564

@@ -724,7 +713,6 @@ protected function reproportion()
724713
return;
725714
}
726715

727-
// Sanitize
728716
$this->width = (int) $this->width;
729717
$this->height = (int) $this->height;
730718

‎system/Images/Handlers/GDHandler.php‎

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515

1616
use CodeIgniter\Images\Exceptions\ImageException;
1717
use Config\Images;
18+
use GdImage;
1819

1920
/**
2021
* Image handler for GD package
22+
*
23+
* @extends BaseHandler<GdImage>
2124
*/
2225
class 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

Comments
 (0)