forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGDHandler.php
More file actions
523 lines (424 loc) · 15.6 KB
/
GDHandler.php
File metadata and controls
523 lines (424 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Images\Handlers;
use CodeIgniter\Images\Exceptions\ImageException;
use Config\Images;
/**
* Image handler for GD package
*/
class GDHandler extends BaseHandler
{
/**
* Constructor.
*
* @param Images|null $config
*
* @throws ImageException
*/
public function __construct($config = null)
{
parent::__construct($config);
if (! extension_loaded('gd')) {
throw ImageException::forMissingExtension('GD'); // @codeCoverageIgnore
}
}
/**
* Handles the rotation of an image resource.
* Doesn't save the image, but replaces the current resource.
*/
protected function _rotate(int $angle): bool
{
// Create the image handle
$srcImg = $this->createImage();
// Set the background color
// This won't work with transparent PNG files so we are
// going to have to figure out how to determine the color
// of the alpha channel in a future release.
$white = imagecolorallocate($srcImg, 255, 255, 255);
// Rotate it!
$destImg = imagerotate($srcImg, $angle, $white);
$this->resource = $destImg;
return true;
}
/**
* Flattens transparencies
*
* @return $this
*/
protected function _flatten(int $red = 255, int $green = 255, int $blue = 255)
{
$srcImg = $this->createImage();
if (function_exists('imagecreatetruecolor')) {
$create = 'imagecreatetruecolor';
$copy = 'imagecopyresampled';
} else {
$create = 'imagecreate';
$copy = 'imagecopyresized';
}
$dest = $create($this->width, $this->height);
$matte = imagecolorallocate($dest, $red, $green, $blue);
imagefilledrectangle($dest, 0, 0, $this->width, $this->height, $matte);
imagecopy($dest, $srcImg, 0, 0, 0, 0, $this->width, $this->height);
$this->resource = $dest;
return $this;
}
/**
* Flips an image along it's vertical or horizontal axis.
*
* @return $this
*/
protected function _flip(string $direction)
{
$srcImg = $this->createImage();
$angle = $direction === 'horizontal' ? IMG_FLIP_HORIZONTAL : IMG_FLIP_VERTICAL;
imageflip($srcImg, $angle);
$this->resource = $srcImg;
return $this;
}
/**
* Get GD version
*
* @return mixed
*/
public function getVersion()
{
if (function_exists('gd_info')) {
$gdVersion = @gd_info();
return preg_replace('/\D/', '', $gdVersion['GD Version']);
}
return false;
}
/**
* Resizes the image.
*
* @return GDHandler
*/
public function _resize(bool $maintainRatio = false)
{
return $this->process('resize');
}
/**
* Crops the image.
*
* @return GDHandler
*/
public function _crop()
{
return $this->process('crop');
}
/**
* Handles all of the grunt work of resizing, etc.
*
* @return $this
*/
protected function process(string $action)
{
$origWidth = $this->image()->origWidth;
$origHeight = $this->image()->origHeight;
if ($action === 'crop') {
// Reassign the source width/height if cropping
$origWidth = $this->width;
$origHeight = $this->height;
// Modify the "original" width/height to the new
// values so that methods that come after have the
// correct size to work with.
$this->image()->origHeight = $this->height;
$this->image()->origWidth = $this->width;
}
// Create the image handle
$src = $this->createImage();
if (function_exists('imagecreatetruecolor')) {
$create = 'imagecreatetruecolor';
$copy = 'imagecopyresampled';
} else {
$create = 'imagecreate';
$copy = 'imagecopyresized';
}
$dest = $create($this->width, $this->height);
// for png, webp and avif we can actually preserve transparency
if (in_array($this->image()->imageType, $this->supportTransparency, true)) {
imagealphablending($dest, false);
imagesavealpha($dest, true);
}
$copy($dest, $src, 0, 0, (int) $this->xAxis, (int) $this->yAxis, $this->width, $this->height, $origWidth, $origHeight);
$this->resource = $dest;
return $this;
}
/**
* Saves any changes that have been made to file. If no new filename is
* provided, the existing image is overwritten, otherwise a copy of the
* file is made at $target.
*
* Example:
* $image->resize(100, 200, true)
* ->save();
*
* @param non-empty-string|null $target
*/
public function save(?string $target = null, int $quality = 90, int $speed = -1): bool
{
$original = $target;
$target = ($target === null || $target === '') ? $this->image()->getPathname() : $target;
// If no new resource has been created, then we're
// simply copy the existing one.
if (empty($this->resource) && $quality === 100) {
if ($original === null) {
return true;
}
$name = basename($target);
$path = pathinfo($target, PATHINFO_DIRNAME);
return $this->image()->copy($path, $name);
}
$this->ensureResource();
// for png, webp and avif we can actually preserve transparency
if (in_array($this->image()->imageType, $this->supportTransparency, true)) {
imagepalettetotruecolor($this->resource);
imagealphablending($this->resource, false);
imagesavealpha($this->resource, true);
}
switch ($this->image()->imageType) {
case IMAGETYPE_GIF:
if (! function_exists('imagegif')) {
throw ImageException::forInvalidImageCreate(lang('Images.gifNotSupported'));
}
if (! @imagegif($this->resource, $target)) {
throw ImageException::forSaveFailed();
}
break;
case IMAGETYPE_JPEG:
if (! function_exists('imagejpeg')) {
throw ImageException::forInvalidImageCreate(lang('Images.jpgNotSupported'));
}
if (! @imagejpeg($this->resource, $target, $quality)) {
throw ImageException::forSaveFailed();
}
break;
case IMAGETYPE_PNG:
if (! function_exists('imagepng')) {
throw ImageException::forInvalidImageCreate(lang('Images.pngNotSupported'));
}
if (! @imagepng($this->resource, $target)) {
throw ImageException::forSaveFailed();
}
break;
case IMAGETYPE_WEBP:
if (! function_exists('imagewebp')) {
throw ImageException::forInvalidImageCreate(lang('Images.webpNotSupported'));
}
if (! @imagewebp($this->resource, $target, $quality)) {
throw ImageException::forSaveFailed();
}
break;
case IMAGETYPE_AVIF:
if (! function_exists('imageavif')) {
throw ImageException::forInvalidImageCreate(lang('Images.avifNotSupported'));
}
if (! @imageavif($this->resource, $target, $quality, $speed)) {
throw ImageException::forSaveFailed();
}
break;
default:
throw ImageException::forInvalidImageCreate();
}
$this->resource = null;
chmod($target, $this->filePermissions);
return true;
}
/**
* Create Image Resource
*
* This simply creates an image resource handle
* based on the type of image being processed
*
* @return bool|resource
*/
protected function createImage(string $path = '', string $imageType = '')
{
if ($this->resource !== null) {
return $this->resource;
}
if ($path === '') {
$path = $this->image()->getPathname();
}
if ($imageType === '') {
$imageType = $this->image()->imageType;
}
return $this->getImageResource($path, $imageType);
}
/**
* Make the image resource object if needed
*/
protected function ensureResource()
{
if ($this->resource === null) {
// if valid image type, make corresponding image resource
$this->resource = $this->getImageResource(
$this->image()->getPathname(),
$this->image()->imageType,
);
}
}
/**
* Check if image type is supported and return image resource
*
* @param string $path Image path
* @param int $imageType Image type
*
* @return bool|resource
*
* @throws ImageException
*/
protected function getImageResource(string $path, int $imageType)
{
switch ($imageType) {
case IMAGETYPE_GIF:
if (! function_exists('imagecreatefromgif')) {
throw ImageException::forInvalidImageCreate(lang('Images.gifNotSupported'));
}
return imagecreatefromgif($path);
case IMAGETYPE_JPEG:
if (! function_exists('imagecreatefromjpeg')) {
throw ImageException::forInvalidImageCreate(lang('Images.jpgNotSupported'));
}
return imagecreatefromjpeg($path);
case IMAGETYPE_PNG:
if (! function_exists('imagecreatefrompng')) {
throw ImageException::forInvalidImageCreate(lang('Images.pngNotSupported'));
}
return @imagecreatefrompng($path);
case IMAGETYPE_WEBP:
if (! function_exists('imagecreatefromwebp')) {
throw ImageException::forInvalidImageCreate(lang('Images.webpNotSupported'));
}
return imagecreatefromwebp($path);
case IMAGETYPE_AVIF:
if (! function_exists('imagecreatefromavif')) {
throw ImageException::forInvalidImageCreate(lang('Images.avifNotSupported'));
}
return imagecreatefromavif($path);
default:
throw ImageException::forInvalidImageCreate('Ima');
}
}
/**
* Add text overlay to an image.
*/
protected function _text(string $text, array $options = [])
{
// Reverse the vertical offset
// When the image is positioned at the bottom
// we don't want the vertical offset to push it
// further down. We want the reverse, so we'll
// invert the offset. Note: The horizontal
// offset flips itself automatically
if ($options['vAlign'] === 'bottom') {
$options['vOffset'] *= -1;
}
if ($options['hAlign'] === 'right') {
$options['hOffset'] *= -1;
}
// Set font width and height
// These are calculated differently depending on
// whether we are using the true type font or not
if (! empty($options['fontPath'])) {
if (function_exists('imagettfbbox')) {
$temp = imagettfbbox($options['fontSize'], 0, $options['fontPath'], $text);
$temp = $temp[2] - $temp[0];
$fontwidth = $temp / strlen($text);
} else {
$fontwidth = $options['fontSize'] - ($options['fontSize'] / 4);
}
$fontheight = $options['fontSize'];
} else {
$fontwidth = imagefontwidth($options['fontSize']);
$fontheight = imagefontheight($options['fontSize']);
}
$options['fontheight'] = $fontheight;
$options['fontwidth'] = $fontwidth;
// Set base X and Y axis values
$xAxis = $options['hOffset'] + $options['padding'];
$yAxis = $options['vOffset'] + $options['padding'];
// Set vertical alignment
if ($options['vAlign'] === 'middle') {
// Don't apply padding when you're in the middle of the image.
$yAxis += ($this->image()->origHeight / 2) + ($fontheight / 2) - $options['padding'] - $fontheight - $options['shadowOffset'];
} elseif ($options['vAlign'] === 'bottom') {
$yAxis = ($this->image()->origHeight - $fontheight - $options['shadowOffset'] - ($fontheight / 2)) - $yAxis;
}
// Set horizontal alignment
if ($options['hAlign'] === 'right') {
$xAxis += ($this->image()->origWidth - ($fontwidth * strlen($text)) - $options['shadowOffset']) - (2 * $options['padding']);
} elseif ($options['hAlign'] === 'center') {
$xAxis += floor(($this->image()->origWidth - ($fontwidth * strlen($text))) / 2);
}
$options['xAxis'] = $xAxis;
$options['yAxis'] = $yAxis;
if ($options['withShadow']) {
// Offset from text
$options['xShadow'] = $xAxis + $options['shadowOffset'];
$options['yShadow'] = $yAxis + $options['shadowOffset'];
$this->textOverlay($text, $options, true);
}
$this->textOverlay($text, $options);
}
/**
* Handler-specific method for overlaying text on an image.
*
* @param bool $isShadow Whether we are drawing the dropshadow or actual text
*/
protected function textOverlay(string $text, array $options = [], bool $isShadow = false)
{
$src = $this->createImage();
/* Set RGB values for shadow
*
* Get the rest of the string and split it into 2-length
* hex values:
*/
$opacity = (int) ($options['opacity'] * 127);
// Allow opacity to be applied to the text
imagealphablending($src, true);
$color = $isShadow ? $options['shadowColor'] : $options['color'];
// shorthand hex, #f00
if (strlen($color) === 3) {
$color = implode('', array_map(str_repeat(...), str_split($color), [2, 2, 2]));
}
$color = str_split(substr($color, 0, 6), 2);
$color = imagecolorclosestalpha($src, hexdec($color[0]), hexdec($color[1]), hexdec($color[2]), $opacity);
$xAxis = $isShadow ? $options['xShadow'] : $options['xAxis'];
$yAxis = $isShadow ? $options['yShadow'] : $options['yAxis'];
// Add the shadow to the source image
if (! empty($options['fontPath'])) {
// We have to add fontheight because imagettftext locates the bottom left corner, not top-left corner.
imagettftext($src, $options['fontSize'], 0, (int) $xAxis, (int) ($yAxis + $options['fontheight']), $color, $options['fontPath'], $text);
} else {
imagestring($src, (int) $options['fontSize'], (int) $xAxis, (int) $yAxis, $text, $color);
}
$this->resource = $src;
}
/**
* Return image width.
*
* @return int
*/
public function _getWidth()
{
return imagesx($this->resource);
}
/**
* Return image height.
*
* @return int
*/
public function _getHeight()
{
return imagesy($this->resource);
}
}