-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathImage.php
More file actions
1183 lines (1005 loc) · 38 KB
/
Image.php
File metadata and controls
1183 lines (1005 loc) · 38 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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Photobooth;
use GdImage;
use Photobooth\Utility\ImageUtility;
use Photobooth\Utility\PathUtility;
use Photobooth\Utility\QrCodeUtility;
class Image
{
/**
* The new filename for the image.
*/
public string $newFilename;
/**
* The debug level for error handling. Set to 0 avoid failing on error.
*/
public int $debugLevel = 0;
/**
* Error counter.
*/
public int $errorCount = 0;
/**
* Array to store error messages.
*/
public array $errorLog = [];
/**
* Indicate source image was modified.
*/
public bool $imageModified = false;
/**
* The quality of the saved jpeg image, from 0 (lowest) to 100 (highest). Default is 80.
*/
public int $jpegQuality = 80;
/**
*
* Text to Image Difinitions
*
*/
/**
* Font size for the text
*/
public int $fontSize = 80;
/**
* Rotation angle of the font
*/
public int $fontRotation = 0;
/**
* X-coordinate of the starting position for the text
*/
public int $fontLocationX = 80;
/**
* Y-coordinate of the starting position for the text
*/
public int $fontLocationY = 80;
/**
* Color of the font in hexadecimal format (e.g., "#FF0000" for red)
*/
public string $fontColor = '#ffffff';
/**
* File path to the TrueType font file to be used
*/
public string $fontPath = '';
/**
* Text for the first line
*/
public string $textLine1 = '';
/**
* Text for the second line
*/
public string $textLine2 = '';
/**
* Text for the third line
*/
public string $textLine3 = '';
/**
* Vertical spacing between lines of text
*/
public int $textLineSpacing = 90;
/**
*
* Apply Frame to Image Difinitions
*
*/
/**
* File path to the frame image (PNG)
*/
public string $framePath = '';
/**
* Whether to extend the frame to fit the source image
*/
public bool $frameExtend = false;
/**
* The percentage of extension to the left side of the frame
*/
public int $frameExtendLeft = 0;
/**
* The percentage of extension to the right side of the frame
*/
public int $frameExtendRight = 0;
/**
* The percentage of extension to the bottom of the frame
*/
public int $frameExtendBottom = 0;
/**
* The percentage of extension to the top of the frame
*/
public int $frameExtendTop = 0;
/**
*
* Add picture to image source Difinitions
*
*/
/**
* The x-coordinate of the top-left corner of the picture to be added.
*/
public int $addPictureX = 0;
/**
* The y-coordinate of the top-left corner of the picture to be added.
*/
public int $addPictureY = 0;
/**
* The width of the picture to be added.
*/
public int $addPictureWidth = 0;
/**
* The height of the picture to be added.
*/
public int $addPictureHeight = 0;
/**
* The rotation angle of the picture to be added (in degrees).
*/
public int $addPictureRotation = 0;
/**
* A flag indicating whether to apply a frame to the picture to be added.
*/
public bool $addPictureApplyFrame = false;
/**
*
* Add dashed line Definitions
*
*/
/**
* Color of the dashed line.
*/
public string $dashedLineColor = '';
/**
* X-coordinate of the starting point of the dashed line.
*/
public int $dashedLineStartX = 0;
/**
* Y-coordinate of the starting point of the dashed line.
*/
public int $dashedLineStartY = 0;
/**
* X-coordinate of the ending point of the dashed line.
*/
public int $dashedLineEndX = 0;
/**
* Y-coordinate of the ending point of the dashed line.
*/
public int $dashedLineEndY = 0;
/**
*
* QR Difinitions
*
*/
/**
* Whether or not to rotate the QR code.
*/
public bool $qrRotate = false;
/**
* The position to place the QR code on the image.
*/
public string $qrPosition = 'bottom-right';
/**
* The offset in pixels from the specified QR code position.
*/
public int $qrOffset = 0;
/**
* The size of the QR code, numeric value within the range of 2 to 10 and even.
*/
public int $qrSize = 4;
/**
* @var int $qrMargin The margin size around the QR code, must be in range between 0 and 10.
*/
public int $qrMargin = 4;
/**
* The color to apply to the QR code pixels.
*/
public string $qrColor = '#ffffff';
/**
* The URL to generate a QR code for.
*/
public string $qrUrl = '';
/**
*
* Polaroid Effect Difinitions
*
*/
/**
* The background color for the polaroid effect in hexadecimal format (e.g., '#c8c8c8').
*/
public string $polaroidBgColor = '#c8c8c8';
/**
* The rotation angle for the polaroid effect in degrees.
*/
public int $polaroidRotation = 0;
/**
* Creates a new filename for the image.
*/
public static function createNewFilename(string $naming = 'random', string $ext = '.jpg'): string
{
if ($naming === 'dateformatted') {
$name = date('Ymd_His') . $ext;
} else {
$name = md5(microtime()) . $ext;
}
return $name;
}
/**
* Collect error data and increase errorCount
*/
public function addErrorData(string|array $errorData): void
{
$this->errorCount++;
$this->errorLog[] = $errorData;
}
/**
* Reset the error count and error log.
*/
public function errorReset(): void
{
$this->errorCount = 0;
$this->errorLog = [];
}
/**
* Sets the new filename for the image using the specified naming convention.
*/
public function setNewFilename(string $naming): void
{
$this->newFilename = $this->createNewFilename($naming);
}
/**
* Returns the new filename for the image.
*/
public function getNewFilename(): string
{
return $this->newFilename;
}
/**
* Sets the new filename for the image using the specified naming convention and returns the new filename.
*/
public function setAndGetNewFilename(string $naming): string
{
$this->setNewFilename($naming);
return $this->newFilename;
}
/**
* Parses a hex color string and returns its RGBA components.
*
* @param string $hexColor The hex color string to parse.
* @return array An array containing the red, green, blue, and alpha components.
* @throws \Exception If the color string is invalid or parsing fails.
*/
public static function getColorComponents(string $hexColor): array
{
try {
if (strlen($hexColor) < 3) {
throw new \Exception('Invalid color: too short.');
}
if (strlen($hexColor) > 9) {
throw new \Exception('Invalid color: too long.');
}
if ($hexColor[0] !== '#') {
throw new \Exception('Color HEX must start with "#".');
}
while (strlen($hexColor) < 9) {
$hexColor .= '0';
}
$colorComponents = sscanf($hexColor, '#%02x%02x%02x%02x');
if ($colorComponents !== null) {
list($r, $g, $b, $a) = $colorComponents;
return [$r, $g, $b, $a];
} else {
throw new \Exception('Color parsing failed: sscanf returned null.');
}
} catch (\Exception $e) {
return [0, 0, 0, 0];
}
}
/**
* Creates a GD image resource from an image file.
*/
public function createFromImage(string $image): GdImage|false
{
try {
if (str_contains($image, '/api/randomImg.php')) {
parse_str(parse_url($image)['query'] ?? '', $query);
$path = is_array($query['dir']) ? $query['dir']['0'] : $query['dir'];
$image = ImageUtility::getRandomImageFromPath($path);
}
if (!file_exists($image)) {
$image = PathUtility::resolveFilePath($image);
}
$resource = imagecreatefromstring((string)file_get_contents($image));
if (!$resource) {
throw new \Exception('Can\'t create GD resource.');
}
return $resource;
} catch (\Exception $e) {
$this->addErrorData($e->getMessage());
return false;
}
}
/**
* Saves a GD image resource to disk.
*/
public function saveJpeg(GdImage $sourceResource, string $destination): bool
{
try {
// Save the image to disk
if (!imagejpeg($sourceResource, $destination, $this->jpegQuality)) {
throw new \Exception('Error saving image.');
}
return true;
} catch (\Exception $e) {
// If there is an exception, return false
$this->addErrorData($e->getMessage());
return false;
}
}
/**
* Rotate and resize an image.
*/
public function rotateResizeImage(GdImage $image, int $degrees, string $bgColor = '#ffffff', bool $useTransparentBackground = false): GdImage|false
{
try {
// simple rotate if possible and ignore changed dimensions (doesn't need to care about background color)
$simple_rotate = [-180, -90, 0, 180, 90, 360];
if (in_array($degrees, $simple_rotate)) {
$new = imagerotate($image, $degrees, 0);
if (!$new) {
throw new \Exception('Cannot rotate image.');
}
} else {
$old_width = imagesx($image);
$old_height = imagesy($image);
// Create a new true color image
$new = imagecreatetruecolor($old_width, $old_height);
if (!$new) {
throw new \Exception('Failed to create new image canvas.');
}
if ($useTransparentBackground) {
// Enable transparency
imagesavealpha($new, true);
imagealphablending($new, false);
// Allocate a fully transparent background
$background = imagecolorallocatealpha($new, 0, 0, 0, 127);
} else {
$colorComponents = self::getColorComponents($bgColor);
list($bg_r, $bg_g, $bg_b, $bg_a) = $colorComponents;
// color background as defined
$background = imagecolorallocatealpha($new, $bg_r, $bg_g, $bg_b, $bg_a);
}
if (!imagefill($new, 0, 0, (int)$background)) {
throw new \Exception('Cannot fill image.');
}
// rotate the image
$image = imagerotate($image, $degrees, (int)$background);
if (!$image) {
throw new \Exception('Cannot rotate image.');
}
// make sure width and/or height fits into old dimensions
$image = self::resizeImage($image, intval($old_width), intval($old_height));
if (!$image instanceof \GdImage) {
throw new \Exception('Failed to resize image.');
}
// get new dimensions after rotate and resize
$new_width = intval(imagesx($image));
$new_height = intval(imagesy($image));
// center rotated image
$x = intval(($old_width - $new_width) / 2);
$y = intval(($old_height - $new_height) / 2);
// copy rotated image to new image with old dimensions
if (imagecopy($new, $image, $x, $y, 0, 0, $new_width, $new_height)) {
throw new \Exception('Cannot copy rotated image to new image.');
}
}
$this->imageModified = true;
return $new;
} catch (\Exception $e) {
$this->addErrorData($e->getMessage());
// Try to clear cache
if ($new instanceof \GdImage) {
unset($new);
}
// Re-throw exception on loglevel > 1
if ($this->debugLevel > 1) {
throw $e;
}
// Return unmodified resource
return $image;
}
}
/**
* Resize an image based on the maximum dimensions.
*/
public function resizeImage(GdImage $image, int $maxWidth, int $maxHeight = null): GdImage|false
{
$maxHeight = $maxHeight ?? $maxWidth;
try {
$old_width = imagesx($image);
$old_height = imagesy($image);
if ($maxWidth <= 0 || $maxHeight <= 0) {
throw new \Exception('Invalid image maximum dimensions.');
}
$scale = min($maxWidth / $old_width, $maxHeight / $old_height);
$new_width = intval(ceil($scale * $old_width));
$new_height = intval(ceil($scale * $old_height));
$new_image = imagescale($image, $new_width, $new_height, IMG_TRIANGLE);
if (!$new_image) {
throw new \Exception('Cannot resize image.');
}
$this->imageModified = true;
return $new_image;
} catch (\Exception $e) {
$this->addErrorData($e->getMessage());
// Re-throw exception on loglevel > 1
if ($this->debugLevel > 1) {
throw $e;
}
// Return unmodified resource
return $image;
}
}
/**
* Resize a PNG image based on the maximum dimensions.
*/
public function resizePngImage(GdImage $image, int $newWidth, ?int $newHeight = null, bool $keepAspectRatio = false): GdImage|false
{
$newHeight = $newHeight ?? $newWidth;
try {
$old_width = intval(imagesx($image));
$old_height = intval(imagesy($image));
if ($newWidth <= 0 || $newHeight <= 0) {
throw new \Exception('Invalid image maximum dimensions.');
}
if ($keepAspectRatio) {
$scale = min($newWidth / $old_width, $newHeight / $old_height);
$newWidth = intval(ceil($scale * $old_width));
$newHeight = intval(ceil($scale * $old_height));
}
$new = imagecreatetruecolor((int)$newWidth, (int)$newHeight);
if (!$new) {
throw new \Exception('Cannot create new image.');
}
imagealphablending($new, false);
imagesavealpha($new, true);
if ($keepAspectRatio) {
if (!imagecopyresized($new, $image, 0, 0, 0, 0, $newWidth, $newHeight, $old_width, $old_height)) {
throw new \Exception('Cannot resize image.');
}
} else {
if (!imagecopyresampled($new, $image, 0, 0, 0, 0, $newWidth, $newHeight, $old_width, $old_height)) {
throw new \Exception('Cannot resize image.');
}
}
$this->imageModified = true;
return $new;
} catch (\Exception $e) {
$this->addErrorData($e->getMessage());
// Try to clear cache
if (isset($new) && $new instanceof \GdImage) {
unset($new);
}
// Re-throw exception on loglevel > 1
if ($this->debugLevel > 1) {
throw $e;
}
// Return unmodified resource
return $image;
}
}
/**
* Resize and crop an image by center.
*/
public function resizeCropImage(GdImage $source_file, int $maxWidth, int $maxHeight = null): GdImage
{
$maxHeight = $maxHeight ?? $maxWidth;
try {
$old_width = intval(imagesx($source_file));
$old_height = intval(imagesy($source_file));
if ($maxWidth <= 0 || $maxHeight <= 0) {
throw new \Exception('Invalid image maximum dimensions.');
}
$new_width = intval(($old_height * $maxWidth) / $maxHeight);
$new_height = intval(($old_width * $maxHeight) / $maxWidth);
settype($maxWidth, 'integer');
settype($maxHeight, 'integer');
$new = imagecreatetruecolor(intval($maxWidth), intval($maxHeight));
if (!$new) {
throw new \Exception('Cannot create new image.');
}
// If the new width is greater than the actual width of the image, then the height is too large and the rest is cut off, or vice versa
if ($new_width > $old_width) {
// Cut point by height
$h_point = intval(($old_height - $new_height) / 2);
// Copy image
if (!imagecopyresampled($new, $source_file, 0, 0, 0, $h_point, $maxWidth, $maxHeight, $old_width, $new_height)) {
throw new \Exception('Cannot resize and crop image by height.');
}
} else {
// Cut point by width
$w_point = intval(($old_width - $new_width) / 2);
if (!imagecopyresampled($new, $source_file, 0, 0, $w_point, 0, $maxWidth, $maxHeight, $new_width, $old_height)) {
throw new \Exception('Cannot resize and crop image by width.');
}
}
$this->imageModified = true;
return $new;
} catch (\Exception $e) {
$this->addErrorData($e->getMessage());
// Try to clear cache
if (isset($new) && $new instanceof GdImage) {
unset($new);
}
// Re-throw exception on loglevel > 1
if ($this->debugLevel > 1) {
throw $e;
}
// Return unmodified resource
return $source_file;
}
}
/**
* Apply the frame to the source image resource
*/
public function applyFrame(GdImage $sourceResource): GdImage
{
try {
if ($this->frameExtend) {
$new_width = intval(imagesx($sourceResource) / (1 - 0.01 * ($this->frameExtendLeft + $this->frameExtendRight)));
$new_height = intval(imagesy($sourceResource) / (1 - 0.01 * ($this->frameExtendTop + $this->frameExtendBottom)));
$img = imagecreatetruecolor($new_width, $new_height);
if (!$img instanceof \GdImage) {
throw new \Exception('Cannot create new image.');
}
$white = intval(imagecolorallocate($img, 255, 255, 255));
// We fill in the new white image
if (!imagefill($img, 0, 0, $white)) {
throw new \Exception('Cannot fill image.');
}
$image_pos_x = intval(imagesx($img) * 0.01 * $this->frameExtendLeft);
$image_pos_y = intval(imagesy($img) * 0.01 * $this->frameExtendTop);
// We copy the image to which we want to apply the frame in our new image.
if (!imagecopy($img, $sourceResource, $image_pos_x, $image_pos_y, 0, 0, imagesx($sourceResource), imagesy($sourceResource))) {
throw new \Exception('Error copying image to new frame.');
}
} else {
$img = $sourceResource;
}
$pic_width = imagesx($img);
$pic_height = imagesy($img);
$frame = self::createFromImage($this->framePath);
if (!$frame instanceof \GdImage) {
throw new \Exception('Failed to create frame from image.');
}
$frame = self::resizePngImage($frame, $pic_width, $pic_height);
if (!$frame) {
throw new \Exception('Cannot resize Frame.');
}
$frame_width = imagesx($frame);
$frame_height = imagesy($frame);
$dst_x = 0;
$dst_y = 0;
if ($pic_height == $frame_height) {
$dst_x = intval(($pic_width - $frame_width) / 2);
} else {
$dst_y = intval(($pic_height - $frame_height) / 2);
}
if (!imagecopy($img, $frame, $dst_x, $dst_y, 0, 0, $frame_width, $frame_height)) {
throw new \Exception('Error applying frame to image.');
}
$this->imageModified = true;
// Return resource with text applied
return $img;
} catch (\Exception $e) {
$this->addErrorData($e->getMessage());
// Clear cache
if ($img instanceof GdImage) {
unset($img);
}
// Re-throw exception on loglevel > 1
if ($this->debugLevel > 1) {
throw $e;
}
// Return unmodified resource
return $sourceResource;
}
}
/**
* Apply text to the source image resource
*/
public function applyText(GdImage $sourceResource): GdImage
{
try {
$fontPath = PathUtility::getAbsolutePath($this->fontPath);
$tempFontPath = $_SERVER['DOCUMENT_ROOT'] . '/tempfont.ttf';
$isTempFont = false;
$fontSize = $this->fontSize;
$fontRotation = $this->fontRotation;
$fontLocationX = $this->fontLocationX;
$fontLocationY = $this->fontLocationY;
$textLineSpacing = $this->textLineSpacing;
// Convert hex color string to RGB values
$colorComponents = self::getColorComponents($this->fontColor);
list($r, $g, $b) = $colorComponents;
// Allocate color and set font
$color = intval(imagecolorallocate($sourceResource, $r, $g, $b));
if (PathUtility::isUrl($this->fontPath)) {
$font = @file_get_contents($this->fontPath);
if ($font === false) {
throw new \Exception('Failed to download font from: ' . $this->fontPath);
}
file_put_contents($tempFontPath, $this->fontPath);
$fontPath = $tempFontPath;
$isTempFont = true;
} else {
$fontPath = PathUtility::resolveFilePath($this->fontPath);
}
// Add first line of text
if (!empty($this->textLine1)) {
if (!imagettftext($sourceResource, $fontSize, $fontRotation, $fontLocationX, $fontLocationY, $color, $fontPath, $this->textLine1)) {
throw new \Exception('Could not add first line of text to resource.');
}
}
// Add second line of text
if (!empty($this->textLine2)) {
$line2Y = $fontRotation < 45 && $fontRotation > -45 ? $fontLocationY + $textLineSpacing : $fontLocationY;
$line2X = $fontRotation < 45 && $fontRotation > -45 ? $fontLocationX : $fontLocationX + $textLineSpacing;
if (!imagettftext($sourceResource, $fontSize, $fontRotation, $line2X, $line2Y, $color, $fontPath, $this->textLine2)) {
throw new \Exception('Could not add second line of text to resource.');
}
}
// Add third line of text
if (!empty($this->textLine3)) {
$line3Y = $fontRotation < 45 && $fontRotation > -45 ? $fontLocationY + $textLineSpacing * 2 : $fontLocationY;
$line3X = $fontRotation < 45 && $fontRotation > -45 ? $fontLocationX : $fontLocationX + $textLineSpacing * 2;
if (!imagettftext($sourceResource, $fontSize, $fontRotation, $line3X, $line3Y, $color, $fontPath, $this->textLine3)) {
throw new \Exception('Could not add third line of text to resource.');
}
}
if ($isTempFont && file_exists($tempFontPath)) {
if (!unlink($tempFontPath)) {
$this->addErrorData('Failed to delete tmp font: ' . $tempFontPath);
}
}
$this->imageModified = true;
// Return resource with text applied
return $sourceResource;
} catch (\Exception $e) {
$this->addErrorData($e->getMessage());
// Re-throw exception on loglevel > 1
if ($this->debugLevel > 1) {
throw $e;
}
// Return unmodified resource
return $sourceResource;
}
}
/**
* Set the picture options for adding a picture to image resource.
*/
public function setAddPictureOptions(int $x, int $y, int $width, int $height, int $rotation, ?bool $applyFrame = null): void
{
$this->addPictureX = $x;
$this->addPictureY = $y;
$this->addPictureWidth = $width;
$this->addPictureHeight = $height;
$this->addPictureRotation = $rotation;
if ($applyFrame !== null) {
$this->addPictureApplyFrame = $applyFrame;
}
}
/**
* Add a picture to the destination image resource.
*/
public function addPicture(GdImage $imageResource, GdImage $destinationResource): void
{
try {
$dX = intval($this->addPictureX);
$dY = intval($this->addPictureY);
$width = intval($this->addPictureWidth);
$height = intval($this->addPictureHeight);
$degrees = intval($this->addPictureRotation);
if ($width <= 0 || $height <= 0) {
throw new \Exception('Invalid image dimensions or maximum dimensions.');
}
if (abs($degrees) == 90) {
$imageResource = self::resizeCropImage($imageResource, $height, $width);
} else {
$imageResource = self::resizeCropImage($imageResource, $width, $height);
}
if ($this->addPictureApplyFrame) {
$imageResource = self::applyFrame($imageResource);
}
if ($degrees != 0) {
$imageResource = self::rotateResizeImage(
image: $imageResource,
degrees: $degrees,
useTransparentBackground: true
);
if (!$imageResource instanceof \GdImage) {
throw new \Exception('Failed to rotate and resize image.');
}
if (abs($degrees) != 90) {
$width = intval(imagesx($imageResource));
$height = intval(imagesy($imageResource));
}
}
if (!imagecopy($destinationResource, $imageResource, $dX, $dY, 0, 0, $width, $height)) {
throw new \Exception('Can\'t add image to resource.');
}
} catch (\Exception $e) {
$this->addErrorData($e->getMessage());
throw $e;
}
$this->imageModified = true;
}
/**
* Draw a dashed line on the specified image resource.
*/
public function drawDashedLine(GdImage $imageResource): void
{
try {
$dashedLine = [
$this->dashedLineColor,
$this->dashedLineColor,
$this->dashedLineColor,
$this->dashedLineColor,
IMG_COLOR_TRANSPARENT,
IMG_COLOR_TRANSPARENT,
IMG_COLOR_TRANSPARENT,
IMG_COLOR_TRANSPARENT,
];
if (!imagesetstyle($imageResource, $dashedLine)) {
throw new \Exception('Can\'t set the style for line drawing.');
}
if (!imageline($imageResource, $this->dashedLineStartX, $this->dashedLineStartY, $this->dashedLineEndX, $this->dashedLineEndY, IMG_COLOR_STYLED)) {
throw new \Exception('Can\'t draw image line.');
}
} catch (\Exception $e) {
$this->addErrorData($e->getMessage());
// Re-throw exception on loglevel > 1
if ($this->debugLevel > 1) {
throw $e;
}
return;
}
$this->imageModified = true;
}
/**
* Generates a QR code image using the provided URL and configuration settings.
*/
public function createQr(): GdImage
{
try {
if (empty($this->qrUrl)) {
throw new \Exception('No URL for QR-Code generation defined.');
}
if (!is_numeric($this->qrSize)) {
throw new \Exception('QR-Size is not numeric.');
}
if ($this->qrSize % 2 != 0) {
throw new \Exception('QR-Size is not even.');
}
if ($this->qrSize < 2 || $this->qrSize > 10) {
throw new \Exception('QR-Size must be 2, 4, 6, 8 or 10.');
}
if (!is_numeric($this->qrMargin)) {
throw new \Exception('QR-Margin is not numeric.');
}
if ($this->qrMargin < 0 || $this->qrMargin > 10) {
throw new \Exception('QR-Size must be in range between 0 and 10.');
}
$size = $this->qrSize * 40;
$margin = (int)($size / 100 * $this->qrMargin);
$result = QrCodeUtility::create($this->qrUrl, '', $size, $margin);
$qrCodeImage = imagecreatefromstring($result->getString());
if (!$qrCodeImage) {
throw new \Exception('Failed to create image from QR code.');
}
if ($this->qrRotate) {
if (!imagerotate($qrCodeImage, 90, 0)) {
throw new \Exception('Unable to rotate QR-Code-Image.');
}
}
if ($this->qrColor != '#ffffff') {
$qrwidth = imagesx($qrCodeImage);
$qrheight = imagesy($qrCodeImage);
$colorComponents = self::getColorComponents($this->qrColor);
list($r, $g, $b) = $colorComponents;
$selected = intval(imagecolorallocate($qrCodeImage, $r, $g, $b));
for ($xpos = 0; $xpos < $qrwidth; $xpos++) {
for ($ypos = 0; $ypos < $qrheight; $ypos++) {
$currentcolor = intval(imagecolorat($qrCodeImage, $xpos, $ypos));
$parts = imagecolorsforindex($qrCodeImage, $currentcolor);
if ($parts['red'] == 255 && $parts['green'] == 255 && $parts['blue'] == 255) {
imagesetpixel($qrCodeImage, $xpos, $ypos, $selected);
}
}
}
}
return $qrCodeImage;
} catch (\Exception $e) {
$this->addErrorData($e->getMessage());
throw $e;
}
}
/**
* Generates a QR code and displays it as a PNG image.
*/
public function showQR(): void
{
try {
// Generate the QR code
$qrCode = $this->createQr();
// Display the QR code as a PNG image
imagepng($qrCode);
} catch (\Exception $e) {
$this->addErrorData($e->getMessage());
// If an exception is caught, display the error message
echo $e->getMessage();
}
}
/**
* Generates a QR code and saves it to a specified destination path.
*/
public function saveQr(string $destination): bool
{
try {
if (empty($destination)) {
throw new \Exception('No destination path given.');
}
// Generate the QR code
$qrCode = $this->createQr();