Skip to content
Navigation Menu
{{ message }}
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathImage.php
More file actions
619 lines (590 loc) · 17.8 KB
/
Copy pathImage.php
File metadata and controls
619 lines (590 loc) · 17.8 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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
<?php declare(strict_types=1);
/*
* This file is part of Aplus Framework Image Library.
*
* (c) Natan Felles <natanfelles@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Framework\Image;
use GdImage;
use InvalidArgumentException;
use JetBrains\PhpStorm\ArrayShape;
use JetBrains\PhpStorm\Pure;
use LogicException;
use RuntimeException;
/**
* Class Image.
*
* @package image
*/
class Image implements \JsonSerializable, \Stringable
{
/**
* Path to the image file.
*/
protected string $filename;
/**
* Image type. One of IMAGETYPE_* constants.
*/
protected int $type;
/**
* MIME type.
*/
protected string $mime;
/**
* GdImage instance.
*/
protected GdImage $instance;
/**
* The image quality/compression level.
*
* 0 to 9 on PNG, default is 6. 0 to 100 on JPEG, default is 75.
* Null to update to the default when getQuality is called.
*
* @see Image::getQuality()
*/
protected ?int $quality = null;
/**
* Image constructor.
*
* @param string $filename Path to the image file.
* Acceptable types are: GIF, JPEG and PNG
*
* @throws InvalidArgumentException for invalid file
* @throws RuntimeException for unsupported image type of could not get image info
*/
public function __construct(string $filename)
{
$realpath = \realpath($filename);
if ($realpath === false || !\is_file($realpath) || !\is_readable($realpath)) {
throw new InvalidArgumentException('File does not exists or is not readable: ' . $filename);
}
$this->filename = $realpath;
$info = \getimagesize($this->filename);
if ($info === false) {
throw new RuntimeException(
'Could not get image info from the given filename: ' . $this->filename
);
}
if (!(\imagetypes() & $info[2])) {
throw new RuntimeException('Unsupported image type: ' . $info[2]);
}
$this->type = $info[2];
$this->mime = $info['mime'];
$instance = match ($this->type) {
\IMAGETYPE_PNG => \imagecreatefrompng($this->filename),
\IMAGETYPE_JPEG => \imagecreatefromjpeg($this->filename),
\IMAGETYPE_GIF => \imagecreatefromgif($this->filename),
default => throw new RuntimeException('Image type is not acceptable: ' . $this->type),
};
if (!$instance instanceof GdImage) {
throw new RuntimeException(
"Image of type '{$this->type}' does not returned a GdImage instance"
);
}
$this->instance = $instance;
}
public function __toString() : string
{
return $this->getDataUrl();
}
/**
* Destroys the GdImage instance.
*
* @return bool
*
* @deprecated
*
* @codeCoverageIgnore
*/
public function destroy() : bool
{
return \imagedestroy($this->instance);
}
/**
* Gets the GdImage instance.
*
* @return GdImage GdImage instance
*/
#[Pure]
public function getInstance() : GdImage
{
return $this->instance;
}
/**
* Sets the GdImage instance.
*
* @param GdImage $instance GdImage instance
*
* @return static
*/
public function setInstance(GdImage $instance) : static
{
$this->instance = $instance;
return $this;
}
/**
* Gets the image quality/compression level.
*
* @return int|null An integer for PNG and JPEG types or null for GIF
*/
public function getQuality() : ?int
{
if ($this->quality === null) {
if ($this->type === \IMAGETYPE_PNG) {
$this->quality = 6;
} elseif ($this->type === \IMAGETYPE_JPEG) {
$this->quality = 75;
}
}
return $this->quality;
}
/**
* Sets the image quality/compression level.
*
* @param int $quality The quality/compression level
*
* @throws LogicException when trying to set a quality value for a GIF image
* @throws InvalidArgumentException if the image type is PNG and the value
* is not between 0 and 9 or if the image type is JPEG and the value is not
* between 0 and 100
*
* @return static
*/
public function setQuality(int $quality) : static
{
if ($this->type === \IMAGETYPE_GIF) {
throw new LogicException(
'GIF images does not receive a quality value'
);
}
if ($this->type === \IMAGETYPE_PNG && ($quality < 0 || $quality > 9)) {
throw new InvalidArgumentException(
'PNG images must receive a quality value between 0 and 9, ' . $quality . ' given'
);
}
if ($this->type === \IMAGETYPE_JPEG && ($quality < 0 || $quality > 100)) {
throw new InvalidArgumentException(
'JPEG images must receive a quality value between 0 and 100, ' . $quality . ' given'
);
}
$this->quality = $quality;
return $this;
}
/**
* Gets the image resolution.
*
* @throws RuntimeException for image could not get resolution
*
* @return array<string,int> Returns an array containing two keys, horizontal and
* vertical, with integers as values
*/
#[ArrayShape(['horizontal' => 'int', 'vertical' => 'int'])]
public function getResolution() : array
{
$resolution = \imageresolution($this->instance);
if ($resolution === false) {
throw new RuntimeException('Image could not to get resolution');
}
return [
'horizontal' => $resolution[0], // @phpstan-ignore-line
// @phpstan-ignore-next-line
'vertical' => $resolution[1],
];
}
/**
* Sets the image resolution.
*
* @param int $horizontal The horizontal resolution in DPI
* @param int $vertical The vertical resolution in DPI
*
* @throws RuntimeException for image could not to set resolution
*
* @return static
*/
public function setResolution(int $horizontal = 96, int $vertical = 96) : static
{
$set = \imageresolution($this->instance, $horizontal, $vertical);
if ($set === false) {
throw new RuntimeException('Image could not to set resolution');
}
return $this;
}
/**
* Gets the image height.
*
* @return int
*/
#[Pure]
public function getHeight() : int
{
return \imagesy($this->instance);
}
/**
* Gets the image width.
*
* @return int
*/
#[Pure]
public function getWidth() : int
{
return \imagesx($this->instance);
}
/**
* Gets the file extension for image type.
*
* @return false|string a string with the extension corresponding to the
* given image type or false on fail
*/
#[Pure]
public function getExtension() : false | string
{
return \image_type_to_extension($this->type);
}
/**
* Gets the image MIME type.
*
* @return string
*/
#[Pure]
public function getMime() : string
{
return $this->mime;
}
/**
* Saves the image contents to a given filename.
*
* @param string|null $filename Optional filename or null to use the original
*
* @return bool
*/
public function save(?string $filename = null) : bool
{
$filename ??= $this->filename;
return match ($this->type) {
\IMAGETYPE_PNG => \imagepng($this->instance, $filename, $this->getQuality()),
\IMAGETYPE_JPEG => \imagejpeg($this->instance, $filename, $this->getQuality()),
\IMAGETYPE_GIF => \imagegif($this->instance, $filename),
default => false,
};
}
/**
* Sends the image contents to the output buffer.
*
* @return bool
*/
public function send() : bool
{
if (\in_array($this->type, [\IMAGETYPE_PNG, \IMAGETYPE_GIF], true)) {
\imagesavealpha($this->instance, true);
}
return match ($this->type) {
\IMAGETYPE_PNG => \imagepng($this->instance, null, $this->getQuality()),
\IMAGETYPE_JPEG => \imagejpeg($this->instance, null, $this->getQuality()),
\IMAGETYPE_GIF => \imagegif($this->instance),
default => false,
};
}
/**
* Renders the image contents.
*
* @throws RuntimeException for image could not be rendered
*
* @return string The image contents
*/
public function render() : string
{
\ob_start();
$status = $this->send();
$contents = \ob_get_clean();
if ($status === false || $contents === false) {
throw new RuntimeException('Image could not be rendered');
}
return $contents;
}
/**
* Crops the image.
*
* @param int $width Width in pixels
* @param int $height Height in pixels
* @param int $marginLeft Margin left in pixels
* @param int $marginTop Margin top in pixels
*
* @throws RuntimeException for image could not to crop
*
* @return static
*/
public function crop(int $width, int $height, int $marginLeft = 0, int $marginTop = 0) : static
{
$crop = \imagecrop($this->instance, [
'x' => $marginLeft,
'y' => $marginTop,
'width' => $width,
'height' => $height,
]);
if ($crop === false) {
throw new RuntimeException('Image could not to crop');
}
$this->instance = $crop;
return $this;
}
/**
* Flips the image.
*
* @param string $direction Allowed values are: h or horizontal. v or vertical. b or both.
*
* @throws InvalidArgumentException for invalid image flip direction
* @throws RuntimeException for image could not to flip
*
* @return static
*/
public function flip(string $direction = 'horizontal') : static
{
$direction = match ($direction) {
'h', 'horizontal' => \IMG_FLIP_HORIZONTAL,
'v', 'vertical' => \IMG_FLIP_VERTICAL,
'b', 'both' => \IMG_FLIP_BOTH,
default => throw new InvalidArgumentException('Invalid image flip direction: ' . $direction),
};
$flip = \imageflip($this->instance, $direction);
if ($flip === false) {
throw new RuntimeException('Image could not to flip');
}
return $this;
}
/**
* Applies a filter to the image.
*
* @param int $type IMG_FILTER_* constants
* @param int ...$arguments Arguments for the filter type
*
* @see https://www.php.net/manual/en/function.imagefilter.php
*
* @throws RuntimeException for image could not apply the filter
*
* @return static
*/
public function filter(int $type, int ...$arguments) : static
{
$filtered = \imagefilter($this->instance, $type, ...$arguments);
if ($filtered === false) {
throw new RuntimeException('Image could not apply the filter');
}
return $this;
}
/**
* Flattens the image.
*
* Replaces transparency with an RGB color.
*
* @param int $red
* @param int $green
* @param int $blue
*
* @throws RuntimeException for could not create a true color image, could
* not allocate a color or image could not to flatten
*
* @return static
*/
public function flatten(int $red = 255, int $green = 255, int $blue = 255) : static
{
\imagesavealpha($this->instance, false);
$image = \imagecreatetruecolor($this->getWidth(), $this->getHeight());
if ($image === false) {
throw new RuntimeException('Could not create a true color image');
}
$color = \imagecolorallocate($image, $red, $green, $blue);
if ($color === false) {
throw new RuntimeException('Image could not allocate a color');
}
\imagefilledrectangle(
$image,
0,
0,
$this->getWidth(),
$this->getHeight(),
$color
);
$copied = \imagecopy(
$image,
$this->instance,
0,
0,
0,
0,
$this->getWidth(),
$this->getHeight()
);
if ($copied === false) {
throw new RuntimeException('Image could not to flatten');
}
$this->instance = $image;
return $this;
}
/**
* Sets the image opacity level.
*
* @param int $opacity Opacity percentage: from 0 to 100
*
* @return static
*/
public function opacity(int $opacity = 100) : static
{
if ($opacity < 0 || $opacity > 100) {
throw new InvalidArgumentException(
'Opacity percentage must be between 0 and 100, ' . $opacity . ' given'
);
}
if ($opacity === 100) {
\imagealphablending($this->instance, true);
return $this;
}
$opacity = (int) \round(\abs(($opacity * 127 / 100) - 127));
\imagelayereffect($this->instance, \IMG_EFFECT_OVERLAY);
$color = \imagecolorallocatealpha($this->instance, 127, 127, 127, $opacity);
if ($color === false) {
throw new RuntimeException('Image could not allocate a color');
}
\imagefilledrectangle(
$this->instance,
0,
0,
$this->getWidth(),
$this->getHeight(),
$color
);
\imagesavealpha($this->instance, true);
\imagealphablending($this->instance, false);
return $this;
}
/**
* Rotates the image with a given angle.
*
* @param float $angle Rotation angle, in degrees. Clockwise direction.
*
* @throws RuntimeException for image could not allocate a color or could not rotate
*
* @return static
*/
public function rotate(float $angle) : static
{
if (\in_array($this->type, [\IMAGETYPE_PNG, \IMAGETYPE_GIF], true)) {
\imagealphablending($this->instance, false);
\imagesavealpha($this->instance, true);
$background = \imagecolorallocatealpha($this->instance, 0, 0, 0, 127);
} else {
$background = \imagecolorallocate($this->instance, 255, 255, 255);
}
if ($background === false) {
throw new RuntimeException('Image could not allocate a color');
}
$rotate = \imagerotate($this->instance, -1 * $angle, $background);
if ($rotate === false) {
throw new RuntimeException('Image could not to rotate');
}
$this->instance = $rotate;
return $this;
}
/**
* Scales the image.
*
* @param int $width Width in pixels
* @param int $height Height in pixels. Use -1 to use a proportional height
* based on the width.
*
* @throws RuntimeException for image could not to scale
*
* @return static
*/
public function scale(int $width, int $height = -1) : static
{
$scale = \imagescale($this->instance, $width, $height);
if ($scale === false) {
throw new RuntimeException('Image could not to scale');
}
$this->instance = $scale;
return $this;
}
/**
* Adds a watermark to the image.
*
* @param Image $watermark The image to use as watermark
* @param int $horizontalPosition Horizontal position
* @param int $verticalPosition Vertical position
*
* @throws RuntimeException for image could not to create watermark
*
* @return static
*/
public function watermark(
Image $watermark,
int $horizontalPosition = 0,
int $verticalPosition = 0
) : static {
if ($horizontalPosition < 0) {
$horizontalPosition = $this->getWidth()
- (-1 * $horizontalPosition + $watermark->getWidth());
}
if ($verticalPosition < 0) {
$verticalPosition = $this->getHeight()
- (-1 * $verticalPosition + $watermark->getHeight());
}
$copied = \imagecopy(
$this->instance,
$watermark->getInstance(),
$horizontalPosition,
$verticalPosition,
0,
0,
$watermark->getWidth(),
$watermark->getHeight()
);
if ($copied === false) {
throw new RuntimeException('Image could not to create watermark');
}
return $this;
}
/**
* Allow embed the image contents in a document.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
* @see https://datatracker.ietf.org/doc/html/rfc2397
*
* @return string The image "data" URL
*/
public function getDataUrl() : string
{
return 'data:' . $this->getMime() . ';base64,' . \base64_encode($this->render());
}
/**
* @return string
*/
public function jsonSerialize() : string
{
return $this->getDataUrl();
}
/**
* Indicates if a given filename has an acceptable image type.
*
* @param string $filename
*
* @return bool
*/
public static function isAcceptable(string $filename) : bool
{
$filename = \realpath($filename);
if ($filename === false || !\is_file($filename) || !\is_readable($filename)) {
return false;
}
$info = \getimagesize($filename);
if ($info === false) {
return false;
}
return match ($info[2]) {
\IMAGETYPE_PNG, \IMAGETYPE_JPEG, \IMAGETYPE_GIF => true,
default => false,
};
}
}
You can’t perform that action at this time.
