forked from WWBN/AVideo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functionsImages.php
1109 lines (982 loc) · 32.7 KB
/
functionsImages.php
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
if (false) {
class Imagick
{
public const FILTER_BOX = 1;
public function getImageFormat()
{
return '';
}
public function coalesceImages()
{
return new Imagick();
}
public function nextImage()
{
return true;
}
public function resizeImage()
{
}
public function deconstructImages()
{
return new Imagick();
}
public function clear()
{
}
public function destroy()
{
}
public function writeImages()
{
}
}
}
function convertThumbsIfNotExists($source, $destination)
{
global $advancedCustom;
if (file_exists($destination)) {
_error_log("convertThumbsIfNotExists destination image exists ");
return true;
}
if (!file_exists($source)) {
_error_log("convertThumbsIfNotExists source image does not exists ");
return false;
}
if (empty($advancedCustom)) {
$advancedCustom = AVideoPlugin::loadPlugin("CustomizeAdvanced");
}
$width = 300;
$height = 300;
$orientation = getImageOrientation($source);
if ($orientation == "landscape") {
$width = $advancedCustom->thumbsWidthLandscape;
$height = $advancedCustom->thumbsHeightLandscape;
} else if ($orientation == "portrait") {
$width = $advancedCustom->thumbsWidthPortrait;
$height = $advancedCustom->thumbsHeightPortrait;
}
return convertImageIfNotExists($source, $destination, $width, $height, true);
}
function getImageOrientation($imagePath)
{
// Get the image dimensions
$imageSize = getimagesize($imagePath);
// Check the width and height
$width = $imageSize[0];
$height = $imageSize[1];
// Determine the orientation
if ($width > $height) {
return "landscape";
} else if ($width < $height) {
return "portrait";
} else {
return "square";
}
}
/**
* Check whether an image is fully transparent.
*
* @param string $filename The path to the image file.
* @return bool True if the image is fully transparent, false otherwise.
*/
function is_image_fully_transparent($filename)
{
if(filesize($filename)>10000){
return false;
}
// Load the image
$image = imagecreatefrompng($filename);
// Get the number of colors in the image
$num_colors = imagecolorstotal($image);
// Loop through each color and check if it's fully transparent
$is_transparent = true;
for ($i = 0; $i < $num_colors; $i++) {
$color = imagecolorsforindex($image, $i);
if ($color['alpha'] != 127) { // 127 is the maximum value for a fully transparent color
$is_transparent = false;
break;
}
}
// Free up memory
imagedestroy($image);
// Return the result
return $is_transparent;
}
function totalImageColors($image_path)
{
$img = imagecreatefromjpeg($image_path);
$w = imagesx($img);
$h = imagesy($img);
// capture the raw data of the image
_ob_start();
imagegd2($img, null, $w);
$data = _ob_get_clean();
$totalLength = strlen($data);
// calculate the length of the actual pixel data
// from that we can derive the header size
$pixelDataLength = $w * $h * 4;
$headerLength = $totalLength - $pixelDataLength;
// use each four-byte segment as the key to a hash table
$counts = [];
for ($i = $headerLength; $i < $totalLength; $i += 4) {
$pixel = substr($data, $i, 4);
$count = &$counts[$pixel];
$count += 1;
}
$colorCount = count($counts);
return $colorCount;
}
function isImageCorrupted($image_path)
{
$fsize = filesize($image_path);
if (strpos($image_path, 'thumbsSmall') !== false) {
if ($fsize < 1000) {
return true;
}
} else {
if ($fsize < 2000) {
return true;
}
}
if (totalImageColors($image_path) === 1) {
return true;
}
if (!isGoodImage($image_path)) {
return true;
}
return false;
}
// detect partial grey immages
function isGoodImage($fn)
{
[$w, $h] = getimagesize($fn);
$im = imagecreatefromstring(file_get_contents($fn));
$grey = 0;
for ($i = 0; $i < 5; ++$i) {
for ($j = 0; $j < 5; ++$j) {
$x = $w - 5 + $i;
$y = $h - 5 + $j;
//[$r, $g, $b] = array_values(imagecolorsforindex($im, imagecolorat($im, $x, $y)));
[$r, $g, $b] = imagecolorsforindex($im, imagecolorat($im, $x, $y));
if ($r == $g && $g == $b && $b == 128) {
++$grey;
}
}
}
return $grey < 12;
}
function defaultIsPortrait()
{
global $_defaultIsPortrait;
if (!isset($_defaultIsPortrait)) {
$_defaultIsPortrait = false;
$obj = AVideoPlugin::getDataObjectIfEnabled('YouPHPFlix2');
if (!empty($obj) && empty($obj->landscapePosters)) {
$_defaultIsPortrait = true;
}
}
return $_defaultIsPortrait;
}
function defaultIsLandscape()
{
return !defaultIsPortrait();
}
function fileIsAnValidImage($filepath)
{
if (file_exists($filepath)) {
if (filesize($filepath) === 42342) {
return false;
} else if (!function_exists('exif_imagetype')) {
if ((list($width, $height, $type, $attr) = getimagesize($filepath)) !== false) {
return $type;
}
} else {
return exif_imagetype($filepath);
}
}
return false;
}
/**
* return true if de file was deleted or does not exits and false if the file still present on the system
* @param string $filepath
* @return boolean
*/
function deleteInvalidImage($filepath)
{
if (file_exists($filepath)) {
if (!fileIsAnValidImage($filepath)) {
_error_log("deleteInvalidImage($filepath)");
unlink($filepath);
return true;
}
return false;
}
return true;
}
function isImage($file)
{
[$width, $height, $type, $attr] = getimagesize($file);
if ($type == IMAGETYPE_PNG) {
return 'png';
}
if ($type == IMAGETYPE_JPEG) {
return 'jpg';
}
if ($type == IMAGETYPE_GIF) {
return 'gif';
}
return false;
}
function convertImageToOG($source, $destination)
{
if (!file_exists($destination)) {
$w = 200;
$h = 200;
$sizes = getimagesize($source);
if ($sizes[0] < $w || $sizes[1] < $h) {
$tmpDir = getTmpDir();
$fileConverted = $tmpDir . "_jpg_" . uniqid() . ".jpg";
convertImage($source, $fileConverted, 100);
im_resize($fileConverted, $destination, $w, $h, 100);
//_error_log("convertImageToOG ($destination) unlink line=".__LINE__);
@unlink($fileConverted);
}
}
return $destination;
}
function convertImageToRoku($source, $destination)
{
return convertImageIfNotExists($source, $destination, 1280, 720, true);
}
function convertImageIfNotExists($source, $destination, $width, $height, $scaleUp = true)
{
if (empty($source)) {
_error_log("convertImageIfNotExists: source image is empty");
return false;
}
$source = str_replace(['_thumbsSmallV2'], [''], $source);
if (!file_exists($source)) {
//_error_log("convertImageIfNotExists: source does not exists {$source}");
return false;
}
if (empty(filesize($source))) {
_error_log("convertImageIfNotExists: source has filesize 0");
return false;
}
$mime = mime_content_type($source);
if ($mime == 'text/plain') {
_error_log("convertImageIfNotExists error, image in wrong format/mime type {$source} " . file_get_contents($source));
unlink($source);
return false;
}
if (file_exists($destination) && filesize($destination) > 1024) {
$sizes = getimagesize($destination);
if ($sizes[0] < $width && $sizes[1] < $height) {
_error_log("convertImageIfNotExists: $destination, w=$width, h=$height file is smaller unlink " . json_encode($sizes));
unlink($destination);
return false;
}
}
if (!file_exists($destination)) {
//_error_log("convertImageIfNotExists($source, $destination, $width, $height)");
try {
$tmpDir = getTmpDir();
$fileConverted = $tmpDir . "_jpg_" . uniqid() . ".jpg";
convertImage($source, $fileConverted, 100);
if (file_exists($fileConverted)) {
if ($scaleUp) {
scaleUpImage($fileConverted, $fileConverted, $width, $height);
}
if (file_exists($fileConverted)) {
im_resize($fileConverted, $destination, $width, $height, 100);
if (!file_exists($destination)) {
_error_log("convertImageIfNotExists: [$fileConverted] [$source] [$destination]");
}else{
_error_log("convertImageIfNotExists: ($source, $destination) line=".__LINE__);
}
} else {
_error_log("convertImageIfNotExists: convertImage error 1 $source, $fileConverted");
}
} else {
_error_log("convertImageIfNotExists: convertImage error 2 $source, $fileConverted");
}
@unlink($fileConverted);
} catch (Exception $exc) {
_error_log("convertImageIfNotExists: " . $exc->getMessage());
return false;
}
}
return $destination;
}
function getVideoImagewithHoverAnimation($relativePath, $relativePathHoverAnimation = '', $title = '', $preloadImage = false, $doNotUseAnimatedGif = false)
{
$id = uniqid();
//getImageTagIfExists($relativePath, $title = '', $id = '', $style = '', $class = 'img img-responsive', $lazyLoad = false, $preloadImage=false)
$img = getImageTagIfExists($relativePath, $title, "thumbsJPG{$id}", '', 'thumbsJPG img img-responsive', false, $preloadImage) . PHP_EOL;
if (empty($doNotUseAnimatedGif) && !empty($relativePathHoverAnimation) && empty($_REQUEST['noImgGif'])) {
$img .= getImageTagIfExists($relativePathHoverAnimation, $title, "thumbsGIF{$id}", 'position: absolute; top: 0;', 'thumbsGIF img img-responsive ', true, $preloadImage) . PHP_EOL;
}
return '<div class="thumbsImage">' . $img . '</div>';
}
function getImageTagIfExists($relativePath, $title = '', $id = '', $style = '', $class = 'img img-responsive', $lazyLoad = false, $preloadImage = false)
{
global $global;
$relativePathOriginal = $relativePath;
$relativePath = getRelativePath($relativePath);
$file = "{$global['systemRootPath']}{$relativePath}";
$wh = '';
if (file_exists($file)) {
// check if there is a thumbs
if (!preg_match('/_thumbsV2.jpg/', $file)) {
$thumbs = str_replace('.jpg', '_thumbsV2.jpg', $file);
if (file_exists($thumbs)) {
$file = $thumbs;
}
}
if (get_browser_name() !== 'Safari') {
$file = createWebPIfNotExists($file);
}
$url = getURL(getRelativePath($file));
//var_dump($relativePath, $file, $url);exit;
if(ImagesPlaceHolders::isDefaultImage($url)){
$class .= ' ImagesPlaceHoldersDefaultImage';
}
if (file_exists($file)) {
$image_info = @getimagesize($file);
if (!empty($image_info)) {
$wh = $image_info[3];
}
}
} elseif (isValidURL($relativePathOriginal)) {
$url = $relativePathOriginal;
} else {
return '<!-- invalid URL ' . $relativePathOriginal . ' -->';
}
if (empty($title)) {
$title = basename($relativePath);
}
$title = safeString($title);
$img = "<img style=\"{$style}\" alt=\"{$title}\" title=\"{$title}\" id=\"{$id}\" class=\"{$class}\" {$wh} ";
if (empty($preloadImage) && $lazyLoad) {
if (is_string($lazyLoad)) {
$loading = getURL($lazyLoad);
} else {
$loading = ImagesPlaceHolders::getVideoPlaceholder(ImagesPlaceHolders::$RETURN_URL);
}
$img .= " src=\"{$loading}\" data-src=\"{$url}\" ";
} else {
$img .= " src=\"{$url}\" ";
}
$img .= "/>";
if ($preloadImage) {
$img = "<link rel=\"prefetch\" href=\"{$url}\" />" . $img;
}
return $img;
}
function im_resize_gif($file_src, $file_dest, $max_width, $max_height)
{
if (class_exists('Imagick')) {
$imagick = new Imagick($file_src);
$format = $imagick->getImageFormat();
if ($format == 'GIF') {
$imagick = $imagick->coalesceImages();
do {
$imagick->resizeImage($max_width, $max_height, Imagick::FILTER_BOX, 1);
} while ($imagick->nextImage());
$imagick = $imagick->deconstructImages();
$imagick->writeImages($file_dest, true);
}
$imagick->clear();
$imagick->destroy();
} else {
copy($file_src, $file_dest);
}
}
function im_resize_max_size($file_src, $file_dest, $max_width, $max_height)
{
$fn = $file_src;
$extension = mb_strtolower(pathinfo($file_dest, PATHINFO_EXTENSION));
if ($extension == 'gif') {
im_resize_gif($file_src, $file_dest, $max_width, $max_height);
_error_log("im_resize_max_size($file_src) unlink line=".__LINE__);
@unlink($file_src);
return true;
}
$tmpFile = getTmpFile() . ".{$extension}";
if (empty($fn)) {
_error_log("im_resize_max_size: file name is empty, Destination: {$file_dest}", AVideoLog::$ERROR);
return false;
}
if (function_exists("exif_read_data")) {
error_log($fn);
convertImage($fn, $tmpFile, 100);
$exif = exif_read_data($tmpFile);
if ($exif && isset($exif['Orientation'])) {
$orientation = $exif['Orientation'];
if ($orientation != 1) {
$img = imagecreatefromjpeg($tmpFile);
$deg = 0;
switch ($orientation) {
case 3:
$deg = 180;
break;
case 6:
$deg = 270;
break;
case 8:
$deg = 90;
break;
}
if ($deg) {
$img = imagerotate($img, $deg, 0);
}
imagejpeg($img, $fn, 100);
}
}
} else {
_error_log("Make sure you install the php_mbstring and php_exif to be able to rotate images");
}
$size = getimagesize($fn);
$ratio = $size[0] / $size[1]; // width/height
if ($size[0] <= $max_width && $size[1] <= $max_height) {
$width = $size[0];
$height = $size[1];
} elseif ($ratio > 1) {
$width = $max_width;
$height = $max_height / $ratio;
} else {
$width = $max_width * $ratio;
$height = $max_height;
}
$src = imagecreatefromstring(file_get_contents($fn));
$dst = imagecreatetruecolor($width, $height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
imagedestroy($src);
imagejpeg($dst, $file_dest); // adjust format as needed
imagedestroy($dst);
_error_log("im_resize_max_size($file_src) unlink line=".__LINE__);
@unlink($file_src);
_error_log("im_resize_max_size($tmpFile) unlink line=".__LINE__);
@unlink($tmpFile);
}
function detect_image_type($file_path)
{
$image_info = @getimagesize($file_path);
if ($image_info !== false) {
$mime_type = $image_info['mime'];
switch ($mime_type) {
case 'image/jpeg':
return IMAGETYPE_JPEG;
case 'image/png':
return IMAGETYPE_PNG;
case 'image/gif':
return IMAGETYPE_GIF;
case 'image/bmp':
return IMAGETYPE_BMP;
case 'image/webp':
return IMAGETYPE_WEBP;
case 'image/x-icon':
return IMAGETYPE_ICO;
default:
return false;
}
} else {
return false;
}
}
/**
*
* @param string $file_src
* @return array get image size with cache
*/
function getimgsize($file_src)
{
global $_getimagesize;
if (empty($file_src) || !file_exists($file_src)) {
return [0, 0];
}
if (empty($_getimagesize)) {
$_getimagesize = [];
}
$name = "getimgsize_" . md5($file_src);
if (!empty($_getimagesize[$name])) {
$size = $_getimagesize[$name];
} else {
$cached = ObjectYPT::getCacheGlobal($name, 86400); //one day
if (!empty($cached)) {
$c = (array) $cached;
$size = [];
foreach ($c as $key => $value) {
if (preg_match("/^[0-9]+$/", $key)) {
$key = intval($key);
}
$size[$key] = $value;
}
$_getimagesize[$name] = $size;
return $size;
}
$size = @getimagesize($file_src);
if (empty($size)) {
$size = [1024, 768];
}
ObjectYPT::setCacheGlobal($name, $size);
$_getimagesize[$name] = $size;
}
return $size;
}
function getImageFormat($file)
{
$size = getimgsize($file);
if ($size === false) {
return false;
}
if (empty($size['mime']) || $size['mime'] == 'image/pjpeg') {
$size['mime'] = 'image/jpeg';
}
//var_dump($file_src, $size);exit;
$format = mb_strtolower(substr($size['mime'], strpos($size['mime'], '/') + 1));
$extension = $format;
if (empty($format)) {
$extension = mb_strtolower(pathinfo($file, PATHINFO_EXTENSION));
if ($extension === 'jpg') {
$format = 'jpeg';
} else {
$size = getimgsize($file);
if ($size === false) {
return false;
}
if (empty($size['mime']) || $size['mime'] == 'image/pjpeg') {
$size['mime'] = 'image/jpeg';
}
//var_dump($file_src, $size);exit;
$format = mb_strtolower(substr($size['mime'], strpos($size['mime'], '/') + 1));
$extension = $format;
if (empty($format)) {
$format = 'jpeg';
$extension = 'jpg';
}
}
}
return ['format' => $format, 'extension' => $extension];
}
function im_resize($file_src, $file_dest, $wd, $hd, $q = 80)
{
if (empty($file_dest)) {
return false;
}
if (preg_match('/notfound_/', $file_dest)) {
return false;
}
if (!file_exists($file_src)) {
_error_log("im_resize: Source not found: {$file_src}");
return false;
}
$format = getImageFormat($file_src);
$destformat = mb_strtolower(pathinfo($file_dest, PATHINFO_EXTENSION));
$icfunc = "imagecreatefrom" . $format['format'];
if (!function_exists($icfunc)) {
_error_log("im_resize: Function does not exists: {$icfunc}");
return false;
}
if (!file_exists($file_src)) {
return false;
}
$imgSize = getimagesize($file_src);
if (empty($imgSize)) {
_error_log("im_resize: getimagesize($file_src) return false " . json_encode($imgSize));
return false;
}
try {
//var_dump($file_src, $icfunc);
$src = $icfunc($file_src);
} catch (Exception $exc) {
_error_log("im_resize: ($file_src) " . $exc->getMessage());
_error_log("im_resize: Try {$icfunc} from string");
$src = imagecreatefromstring(file_get_contents($file_src));
if (!$src) {
_error_log("im_resize: fail {$icfunc} from string");
return false;
}
}
if (is_bool($src)) {
//_error_log("im_resize error on source {$file_src} ", AVideoLog::$ERROR);
return false;
}
$ws = imagesx($src);
$hs = imagesy($src);
if ($ws <= $hs) {
$hd = ceil(($wd * $hs) / $ws);
} else {
$wd = ceil(($hd * $ws) / $hs);
}
if ($ws <= $wd) {
$wd = $ws;
$hd = $hs;
}
if (empty($hd)) {
$hd = $hs;
}
if (empty($wd)) {
$wd = $ws;
}
$wc = ($wd * $hs) / $hd;
if ($wc <= $ws) {
$hc = ($wc * $hd) / $wd;
} else {
$hc = ($ws * $hd) / $wd;
$wc = ($wd * $hc) / $hd;
}
$dest = imagecreatetruecolor($wd, $hd);
switch ($format) {
case "png":
imagealphablending($dest, false);
imagesavealpha($dest, true);
$transparent = imagecolorallocatealpha($dest, 255, 255, 255, 127);
imagefilledrectangle($dest, 0, 0, $wd, $hd, $transparent);
break;
case "gif":
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($src, 0, 0, 0);
// removing the black from the placeholder
imagecolortransparent($src, $background);
break;
}
imagecopyresampled($dest, $src, 0, 0, ($ws - $wc) / 2, ($hs - $hc) / 2, $wd, $hd, $wc, $hc);
$saved = false;
if ($destformat === 'png') {
$saved = imagepng($dest, $file_dest);
} elseif ($destformat === 'jpg') {
$saved = imagejpeg($dest, $file_dest, $q);
} elseif ($destformat === 'webp') {
$saved = imagewebp($dest, $file_dest, $q);
} elseif ($destformat === 'gif') {
$saved = imagegif($dest, $file_dest);
}
if (!$saved) {
_error_log("im_resize: saving failed $file_src, $file_dest");
}
imagedestroy($dest);
imagedestroy($src);
@chmod($file_dest, 0666);
return true;
}
function scaleUpAndMantainAspectRatioFinalSizes($new_w, $old_w, $new_h, $old_h)
{
if (empty($old_h)) {
$old_h = $new_h;
}
if (empty($new_h)) {
$new_h = $old_h;
}
if (empty($old_w)) {
$old_w = $new_w;
}
if (empty($new_w)) {
$new_w = $old_w;
}
if (empty($old_h) || empty($new_h)) {
// Return an error or handle the case accordingly
return ['w' => 0, 'h' => 0];
}
$aspect_ratio_src = $old_w / $old_h;
$aspect_ratio_new = $new_w / $new_h;
if ($aspect_ratio_src > $aspect_ratio_new) {
// The source image is wider than the specified dimensions
$thumb_w = $new_w;
$thumb_h = $old_h * ($new_w / $old_w);
} else {
// The source image is taller than the specified dimensions
$thumb_w = $old_w * ($new_h / $old_h);
$thumb_h = $new_h;
}
return ['w' => $thumb_w, 'h' => $thumb_h];
}
function scaleUpImage($file_src, $file_dest, $wd, $hd)
{
if (!file_exists($file_src)) {
return false;
}
$path = $file_src;
$newWidth = $wd;
$newHeight = $hd;
$new_thumb_loc = $file_dest;
$mime = getimagesize($path);
if (empty($mime)) {
$mime = mime_content_type($path);
if ($mime == 'text/plain') {
_error_log("scaleUpImage error, image in wrong format/mime type {$path} " . file_get_contents($path));
unlink($path);
return false;
}
_error_log("scaleUpImage error, undefined mime ".humanFileSize(filesize($file_src)));
return false;
}
switch ($mime['mime']) {
case 'image/png':
$src_img = imagecreatefrompng($path);
break;
case 'image/jpg':
case 'image/jpeg':
case 'image/pjpeg':
$src_img = imagecreatefromjpeg($path);
break;
case 'image/webp':
$src_img = imagecreatefromwebp($path);
break;
default:
_error_log("Unsupported image type: " . $mime['mime']);
return false;
}
if (empty($src_img)) {
_error_log("scaleUpImage error, we could not convert it [" . json_encode($mime) . "] " . json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)));
return false;
}
$old_x = imageSX($src_img);
$old_y = imageSY($src_img);
$sizes = scaleUpAndMantainAspectRatioFinalSizes($wd, $old_x, $hd, $old_y);
/*
if($wd!==200){
echo "<h1>Original</h1>X={$old_x} Y={$old_y}";
echo "<h1>Destination</h1>X={$wd} Y={$hd}";
echo '<h1>Results</h1>';
var_dump($sizes);exit;
}
*
*/
$thumb_w = intval($sizes['w']);
$thumb_h = intval($sizes['h']);
$dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y);
switch ($mime['mime']) {
case 'image/png':
$result = imagepng($dst_img, $new_thumb_loc, 8);
break;
case 'image/jpg':
case 'image/jpeg':
case 'image/pjpeg':
$result = imagejpeg($dst_img, $new_thumb_loc, 80);
break;
case 'image/webp':
$result = imagewebp($dst_img, $new_thumb_loc, 80);
break;
default:
_error_log("scaleUpImage error, unsupported mime type: " . $mime['mime']);
return false;
}
imagedestroy($dst_img);
imagedestroy($src_img);
return $result;
}
function resize_png_image($source_file_path, $destination_file_path, $target_width, $target_height)
{
// Check if the source file exists
if (!file_exists($source_file_path)) {
return false;
}
// Validate the target width and height
if ($target_width <= 0 || $target_height <= 0) {
return false;
}
$src_image = imagecreatefrompng($source_file_path);
$src_width = imagesx($src_image);
$src_height = imagesy($src_image);
$target_image = imagecreatetruecolor($target_width, $target_height);
imagealphablending($target_image, true);
imagesavealpha($target_image, true);
imagecopyresampled(
$target_image,
$src_image,
0,
0,
0,
0,
$target_width,
$target_height,
$src_width,
$src_height
);
$saved = imagepng($target_image, $destination_file_path);
return $saved;
}
function convertImage($originalImage, $outputImage, $quality, $useExif = false)
{
ini_set('memory_limit', '512M');
if (!file_exists($originalImage) || empty(filesize($originalImage))) {
return false;
}
$originalImage = str_replace('"', '', $originalImage);
$outputImage = str_replace('"', '', $outputImage);
make_path($outputImage);
$imagetype = 0;
if (!empty($useExif) && function_exists('exif_imagetype')) {
$imagetype = exif_imagetype($originalImage);
} else {
$imagetype = detect_image_type($originalImage);
}
$ext = mb_strtolower(pathinfo($originalImage, PATHINFO_EXTENSION));
$extOutput = mb_strtolower(pathinfo($outputImage, PATHINFO_EXTENSION));
if ($ext == $extOutput) {
//_error_log("convertImage: same extension $ext == $extOutput [$originalImage, $outputImage]");
return copy($originalImage, $outputImage);
}
try {
if ($imagetype == IMAGETYPE_WEBP) {
//_error_log("convertImage: IMAGETYPE_WEBP");
$imageTmp = imagecreatefromwebp($originalImage);
if (!$imageTmp) {
_error_log("convertImage: imagecreatefromwebp error $originalImage [$imagetype] $useExif");
if (!$useExif) {
return convertImage($originalImage, $outputImage, $quality, true);
}
$supported_extensions = ['jpeg', 'png', 'bmp', 'gif'];
foreach ($supported_extensions as $ext) {
$function_name = "imagecreatefrom$ext";
$imageTmp = @$function_name($originalImage);
if ($imageTmp) {
break;
} else {
//_error_log("convertImage: Could not create image resource using $function_name");
}
}
if (!$imageTmp) {
copy($originalImage, $outputImage);
_error_log("convertImage: Could not create image resource for $originalImage we will just copy it");
return false;
}
}
}
if (empty($imageTmp)) {
if ($imagetype === IMAGETYPE_JPEG || preg_match('/jpg|jpeg/i', $ext)) {
//_error_log("convertImage: IMAGETYPE_JPEG");
$imageTmp = imagecreatefromjpeg($originalImage);
} elseif ($imagetype == IMAGETYPE_PNG || preg_match('/png/i', $ext)) {
//_error_log("convertImage: IMAGETYPE_PNG");
$imageTmp = imagecreatefrompng($originalImage);
} elseif ($imagetype == IMAGETYPE_GIF || preg_match('/gif/i', $ext)) {
//_error_log("convertImage: IMAGETYPE_GIF");
$imageTmp = imagecreatefromgif($originalImage);
} elseif ($imagetype == IMAGETYPE_BMP || preg_match('/bmp/i', $ext)) {
//_error_log("convertImage: IMAGETYPE_BMP");
$imageTmp = imagecreatefrombmp($originalImage);
} elseif ($imagetype == IMAGETYPE_WEBP || preg_match('/webp/i', $ext)) {
//_error_log("convertImage: IMAGETYPE_WEBP");
$imageTmp = imagecreatefromwebp($originalImage);
} else {
_error_log("convertImage: File Extension not found ($originalImage, $outputImage, $quality) " . exif_imagetype($originalImage));
return 0;
}
}
} catch (Exception $exc) {
_error_log("convertImage: " . $exc->getMessage());
return 0;
}
if ($imageTmp === false) {
//_error_log("convertImage: could not create a resource: [$imagetype] $originalImage, $outputImage, $quality, $ext ");
return 0;
}
// quality is a value from 0 (worst) to 100 (best)
$response = 0;
if ($extOutput === 'jpg') {
if (function_exists('imagejpeg')) {
$response = imagejpeg($imageTmp, $outputImage, $quality);
} else {
_error_log("convertImage ERROR: function imagejpeg does not exists");
}
} elseif ($extOutput === 'png') {