1: <?php
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: class imageLib
164: {
165:
166: private $fileName;
167: private $image;
168: protected $imageResized;
169: private $widthOriginal;
170: private $heightOriginal;
171: private $width;
172: private $height;
173: private $imageSize;
174: private $fileExtension;
175:
176: private $debug = true;
177: private $errorArray = array();
178:
179: private $forceStretch = true;
180: private $aggresiveSharpening = false;
181:
182: private $transparentArray = array('.png', '.gif');
183: private $keepTransparency = true;
184: private $fillColorArray = array('r'=>255, 'g'=>255, 'b'=>255);
185:
186: private $sharpenArray = array('jpg');
187:
188: private $psdReaderPath;
189: private $filterOverlayPath;
190:
191: private $isInterlace;
192:
193: private $captionBoxPositionArray = array();
194:
195: private $fontDir = 'fonts';
196:
197: private $cropFromTopPercent = 10;
198:
199:
200:
201:
202: function __construct($fileName)
203:
204:
205:
206:
207:
208:
209:
210:
211: {
212: if (!$this->testGDInstalled()) { if ($this->debug) { throw new Exception('The GD Library is not installed.'); }else{ throw new Exception(); }};
213:
214: $this->initialise();
215:
216:
217: $this->fileName = $fileName;
218: $this->fileExtension = fix_strtolower(strrchr($fileName, '.'));
219:
220:
221: $this->image = $this->openImage($fileName);
222:
223:
224:
225: $this->imageResized = $this->image;
226:
227:
228: if ($this->testIsImage($this->image))
229: {
230:
231: $this->width = imagesx($this->image);
232: $this->widthOriginal = imagesx($this->image);
233: $this->height = imagesy($this->image);
234: $this->heightOriginal = imagesy($this->image);
235:
236:
237: 238: 239: 240: 241: 242: 243: 244: 245:
246: $this->imageSize = getimagesize($this->fileName);
247:
248: } else {
249: $this->errorArray[] = 'File is not an image';
250: }
251: }
252:
253:
254:
255: private function initialise () {
256:
257: $this->psdReaderPath = __DIR__ . '/classPhpPsdReader.php';
258: $this->filterOverlayPath = __DIR__ . '/filters';
259:
260:
261: $this->isInterlace = false;
262: }
263:
264:
265:
266: 267: 268:
269:
270:
271: public function resizeImage($newWidth, $newHeight, $option = 0, $sharpen = false, $autoRotate = false)
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: $cropPos = 'm';
313: if (is_array($option) && fix_strtolower($option[0]) === 'crop') {
314: $cropPos = $option[1];
315: } else if (strpos($option, '-') !== false) {
316:
317: $optionPiecesArray = explode('-', $option);
318: $cropPos = end($optionPiecesArray);
319: }
320:
321:
322: $option = $this->prepOption($option);
323:
324:
325: if (!$this->image) { if ($this->debug) { throw new Exception('file ' . $this->getFileName() .' is missing or invalid'); }else{ throw new Exception(); }};
326:
327:
328: $dimensionsArray = $this->getDimensions($newWidth, $newHeight, $option);
329:
330: $optimalWidth = $dimensionsArray['optimalWidth'];
331: $optimalHeight = $dimensionsArray['optimalHeight'];
332:
333:
334: $this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight);
335: $this->keepTransparancy($optimalWidth, $optimalHeight, $this->imageResized);
336: imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height);
337:
338:
339:
340: if ($option == 4 || $option === 'crop') {
341:
342: if (($optimalWidth >= $newWidth && $optimalHeight >= $newHeight)) {
343: $this->crop($optimalWidth, $optimalHeight, $newWidth, $newHeight, $cropPos);
344: }
345: }
346:
347:
348: if ($autoRotate) {
349:
350: $exifData = $this->getExif(false);
351: if (count($exifData) > 0) {
352:
353: switch($exifData['orientation']) {
354: case 8:
355: $this->imageResized = imagerotate($this->imageResized,90,0);
356: break;
357: case 3:
358: $this->imageResized = imagerotate($this->imageResized,180,0);
359: break;
360: case 6:
361: $this->imageResized = imagerotate($this->imageResized,-90,0);
362: break;
363: }
364: }
365: }
366:
367:
368: if ($sharpen && in_array($this->fileExtension, $this->sharpenArray)) {
369:
370:
371: $this->sharpen();
372: }
373: }
374:
375:
376:
377: public function cropImage($newWidth, $newHeight, $cropPos = 'm')
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391: {
392:
393:
394: if (!$this->image) { if ($this->debug) { throw new Exception('file ' . $this->getFileName() .' is missing or invalid'); }else{ throw new Exception(); }};
395:
396: $this->imageResized = $this->image;
397: $this->crop($this->width, $this->height, $newWidth, $newHeight, $cropPos);
398:
399: }
400:
401:
402:
403: private function keepTransparancy($width, $height, $im)
404:
405:
406:
407:
408:
409:
410:
411:
412: {
413:
414: if (in_array($this->fileExtension, $this->transparentArray) && $this->keepTransparency) {
415: imagealphablending($im, false);
416: imagesavealpha($im, true);
417: $transparent = imagecolorallocatealpha($im, 255, 255, 255, 127);
418: imagefilledrectangle($im, 0, 0, $width, $height, $transparent);
419: } else {
420: $color = imagecolorallocate($im, $this->fillColorArray['r'], $this->fillColorArray['g'], $this->fillColorArray['b']);
421: imagefilledrectangle($im, 0, 0, $width, $height, $color);
422: }
423: }
424:
425:
426:
427: private function crop($optimalWidth, $optimalHeight, $newWidth, $newHeight, $cropPos)
428:
429:
430:
431:
432:
433:
434:
435:
436:
437: {
438:
439:
440: $cropArray = $this->getCropPlacing($optimalWidth, $optimalHeight, $newWidth, $newHeight, $cropPos);
441: $cropStartX = $cropArray['x'];
442: $cropStartY = $cropArray['y'];
443:
444:
445: $crop = imagecreatetruecolor($newWidth , $newHeight);
446: $this->keepTransparancy($optimalWidth, $optimalHeight, $crop);
447: imagecopyresampled($crop, $this->imageResized, 0, 0, $cropStartX, $cropStartY, $newWidth, $newHeight , $newWidth, $newHeight);
448:
449: $this->imageResized = $crop;
450:
451:
452: $this->width = $newWidth;
453: $this->height = $newHeight;
454:
455: }
456:
457:
458:
459: private function getCropPlacing($optimalWidth, $optimalHeight, $newWidth, $newHeight, $pos='m')
460:
461:
462:
463:
464:
465:
466:
467:
468:
469:
470: {
471: $pos = fix_strtolower($pos);
472:
473:
474: if (strstr($pos, 'x')) {
475: $pos = str_replace(' ', '', $pos);
476:
477: $xyArray = explode('x', $pos);
478: list($cropStartX, $cropStartY) = $xyArray;
479:
480: } else {
481:
482: switch ($pos) {
483: case 'tl':
484: $cropStartX = 0;
485: $cropStartY = 0;
486: break;
487:
488: case 't':
489: $cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
490: $cropStartY = 0;
491: break;
492:
493: case 'tr':
494: $cropStartX = $optimalWidth - $newWidth;
495: $cropStartY = 0;
496: break;
497:
498: case 'l':
499: $cropStartX = 0;
500: $cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 );
501: break;
502:
503: case 'm':
504: $cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
505: $cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 );
506: break;
507:
508: case 'r':
509: $cropStartX = $optimalWidth - $newWidth;
510: $cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 );
511: break;
512:
513: case 'bl':
514: $cropStartX = 0;
515: $cropStartY = $optimalHeight - $newHeight;
516: break;
517:
518: case 'b':
519: $cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
520: $cropStartY = $optimalHeight - $newHeight;
521: break;
522:
523: case 'br':
524: $cropStartX = $optimalWidth - $newWidth;
525: $cropStartY = $optimalHeight - $newHeight;
526: break;
527:
528: case 'auto':
529:
530: if ($optimalHeight > $optimalWidth) {
531: $cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
532: $cropStartY = ($this->cropFromTopPercent /100) * $optimalHeight;
533: } else {
534:
535:
536: $cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
537: $cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 );
538: }
539: break;
540:
541: default:
542:
543: $cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
544: $cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 );
545: break;
546: }
547: }
548:
549: return array('x' => $cropStartX, 'y' => $cropStartY);
550: }
551:
552:
553:
554: private function getDimensions($newWidth, $newHeight, $option)
555:
556:
557:
558:
559:
560:
561:
562:
563:
564:
565:
566:
567:
568:
569:
570:
571:
572:
573:
574:
575: {
576:
577: switch ((string)($option))
578: {
579: case '0':
580: case 'exact':
581: $optimalWidth = $newWidth;
582: $optimalHeight= $newHeight;
583: break;
584: case '1':
585: case 'portrait':
586: $dimensionsArray = $this->getSizeByFixedHeight($newWidth, $newHeight);
587: $optimalWidth = $dimensionsArray['optimalWidth'];
588: $optimalHeight = $dimensionsArray['optimalHeight'];
589: break;
590: case '2':
591: case 'landscape':
592: $dimensionsArray = $this->getSizeByFixedWidth($newWidth, $newHeight);
593: $optimalWidth = $dimensionsArray['optimalWidth'];
594: $optimalHeight = $dimensionsArray['optimalHeight'];
595: break;
596: case '3':
597: case 'auto':
598: $dimensionsArray = $this->getSizeByAuto($newWidth, $newHeight);
599: $optimalWidth = $dimensionsArray['optimalWidth'];
600: $optimalHeight = $dimensionsArray['optimalHeight'];
601: break;
602: case '4':
603: case 'crop':
604: $dimensionsArray = $this->getOptimalCrop($newWidth, $newHeight);
605: $optimalWidth = $dimensionsArray['optimalWidth'];
606: $optimalHeight = $dimensionsArray['optimalHeight'];
607: break;
608: }
609:
610: return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
611: }
612:
613:
614:
615: private function getSizeByFixedHeight($newWidth, $newHeight)
616: {
617:
618: if (!$this->forceStretch) {
619:
620:
621: if ($this->height < $newHeight) {
622: return array('optimalWidth' => $this->width, 'optimalHeight' => $this->height);
623: }
624: }
625:
626: $ratio = $this->width / $this->height;
627:
628: $newWidth = $newHeight * $ratio;
629:
630:
631: return array('optimalWidth' => $newWidth, 'optimalHeight' => $newHeight);
632: }
633:
634:
635:
636: private function getSizeByFixedWidth($newWidth, $newHeight)
637: {
638:
639: if (!$this->forceStretch) {
640:
641:
642: if ($this->width < $newWidth) {
643: return array('optimalWidth' => $this->width, 'optimalHeight' => $this->height);
644: }
645: }
646:
647: $ratio = $this->height / $this->width;
648:
649: $newHeight = $newWidth * $ratio;
650:
651:
652: return array('optimalWidth' => $newWidth, 'optimalHeight' => $newHeight);
653: }
654:
655:
656:
657: private function getSizeByAuto($newWidth, $newHeight)
658:
659:
660:
661:
662:
663:
664: {
665:
666: if (!$this->forceStretch) {
667:
668:
669: if ($this->width < $newWidth && $this->height < $newHeight) {
670: return array('optimalWidth' => $this->width, 'optimalHeight' => $this->height);
671: }
672: }
673:
674: if ($this->height < $this->width)
675:
676: {
677:
678:
679:
680: $dimensionsArray = $this->getSizeByFixedWidth($newWidth, $newHeight);
681: $optimalWidth = $dimensionsArray['optimalWidth'];
682: $optimalHeight = $dimensionsArray['optimalHeight'];
683: }
684: elseif ($this->height > $this->width)
685:
686: {
687:
688:
689:
690: $dimensionsArray = $this->getSizeByFixedHeight($newWidth, $newHeight);
691: $optimalWidth = $dimensionsArray['optimalWidth'];
692: $optimalHeight = $dimensionsArray['optimalHeight'];
693: }
694: else
695:
696: {
697:
698: if ($newHeight < $newWidth) {
699:
700:
701: $dimensionsArray = $this->getSizeByFixedWidth($newWidth, $newHeight);
702: $optimalWidth = $dimensionsArray['optimalWidth'];
703: $optimalHeight = $dimensionsArray['optimalHeight'];
704: } else if ($newHeight > $newWidth) {
705:
706:
707: $dimensionsArray = $this->getSizeByFixedHeight($newWidth, $newHeight);
708: $optimalWidth = $dimensionsArray['optimalWidth'];
709: $optimalHeight = $dimensionsArray['optimalHeight'];
710: } else {
711:
712: $optimalWidth = $newWidth;
713: $optimalHeight= $newHeight;
714: }
715: }
716:
717: return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
718: }
719:
720:
721:
722: private function getOptimalCrop($newWidth, $newHeight)
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: if (!$this->forceStretch) {
756:
757:
758: if ($this->width < $newWidth && $this->height < $newHeight) {
759: return array('optimalWidth' => $this->width, 'optimalHeight' => $this->height);
760: }
761: }
762:
763: $heightRatio = $this->height / $newHeight;
764: $widthRatio = $this->width / $newWidth;
765:
766: if ($heightRatio < $widthRatio) {
767: $optimalRatio = $heightRatio;
768: } else {
769: $optimalRatio = $widthRatio;
770: }
771:
772: $optimalHeight = round( $this->height / $optimalRatio );
773: $optimalWidth = round( $this->width / $optimalRatio );
774:
775: return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
776: }
777:
778:
779:
780: private function sharpen()
781:
782:
783:
784:
785:
786:
787:
788:
789: {
790:
791: if (version_compare(PHP_VERSION, '5.1.0') >= 0) {
792:
793:
794: if ($this->aggresiveSharpening) {
795:
796: $sharpenMatrix = array( array( -1, -1, -1 ),
797: array( -1, 16, -1 ),
798: array( -1, -1, -1 ) );
799: $divisor = 8;
800: $offset = 0;
801:
802: imageconvolution($this->imageResized, $sharpenMatrix, $divisor, $offset);
803: }
804: else
805: {
806: $sharpness = $this->findSharp($this->widthOriginal, $this->width);
807:
808: $sharpenMatrix = array(
809: array(-1, -2, -1),
810: array(-2, $sharpness + 12, -2),
811: array(-1, -2, -1)
812: );
813: $divisor = $sharpness;
814: $offset = 0;
815: imageconvolution($this->imageResized, $sharpenMatrix, $divisor, $offset);
816: }
817: }
818: else
819: {
820: if ($this->debug) { throw new Exception('Sharpening required PHP 5.1.0 or greater.'); }
821: }
822: }
823:
824:
825:
826: private function sharpen2($level)
827: {
828: $sharpenMatrix = array(
829: array($level, $level, $level),
830: array($level, (8*$level)+1, $level),
831: array($level, $level, $level)
832: );
833:
834: }
835:
836:
837:
838: private function findSharp($orig, $final)
839:
840:
841:
842:
843:
844:
845:
846: {
847: $final = $final * (750.0 / $orig);
848: $a = 52;
849: $b = -0.27810650887573124;
850: $c = .00047337278106508946;
851:
852: $result = $a + $b * $final + $c * $final * $final;
853:
854: return max(round($result), 0);
855: }
856:
857:
858:
859: private function prepOption($option)
860:
861:
862:
863:
864:
865:
866:
867: {
868: if (is_array($option)) {
869: if (fix_strtolower($option[0]) === 'crop' && count($option) == 2) {
870: return 'crop';
871: } else {
872: throw new Exception('Crop resize option array is badly formatted.');
873: }
874: } else if (strpos($option, 'crop') !== false) {
875: return 'crop';
876: }
877:
878: if (is_string($option)) {
879: return fix_strtolower($option);
880: }
881:
882: return $option;
883: }
884:
885:
886: 887: 888:
889:
890:
891:
892:
893:
894:
895:
896:
897: public function borderPreset($preset)
898: {
899: switch ($preset)
900: {
901:
902: case 'simple':
903: $this->addBorder(7, '#fff');
904: $this->addBorder(6, '#f2f1f0');
905: $this->addBorder(2, '#fff');
906: $this->addBorder(1, '#ccc');
907: break;
908: default:
909: break;
910: }
911:
912: }
913:
914:
915: 916: 917:
918:
919: public function addBorder($thickness = 1, $rgbArray = array(255, 255, 255))
920:
921:
922:
923:
924:
925:
926:
927:
928: {
929: if ($this->imageResized) {
930:
931: $rgbArray = $this->formatColor($rgbArray);
932: $r = $rgbArray['r'];
933: $g = $rgbArray['g'];
934: $b = $rgbArray['b'];
935:
936:
937: $x1 = 0;
938: $y1 = 0;
939: $x2 = imagesx($this->imageResized) - 1;
940: $y2 = imagesy($this->imageResized) - 1;
941:
942: $rgbArray = imagecolorallocate($this->imageResized, $r, $g, $b);
943:
944:
945: for($i = 0; $i < $thickness; $i++) {
946: imagerectangle($this->imageResized, $x1++, $y1++, $x2--, $y2--, $rgbArray);
947: }
948: }
949: }
950:
951:
952: 953: 954:
955:
956: public function greyScale()
957:
958:
959:
960:
961:
962:
963:
964:
965: {
966: if ($this->imageResized) {
967: imagefilter($this->imageResized, IMG_FILTER_GRAYSCALE);
968: }
969:
970: }
971:
972:
973:
974: public function greyScaleEnhanced()
975:
976:
977:
978:
979:
980:
981:
982:
983: {
984: if ($this->imageResized) {
985: imagefilter($this->imageResized, IMG_FILTER_GRAYSCALE);
986: imagefilter($this->imageResized, IMG_FILTER_CONTRAST, -15);
987: imagefilter($this->imageResized, IMG_FILTER_BRIGHTNESS, 2);
988: $this->sharpen($this->width);
989: }
990: }
991:
992:
993:
994: public function greyScaleDramatic()
995:
996: {
997: $this->gd_filter_monopin();
998: }
999:
1000:
1001: 1002: 1003:
1004:
1005: public function blackAndWhite()
1006:
1007:
1008:
1009:
1010:
1011:
1012:
1013:
1014: {
1015: if ($this->imageResized) {
1016:
1017: imagefilter($this->imageResized, IMG_FILTER_GRAYSCALE);
1018: imagefilter($this->imageResized, IMG_FILTER_CONTRAST, -1000);
1019: }
1020:
1021: }
1022:
1023:
1024: 1025: 1026:
1027:
1028: public function negative()
1029:
1030:
1031:
1032:
1033:
1034:
1035:
1036:
1037: {
1038: if ($this->imageResized) {
1039:
1040: imagefilter($this->imageResized, IMG_FILTER_NEGATE);
1041: }
1042:
1043: }
1044:
1045:
1046: 1047: 1048:
1049:
1050: public function sepia()
1051:
1052:
1053:
1054:
1055:
1056:
1057:
1058:
1059: {
1060: if ($this->imageResized) {
1061: imagefilter($this->imageResized, IMG_FILTER_GRAYSCALE);
1062: imagefilter($this->imageResized, IMG_FILTER_BRIGHTNESS, -10);
1063: imagefilter($this->imageResized, IMG_FILTER_CONTRAST, -20);
1064: imagefilter($this->imageResized, IMG_FILTER_COLORIZE, 60, 30, -15);
1065: }
1066: }
1067:
1068:
1069:
1070: public function sepia2()
1071:
1072: {
1073: if ($this->imageResized) {
1074:
1075: $total = imagecolorstotal( $this->imageResized );
1076: for ( $i = 0; $i < $total; $i++ ) {
1077: $index = imagecolorsforindex( $this->imageResized, $i );
1078: $red = ( $index["red"] * 0.393 + $index["green"] * 0.769 + $index["blue"] * 0.189 ) / 1.351;
1079: $green = ( $index["red"] * 0.349 + $index["green"] * 0.686 + $index["blue"] * 0.168 ) / 1.203;
1080: $blue = ( $index["red"] * 0.272 + $index["green"] * 0.534 + $index["blue"] * 0.131 ) / 2.140;
1081: imagecolorset( $this->imageResized, $i, $red, $green, $blue );
1082: }
1083:
1084:
1085: }
1086: }
1087:
1088:
1089: 1090: 1091:
1092:
1093: public function vintage()
1094:
1095: {
1096: $this->gd_filter_vintage();
1097: }
1098:
1099: 1100: 1101:
1102:
1103:
1104:
1105: public function gd_filter_monopin()
1106: {
1107:
1108: if ($this->imageResized) {
1109: imagefilter($this->imageResized, IMG_FILTER_GRAYSCALE);
1110: imagefilter($this->imageResized, IMG_FILTER_BRIGHTNESS, -15);
1111: imagefilter($this->imageResized, IMG_FILTER_CONTRAST, -15);
1112: $this->imageResized = $this->gd_apply_overlay($this->imageResized, 'vignette', 100);
1113: }
1114: }
1115:
1116:
1117:
1118: public function gd_filter_vintage()
1119: {
1120: if ($this->imageResized) {
1121: $this->imageResized = $this->gd_apply_overlay($this->imageResized, 'vignette', 45);
1122: imagefilter($this->imageResized, IMG_FILTER_BRIGHTNESS, 20);
1123: imagefilter($this->imageResized, IMG_FILTER_CONTRAST, -35);
1124: imagefilter($this->imageResized, IMG_FILTER_COLORIZE, 60, -10, 35);
1125: imagefilter($this->imageResized, IMG_FILTER_SMOOTH, 7);
1126: $this->imageResized = $this->gd_apply_overlay($this->imageResized, 'scratch', 10);
1127: }
1128: }
1129:
1130:
1131:
1132:
1133: private function gd_apply_overlay($im, $type, $amount)
1134:
1135:
1136:
1137:
1138:
1139:
1140:
1141:
1142: {
1143: $width = imagesx($im);
1144: $height = imagesy($im);
1145: $filter = imagecreatetruecolor($width, $height);
1146:
1147: imagealphablending($filter, false);
1148: imagesavealpha($filter, true);
1149:
1150: $transparent = imagecolorallocatealpha($filter, 255, 255, 255, 127);
1151: imagefilledrectangle($filter, 0, 0, $width, $height, $transparent);
1152:
1153:
1154: $overlay = $this->filterOverlayPath . '/' . $type . '.png';
1155: $png = imagecreatefrompng($overlay);
1156: imagecopyresampled($filter, $png, 0, 0, 0, 0, $width, $height, imagesx($png), imagesy($png));
1157:
1158: $comp = imagecreatetruecolor($width, $height);
1159: imagecopy($comp, $im, 0, 0, 0, 0, $width, $height);
1160: imagecopy($comp, $filter, 0, 0, 0, 0, $width, $height);
1161: imagecopymerge($im, $comp, 0, 0, 0, 0, $width, $height, $amount);
1162:
1163: imagedestroy($comp);
1164: return $im;
1165: }
1166:
1167:
1168: 1169: 1170:
1171:
1172: public function image_colorize($rgb) {
1173: imagetruecolortopalette($this->imageResized,true,256);
1174: $numColors = imagecolorstotal($this->imageResized);
1175:
1176: for ($x = 0; $x < $numColors; $x++) {
1177: list($r,$g,$b) = array_values(imagecolorsforindex($this->imageResized,$x));
1178:
1179:
1180: $grayscale = ($r + $g + $b) / 3 / 0xff;
1181:
1182: imagecolorset($this->imageResized,$x,
1183: $grayscale * $rgb[0],
1184: $grayscale * $rgb[1],
1185: $grayscale * $rgb[2]
1186: );
1187:
1188: }
1189:
1190: return true;
1191: }
1192:
1193:
1194: 1195: 1196:
1197:
1198: public function addReflection($reflectionHeight = 50, $startingTransparency = 30, $inside = false, $bgColor = '#fff', $stretch=false, $divider = 0)
1199: {
1200:
1201:
1202: $rgbArray = $this->formatColor($bgColor);
1203: $r = $rgbArray['r'];
1204: $g = $rgbArray['g'];
1205: $b = $rgbArray['b'];
1206:
1207: $im = $this->imageResized;
1208: $li = imagecreatetruecolor($this->width, 1);
1209:
1210: $bgc = imagecolorallocate($li, $r, $g, $b);
1211: imagefilledrectangle($li, 0, 0, $this->width, 1, $bgc);
1212:
1213: $bg = imagecreatetruecolor($this->width, $reflectionHeight);
1214: $wh = imagecolorallocate($im, 255, 255, 255);
1215:
1216: $im = imagerotate($im, -180, $wh);
1217: imagecopyresampled($bg, $im, 0, 0, 0, 0, $this->width, $this->height, $this->width, $this->height);
1218:
1219: $im = $bg;
1220:
1221: $bg = imagecreatetruecolor($this->width, $reflectionHeight);
1222:
1223: for ($x = 0; $x < $this->width; $x++) {
1224: imagecopy($bg, $im, $x, 0, $this->width-$x -1, 0, 1, $reflectionHeight);
1225: }
1226: $im = $bg;
1227:
1228: $transaprencyAmount = $this->invertTransparency($startingTransparency, 100);
1229:
1230:
1231:
1232: if ($stretch) {
1233: $step = 100/($reflectionHeight + $startingTransparency);
1234: } else{
1235: $step = 100/$reflectionHeight;
1236: }
1237: for($i=0; $i<=$reflectionHeight; $i++){
1238:
1239: if($startingTransparency>100) $startingTransparency = 100;
1240: if($startingTransparency< 1) $startingTransparency = 1;
1241: imagecopymerge($bg, $li, 0, $i, 0, 0, $this->width, 1, $startingTransparency);
1242: $startingTransparency+=$step;
1243: }
1244:
1245:
1246: imagecopymerge($im, $li, 0, 0, 0, 0, $this->width, $divider, 100);
1247:
1248:
1249:
1250: $x = imagesx($im);
1251: $y = imagesy($im);
1252:
1253:
1254:
1255: if ($inside) {
1256:
1257:
1258: $final = imagecreatetruecolor($this->width, $this->height);
1259:
1260: imagecopymerge ($final, $this->imageResized, 0, 0, 0, $reflectionHeight, $this->width, $this->height - $reflectionHeight, 100);
1261: imagecopymerge ($final, $im, 0, $this->height - $reflectionHeight, 0, 0, $x, $y, 100);
1262:
1263: } else {
1264:
1265:
1266: $final = imagecreatetruecolor($this->width, $this->height + $y);
1267:
1268: imagecopymerge ($final, $this->imageResized, 0, 0, 0, 0, $this->width, $this->height, 100);
1269: imagecopymerge ($final, $im, 0, $this->height, 0, 0, $x, $y, 100);
1270: }
1271:
1272: $this->imageResized = $final;
1273:
1274: imagedestroy($li);
1275: imagedestroy($im);
1276: }
1277:
1278:
1279: 1280: 1281:
1282:
1283: public function rotate($value = 90, $bgColor = 'transparent')
1284:
1285:
1286:
1287:
1288:
1289:
1290:
1291:
1292:
1293:
1294:
1295: {
1296: if ($this->imageResized) {
1297:
1298: if (is_integer($value)) {
1299: $degrees = $value;
1300: }
1301:
1302:
1303: $rgbArray = $this->formatColor($bgColor);
1304: $r = $rgbArray['r'];
1305: $g = $rgbArray['g'];
1306: $b = $rgbArray['b'];
1307: if (isset($rgbArray['a'])) {$a = $rgbArray['a']; }
1308:
1309: if (is_string($value)) {
1310:
1311: $value = fix_strtolower($value);
1312:
1313: switch ($value) {
1314: case 'left':
1315: $degrees = 90;
1316: break;
1317: case 'right':
1318: $degrees = 270;
1319: break;
1320: case 'upside':
1321: $degrees = 180;
1322: break;
1323: default:
1324: break;
1325: }
1326:
1327: }
1328:
1329:
1330:
1331: $degrees = 360 - $degrees;
1332:
1333:
1334: $bg = imagecolorallocatealpha($this->imageResized, $r, $g, $b, $a);
1335:
1336:
1337: imagefill($this->imageResized, 0, 0 , $bg);
1338:
1339:
1340: $this->imageResized = imagerotate($this->imageResized, $degrees, $bg);
1341:
1342:
1343: imagesavealpha($this->imageResized,true);
1344:
1345: }
1346: }
1347:
1348:
1349: 1350: 1351:
1352:
1353: public function roundCorners($radius = 5, $bgColor = 'transparent')
1354:
1355:
1356:
1357:
1358:
1359:
1360:
1361:
1362:
1363: {
1364:
1365:
1366: $isTransparent = false;
1367: if (!is_array($bgColor)) {
1368: if (fix_strtolower($bgColor) === 'transparent') {
1369: $isTransparent = true;
1370: }
1371: }
1372:
1373:
1374:
1375: if ($isTransparent) {
1376: $bgColor = $this->findUnusedGreen();
1377: }
1378:
1379:
1380: $rgbArray = $this->formatColor($bgColor);
1381: $r = $rgbArray['r'];
1382: $g = $rgbArray['g'];
1383: $b = $rgbArray['b'];
1384: if (isset($rgbArray['a'])) {$a = $rgbArray['a']; }
1385:
1386:
1387:
1388:
1389: $cornerImg = imagecreatetruecolor($radius, $radius);
1390:
1391:
1392:
1393:
1394:
1395:
1396:
1397:
1398:
1399: $maskColor = imagecolorallocate($cornerImg, 0, 0, 0);
1400:
1401:
1402:
1403:
1404: imagecolortransparent($cornerImg, $maskColor);
1405:
1406:
1407:
1408:
1409: $imagebgColor = imagecolorallocate($cornerImg, $r, $g, $b);
1410:
1411:
1412:
1413:
1414: imagefill($cornerImg, 0, 0, $imagebgColor);
1415:
1416:
1417: imagefilledellipse($cornerImg, $radius, $radius, $radius * 2, $radius * 2, $maskColor );
1418:
1419:
1420:
1421: imagecopymerge($this->imageResized, $cornerImg, 0, 0, 0, 0, $radius, $radius, 100);
1422:
1423:
1424: $cornerImg = imagerotate($cornerImg, 90, 0);
1425: imagecopymerge($this->imageResized, $cornerImg, 0, $this->height - $radius, 0, 0, $radius, $radius, 100);
1426:
1427: $cornerImg = imagerotate($cornerImg, 90, 0);
1428: imagecopymerge($this->imageResized, $cornerImg, $this->width - $radius, $this->height - $radius, 0, 0, $radius, $radius, 100);
1429:
1430: $cornerImg = imagerotate($cornerImg, 90, 0);
1431: imagecopymerge($this->imageResized, $cornerImg, $this->width - $radius, 0, 0, 0, $radius, $radius, 100);
1432:
1433:
1434:
1435: if ($isTransparent) {
1436:
1437: $this->imageResized = $this->transparentImage($this->imageResized);
1438: imagesavealpha($this->imageResized, true);
1439: }
1440:
1441: }
1442:
1443:
1444: 1445: 1446:
1447:
1448: public function addShadow($shadowAngle=45, $blur=15, $bgColor='transparent')
1449:
1450:
1451:
1452:
1453:
1454:
1455:
1456:
1457:
1458:
1459: {
1460:
1461: define('STEPS', $blur*2);
1462:
1463:
1464: $shadowDistance = $blur*0.25;
1465:
1466:
1467: $blurWidth = $blurHeight = $blur;
1468:
1469:
1470: if ($shadowAngle == 0) {
1471: $distWidth = 0;
1472: $distHeight = 0;
1473: } else {
1474: $distWidth = $shadowDistance * cos(deg2rad($shadowAngle));
1475: $distHeight = $shadowDistance * sin(deg2rad($shadowAngle));
1476: }
1477:
1478:
1479:
1480: if (fix_strtolower($bgColor) !== 'transparent') {
1481: $rgbArray = $this->formatColor($bgColor);
1482: $r0 = $rgbArray['r'];
1483: $g0 = $rgbArray['g'];
1484: $b0 = $rgbArray['b'];
1485: }
1486:
1487:
1488: $image = $this->imageResized;
1489: $width = $this->width;
1490: $height = $this->height;
1491:
1492:
1493: $newImage = imagecreatetruecolor($width, $height);
1494: imagecopyresampled($newImage, $image, 0, 0, 0, 0, $width, $height, $width, $height);
1495:
1496:
1497:
1498: $rgb = imagecreatetruecolor($width+$blurWidth,$height+$blurHeight);
1499: $colour = imagecolorallocate($rgb, 0, 0, 0);
1500: imagefilledrectangle($rgb, 0, 0, $width+$blurWidth, $height+$blurHeight, $colour);
1501: $colour = imagecolorallocate($rgb, 255, 255, 255);
1502:
1503: imagefilledrectangle($rgb, $blurWidth*0.5-$distWidth, $blurHeight*0.5-$distHeight, $width+$blurWidth*0.5-$distWidth, $height+$blurWidth*0.5-$distHeight, $colour);
1504:
1505: imagecopymerge($rgb, $newImage, $blurWidth*0.5-$distWidth, $blurHeight*0.5-$distHeight, 0,0, $width+$blurWidth, $height+$blurHeight, 100);
1506:
1507:
1508:
1509: $shadow = imagecreatetruecolor($width+$blurWidth,$height+$blurHeight);
1510: imagealphablending($shadow, false);
1511: $colour = imagecolorallocate($shadow, 0, 0, 0);
1512: imagefilledrectangle($shadow, 0, 0, $width+$blurWidth, $height+$blurHeight, $colour);
1513:
1514:
1515: for($i=0;$i<=STEPS;$i++) {
1516:
1517: $t = ((1.0*$i)/STEPS);
1518: $intensity = 255*$t*$t;
1519:
1520: $colour = imagecolorallocate($shadow, $intensity, $intensity, $intensity);
1521: $points = array(
1522: $blurWidth*$t, $blurHeight,
1523: $blurWidth, $blurHeight*$t,
1524: $width, $blurHeight*$t,
1525: $width+$blurWidth*(1-$t), $blurHeight,
1526: $width+$blurWidth*(1-$t), $height,
1527: $width, $height+$blurHeight*(1-$t),
1528: $blurWidth, $height+$blurHeight*(1-$t),
1529: $blurWidth*$t, $height
1530: );
1531: imagepolygon($shadow, $points, 8, $colour);
1532: }
1533:
1534: for($i=0;$i<=STEPS;$i++) {
1535:
1536: $t = ((1.0*$i)/STEPS);
1537: $intensity = 255*$t*$t;
1538:
1539: $colour = imagecolorallocate($shadow, $intensity, $intensity, $intensity);
1540: imagefilledarc($shadow, $blurWidth-1, $blurHeight-1, 2*(1-$t)*$blurWidth, 2*(1-$t)*$blurHeight, 180, 268, $colour, IMG_ARC_PIE);
1541: imagefilledarc($shadow, $width, $blurHeight-1, 2*(1-$t)*$blurWidth, 2*(1-$t)*$blurHeight, 270, 358, $colour, IMG_ARC_PIE);
1542: imagefilledarc($shadow, $width, $height, 2*(1-$t)*$blurWidth, 2*(1-$t)*$blurHeight, 0, 90, $colour, IMG_ARC_PIE);
1543: imagefilledarc($shadow, $blurWidth-1, $height, 2*(1-$t)*$blurWidth, 2*(1-$t)*$blurHeight, 90, 180, $colour, IMG_ARC_PIE);
1544: }
1545:
1546:
1547: $colour = imagecolorallocate($shadow, 255, 255, 255);
1548: imagefilledrectangle($shadow, $blurWidth, $blurHeight, $width, $height, $colour);
1549: imagefilledrectangle($shadow, $blurWidth*0.5-$distWidth, $blurHeight*0.5-$distHeight, $width+$blurWidth*0.5-1-$distWidth, $height+$blurHeight*0.5-1-$distHeight, $colour);
1550:
1551:
1552:
1553: imagealphablending($rgb, false);
1554:
1555: for ($theX=0;$theX<imagesx($rgb);$theX++){
1556: for ($theY=0;$theY<imagesy($rgb);$theY++){
1557:
1558:
1559: $colArray = imagecolorat($rgb,$theX,$theY);
1560: $r = ($colArray >> 16) & 0xFF;
1561: $g = ($colArray >> 8) & 0xFF;
1562: $b = $colArray & 0xFF;
1563:
1564:
1565: $colArray = imagecolorat($shadow,$theX,$theY);
1566: $a = $colArray & 0xFF;
1567: $a = 127-floor($a/2);
1568: $t = $a/128.0;
1569:
1570:
1571: if(fix_strtolower($bgColor) === 'transparent') {
1572: $myColour = imagecolorallocatealpha($rgb,$r,$g,$b,$a);
1573: } else {
1574: $myColour = imagecolorallocate($rgb,$r*(1.0-$t)+$r0*$t,$g*(1.0-$t)+$g0*$t,$b*(1.0-$t)+$b0*$t);
1575: }
1576:
1577:
1578: imagesetpixel($rgb, $theX, $theY, $myColour);
1579: }
1580: }
1581:
1582: imagealphablending($rgb, true);
1583: imagesavealpha($rgb, true);
1584:
1585: $this->imageResized = $rgb;
1586:
1587: imagedestroy($image);
1588: imagedestroy($newImage);
1589: imagedestroy($shadow);
1590: }
1591:
1592:
1593: 1594: 1595:
1596:
1597: public function addCaptionBox($side='b', $thickness=50, $padding=0, $bgColor='#000', $transaprencyAmount=30)
1598:
1599:
1600:
1601:
1602:
1603:
1604:
1605:
1606:
1607:
1608:
1609:
1610: {
1611: $side = fix_strtolower($side);
1612:
1613:
1614: $rgbArray = $this->formatColor($bgColor);
1615: $r = $rgbArray['r'];
1616: $g = $rgbArray['g'];
1617: $b = $rgbArray['b'];
1618:
1619: $positionArray = $this->calculateCaptionBoxPosition($side, $thickness, $padding);
1620:
1621:
1622: $this->captionBoxPositionArray = $positionArray;
1623:
1624:
1625: $transaprencyAmount = $this->invertTransparency($transaprencyAmount, 127, false);
1626: $transparent = imagecolorallocatealpha($this->imageResized, $r, $g, $b, $transaprencyAmount);
1627: imagefilledrectangle($this->imageResized, $positionArray['x1'], $positionArray['y1'], $positionArray['x2'], $positionArray['y2'], $transparent);
1628: }
1629:
1630:
1631:
1632: public function addTextToCaptionBox($text, $fontColor='#fff', $fontSize = 12, $angle = 0, $font = null)
1633:
1634:
1635:
1636:
1637:
1638:
1639:
1640:
1641:
1642: {
1643:
1644:
1645: if (count($this->captionBoxPositionArray) == 4) {
1646: $x1 = $this->captionBoxPositionArray['x1'];
1647: $x2 = $this->captionBoxPositionArray['x2'];
1648: $y1 = $this->captionBoxPositionArray['y1'];
1649: $y2 = $this->captionBoxPositionArray['y2'];
1650: } else {
1651: if ($this->debug) { throw new Exception('No caption box found.'); }else{ return false; }
1652: }
1653:
1654:
1655:
1656: $font = $this->getTextFont($font);
1657:
1658:
1659: $textSizeArray = $this->getTextSize($fontSize, $angle, $font, $text);
1660: $textWidth = $textSizeArray['width'];
1661: $textHeight = $textSizeArray['height'];
1662:
1663:
1664: $boxXMiddle = (($x2 - $x1) / 2);
1665: $boxYMiddle = (($y2 - $y1) / 2);
1666:
1667:
1668: $xPos = ($x1 + $boxXMiddle) - ($textWidth/2);
1669: $yPos = ($y1 + $boxYMiddle) - ($textHeight/2);
1670:
1671: $pos = $xPos . 'x' . $yPos;
1672:
1673: $this->addText($text, $pos, $padding = 0, $fontColor, $fontSize, $angle, $font);
1674:
1675: }
1676:
1677:
1678:
1679: private function calculateCaptionBoxPosition($side, $thickness, $padding)
1680: {
1681: $positionArray = array();
1682:
1683: switch ($side) {
1684: case 't':
1685: $positionArray['x1'] = 0;
1686: $positionArray['y1'] = $padding;
1687: $positionArray['x2'] = $this->width;
1688: $positionArray['y2'] = $thickness + $padding;
1689: break;
1690: case 'r':
1691: $positionArray['x1'] = $this->width - $thickness - $padding;
1692: $positionArray['y1'] = 0;
1693: $positionArray['x2'] = $this->width - $padding;
1694: $positionArray['y2'] = $this->height;
1695: break;
1696: case 'b':
1697: $positionArray['x1'] = 0;
1698: $positionArray['y1'] = $this->height - $thickness - $padding;
1699: $positionArray['x2'] = $this->width;
1700: $positionArray['y2'] = $this->height - $padding;
1701: break;
1702: case 'l':
1703: $positionArray['x1'] = $padding;
1704: $positionArray['y1'] = 0;
1705: $positionArray['x2'] = $thickness + $padding;
1706: $positionArray['y2'] = $this->height;
1707: break;
1708:
1709: default:
1710: break;
1711: }
1712:
1713: return $positionArray;
1714:
1715: }
1716:
1717: 1718: 1719:
1720:
1721: public function getExif($debug=false)
1722:
1723:
1724:
1725:
1726:
1727:
1728:
1729:
1730:
1731: {
1732:
1733: if (!$this->debug || !$debug) { $debug = false; }
1734:
1735:
1736: if (!$this->testEXIFInstalled()) { if ($debug) { throw new Exception('The EXIF Library is not installed.'); }else{ return array(); }};
1737: if (!file_exists($this->fileName)) { if ($debug) { throw new Exception('Image not found.'); }else{ return array(); }};
1738: if ($this->fileExtension !== '.jpg') { if ($debug) { throw new Exception('Metadata not supported for this image type.'); }else{ return array(); }};
1739: $exifData = exif_read_data($this->fileName, 'IFD0');
1740:
1741:
1742: $ev = $exifData['ApertureValue'];
1743: $apPeicesArray = explode('/', $ev);
1744: if (count($apPeicesArray) == 2) {
1745: $apertureValue = round($apPeicesArray[0] / $apPeicesArray[1], 2, PHP_ROUND_HALF_DOWN) . ' EV';
1746: } else { $apertureValue = '';}
1747:
1748:
1749: $focalLength = $exifData['FocalLength'];
1750: $flPeicesArray = explode('/', $focalLength);
1751: if (count($flPeicesArray) == 2) {
1752: $focalLength = $flPeicesArray[0] / $flPeicesArray[1] . '.0 mm';
1753: } else { $focalLength = '';}
1754:
1755:
1756: $fNumber = $exifData['FNumber'];
1757: $fnPeicesArray = explode('/', $fNumber);
1758: if (count($fnPeicesArray) == 2) {
1759: $fNumber = $fnPeicesArray[0] / $fnPeicesArray[1];
1760: } else { $fNumber = '';}
1761:
1762:
1763: if (isset($exifData['ExposureProgram'])) { $ep = $exifData['ExposureProgram']; }
1764: if (isset($ep)) { $ep = $this->resolveExposureProgram($ep); }
1765:
1766:
1767:
1768: $mm = $exifData['MeteringMode'];
1769: $mm = $this->resolveMeteringMode($mm);
1770:
1771:
1772: $flash = $exifData['Flash'];
1773: $flash = $this->resolveFlash($flash);
1774:
1775:
1776: if (isset($exifData['Make'])) {
1777: $exifDataArray['make'] = $exifData['Make'];
1778: } else { $exifDataArray['make'] = ''; }
1779:
1780: if (isset($exifData['Model'])) {
1781: $exifDataArray['model'] = $exifData['Model'];
1782: } else { $exifDataArray['model'] = ''; }
1783:
1784: if (isset($exifData['DateTime'])) {
1785: $exifDataArray['date'] = $exifData['DateTime'];
1786: } else { $exifDataArray['date'] = ''; }
1787:
1788: if (isset($exifData['ExposureTime'])) {
1789: $exifDataArray['exposure time'] = $exifData['ExposureTime'] . ' sec.';
1790: } else { $exifDataArray['exposure time'] = ''; }
1791:
1792: if ($apertureValue != '') {
1793: $exifDataArray['aperture value'] = $apertureValue;
1794: } else { $exifDataArray['aperture value'] = ''; }
1795:
1796: if (isset($exifData['COMPUTED']['ApertureFNumber'])) {
1797: $exifDataArray['f-stop'] = $exifData['COMPUTED']['ApertureFNumber'];
1798: } else { $exifDataArray['f-stop'] = ''; }
1799:
1800: if (isset($exifData['FNumber'])) {
1801: $exifDataArray['fnumber'] = $exifData['FNumber'];
1802: } else { $exifDataArray['fnumber'] = ''; }
1803:
1804: if ($fNumber != '') {
1805: $exifDataArray['fnumber value'] = $fNumber;
1806: } else { $exifDataArray['fnumber value'] = ''; }
1807:
1808: if (isset($exifData['ISOSpeedRatings'])) {
1809: $exifDataArray['iso'] = $exifData['ISOSpeedRatings'];
1810: } else { $exifDataArray['iso'] = ''; }
1811:
1812: if ($focalLength != '') {
1813: $exifDataArray['focal length'] = $focalLength;
1814: } else { $exifDataArray['focal length'] = ''; }
1815:
1816: if (isset($ep)) {
1817: $exifDataArray['exposure program'] = $ep;
1818: } else { $exifDataArray['exposure program'] = ''; }
1819:
1820: if ($mm != '') {
1821: $exifDataArray['metering mode'] = $mm;
1822: } else { $exifDataArray['metering mode'] = ''; }
1823:
1824: if ($flash != '') {
1825: $exifDataArray['flash status'] = $flash;
1826: } else { $exifDataArray['flash status'] = ''; }
1827:
1828: if (isset($exifData['Artist'])) {
1829: $exifDataArray['creator'] = $exifData['Artist'] ;
1830: } else { $exifDataArray['creator'] = ''; }
1831:
1832: if (isset($exifData['Copyright'])) {
1833: $exifDataArray['copyright'] = $exifData['Copyright'];
1834: } else { $exifDataArray['copyright'] = ''; }
1835:
1836:
1837: if (isset($exifData['Orientation'])) {
1838: $exifDataArray['orientation'] = $exifData['Orientation'];
1839: } else { $exifDataArray['orientation'] = ''; }
1840:
1841: return $exifDataArray;
1842: }
1843:
1844:
1845:
1846: private function resolveExposureProgram($ep)
1847: {
1848: switch ($ep) {
1849: case 0:
1850: $ep = '';
1851: break;
1852: case 1:
1853: $ep = 'manual';
1854: break;
1855: case 2:
1856: $ep = 'normal program';
1857: break;
1858: case 3:
1859: $ep = 'aperture priority';
1860: break;
1861: case 4:
1862: $ep = 'shutter priority';
1863: break;
1864: case 5:
1865: $ep = 'creative program';
1866: break;
1867: case 6:
1868: $ep = 'action program';
1869: break;
1870: case 7:
1871: $ep = 'portrait mode';
1872: break;
1873: case 8:
1874: $ep = 'landscape mode';
1875: break;
1876:
1877: default:
1878: break;
1879: }
1880:
1881: return $ep;
1882: }
1883:
1884:
1885:
1886: private function resolveMeteringMode($mm)
1887: {
1888: switch ($mm) {
1889: case 0:
1890: $mm = 'unknown';
1891: break;
1892: case 1:
1893: $mm = 'average';
1894: break;
1895: case 2:
1896: $mm = 'center weighted average';
1897: break;
1898: case 3:
1899: $mm = 'spot';
1900: break;
1901: case 4:
1902: $mm = 'multi spot';
1903: break;
1904: case 5:
1905: $mm = 'pattern';
1906: break;
1907: case 6:
1908: $mm = 'partial';
1909: break;
1910: case 255:
1911: $mm = 'other';
1912: break;
1913:
1914: default:
1915: break;
1916: }
1917:
1918: return $mm;
1919: }
1920:
1921:
1922:
1923: private function resolveFlash($flash)
1924: {
1925: switch ($flash) {
1926: case 0:
1927: $flash = 'flash did not fire';
1928: break;
1929: case 1:
1930: $flash = 'flash fired';
1931: break;
1932: case 5:
1933: $flash = 'strobe return light not detected';
1934: break;
1935: case 7:
1936: $flash = 'strobe return light detected';
1937: break;
1938: case 9:
1939: $flash = 'flash fired, compulsory flash mode';
1940: break;
1941: case 13:
1942: $flash = 'flash fired, compulsory flash mode, return light not detected';
1943: break;
1944: case 15:
1945: $flash = 'flash fired, compulsory flash mode, return light detected';
1946: break;
1947: case 16:
1948: $flash = 'flash did not fire, compulsory flash mode';
1949: break;
1950: case 24:
1951: $flash = 'flash did not fire, auto mode';
1952: break;
1953: case 25:
1954: $flash = 'flash fired, auto mode';
1955: break;
1956: case 29:
1957: $flash = 'flash fired, auto mode, return light not detected';
1958: break;
1959: case 31:
1960: $flash = 'flash fired, auto mode, return light detected';
1961: break;
1962: case 32:
1963: $flash = 'no flash function';
1964: break;
1965: case 65:
1966: $flash = 'flash fired, red-eye reduction mode';
1967: break;
1968: case 69:
1969: $flash = 'flash fired, red-eye reduction mode, return light not detected';
1970: break;
1971: case 71:
1972: $flash = 'flash fired, red-eye reduction mode, return light detected';
1973: break;
1974: case 73:
1975: $flash = 'flash fired, compulsory flash mode, red-eye reduction mode';
1976: break;
1977: case 77:
1978: $flash = 'flash fired, compulsory flash mode, red-eye reduction mode, return light not detected';
1979: break;
1980: case 79:
1981: $flash = 'flash fired, compulsory flash mode, red-eye reduction mode, return light detected';
1982: break;
1983: case 89:
1984: $flash = 'flash fired, auto mode, red-eye reduction mode';
1985: break;
1986: case 93:
1987: $flash = 'flash fired, auto mode, return light not detected, red-eye reduction mode';
1988: break;
1989: case 95:
1990: $flash = 'flash fired, auto mode, return light detected, red-eye reduction mode';
1991: break;
1992:
1993: default:
1994: break;
1995: }
1996:
1997: return $flash;
1998:
1999: }
2000:
2001:
2002: 2003: 2004:
2005:
2006:
2007: 2008: 2009:
2010:
2011: public function writeIPTCcaption($value)
2012:
2013: {
2014: $this->writeIPTC(120, $value);
2015: }
2016:
2017:
2018:
2019: public function writeIPTCwriter($value)
2020: {
2021:
2022: }
2023:
2024:
2025:
2026: private function writeIPTC($dat, $value)
2027: {
2028:
2029:
2030:
2031: $caption_block = $this->iptc_maketag(2, $dat, $value);
2032: $image_string = iptcembed($caption_block, $this->fileName);
2033: file_put_contents('iptc.jpg', $image_string);
2034: }
2035:
2036:
2037:
2038: private function iptc_maketag($rec,$dat,$val)
2039:
2040:
2041:
2042:
2043:
2044:
2045:
2046:
2047:
2048:
2049:
2050: {
2051: $len = strlen($val);
2052: if ($len < 0x8000)
2053: return chr(0x1c).chr($rec).chr($dat).
2054: chr($len >> 8).
2055: chr($len & 0xff).
2056: $val;
2057: else
2058: return chr(0x1c).chr($rec).chr($dat).
2059: chr(0x80).chr(0x04).
2060: chr(($len >> 24) & 0xff).
2061: chr(($len >> 16) & 0xff).
2062: chr(($len >> 8 ) & 0xff).
2063: chr(($len ) & 0xff).
2064: $val;
2065: }
2066:
2067:
2068:
2069: 2070: 2071:
2072:
2073:
2074:
2075:
2076: 2077: 2078:
2079:
2080: public function addText($text, $pos = '20x20', $padding = 0, $fontColor='#fff', $fontSize = 12, $angle = 0, $font = null)
2081:
2082:
2083:
2084:
2085:
2086:
2087:
2088:
2089: {
2090:
2091:
2092: $rgbArray = $this->formatColor($fontColor);
2093: $r = $rgbArray['r'];
2094: $g = $rgbArray['g'];
2095: $b = $rgbArray['b'];
2096:
2097:
2098: $font = $this->getTextFont($font);
2099:
2100:
2101: $textSizeArray = $this->getTextSize($fontSize, $angle, $font, $text);
2102: $textWidth = $textSizeArray['width'];
2103: $textHeight = $textSizeArray['height'];
2104:
2105:
2106: $posArray = $this->calculatePosition($pos, $padding, $textWidth, $textHeight, false);
2107: $x = $posArray['width'];
2108: $y = $posArray['height'];
2109:
2110: $fontColor = imagecolorallocate($this->imageResized, $r, $g, $b);
2111:
2112:
2113: imagettftext($this->imageResized, $fontSize, $angle, $x, $y, $fontColor, $font, $text);
2114: }
2115:
2116:
2117:
2118: private function getTextFont($font)
2119: {
2120:
2121: $fontPath = __DIR__ . '/' . $this->fontDir;
2122:
2123:
2124:
2125: putenv('GDFONTPATH=' . realpath('.'));
2126:
2127:
2128: if ($font == null || !file_exists($font)) {
2129:
2130:
2131: $font = $fontPath . '/arimo.ttf';
2132:
2133:
2134: if (!file_exists($font)) {
2135:
2136:
2137: if ($this->debug) { throw new Exception('Font not found'); }else{ return false; }
2138: }
2139: }
2140:
2141: return $font;
2142:
2143: }
2144:
2145:
2146:
2147: private function getTextSize($fontSize, $angle, $font, $text)
2148: {
2149:
2150:
2151: $box = @imagettfbbox($fontSize, $angle, $font, $text);
2152:
2153:
2154: $textWidth = abs($box[4] - $box[0]);
2155:
2156:
2157: $textHeight = abs($box[5] - $box[1]);
2158:
2159: return array('height' => $textHeight, 'width' => $textWidth);
2160: }
2161:
2162:
2163: 2164: 2165:
2166:
2167: public function addWatermark($watermarkImage, $pos, $padding = 0, $opacity = 0)
2168:
2169:
2170:
2171:
2172:
2173:
2174:
2175:
2176:
2177:
2178:
2179:
2180:
2181:
2182:
2183:
2184:
2185:
2186:
2187:
2188:
2189:
2190:
2191:
2192: {
2193:
2194:
2195: $stamp = $this->openImage ($watermarkImage);
2196: $im = $this->imageResized;
2197:
2198:
2199: $sx = imagesx($stamp);
2200: $sy = imagesy($stamp);
2201:
2202:
2203: $posArray = $this->calculatePosition($pos, $padding, $sx, $sy);
2204: $x = $posArray['width'];
2205: $y = $posArray['height'];
2206:
2207:
2208: if (fix_strtolower(strrchr($watermarkImage, '.')) === '.png') {
2209:
2210: $opacity = $this->invertTransparency($opacity, 100);
2211: $this->filterOpacity($stamp, $opacity);
2212: }
2213:
2214:
2215: imagecopy($im, $stamp, $x, $y, 0, 0, imagesx($stamp), imagesy($stamp));
2216:
2217: }
2218:
2219:
2220:
2221: private function calculatePosition($pos, $padding, $assetWidth, $assetHeight, $upperLeft = true)
2222:
2223:
2224:
2225:
2226:
2227:
2228:
2229:
2230:
2231:
2232:
2233:
2234:
2235:
2236:
2237:
2238:
2239:
2240:
2241: {
2242: $pos = fix_strtolower($pos);
2243:
2244:
2245: if (strstr($pos, 'x')) {
2246: $pos = str_replace(' ', '', $pos);
2247:
2248: $xyArray = explode('x', $pos);
2249: list($width, $height) = $xyArray;
2250:
2251: } else {
2252:
2253: switch ($pos) {
2254: case 'tl':
2255: $width = 0 + $padding;
2256: $height = 0 + $padding;
2257: break;
2258:
2259: case 't':
2260: $width = ($this->width / 2) - ($assetWidth / 2);
2261: $height = 0 + $padding;
2262: break;
2263:
2264: case 'tr':
2265: $width = $this->width - $assetWidth - $padding;
2266: $height = 0 + $padding;;
2267: break;
2268:
2269: case 'l':
2270: $width = 0 + $padding;
2271: $height = ($this->height / 2) - ($assetHeight / 2);
2272: break;
2273:
2274: case 'm':
2275: $width = ($this->width / 2) - ($assetWidth / 2);
2276: $height = ($this->height / 2) - ($assetHeight / 2);
2277: break;
2278:
2279: case 'r':
2280: $width = $this->width - $assetWidth - $padding;
2281: $height = ($this->height / 2) - ($assetHeight / 2);
2282: break;
2283:
2284: case 'bl':
2285: $width = 0 + $padding;
2286: $height = $this->height - $assetHeight - $padding;
2287: break;
2288:
2289: case 'b':
2290: $width = ($this->width / 2) - ($assetWidth / 2);
2291: $height = $this->height - $assetHeight - $padding;
2292: break;
2293:
2294: case 'br':
2295: $width = $this->width - $assetWidth - $padding;
2296: $height = $this->height - $assetHeight - $padding;
2297: break;
2298:
2299: default:
2300: $width = 0;
2301: $height = 0;
2302: break;
2303: }
2304: }
2305:
2306: if (!$upperLeft) {
2307: $height = $height + $assetHeight;
2308: }
2309:
2310: return array('width' => $width, 'height' => $height);
2311: }
2312:
2313:
2314:
2315:
2316: private function filterOpacity(&$img, $opacity = 75)
2317:
2318:
2319:
2320:
2321:
2322:
2323:
2324:
2325:
2326:
2327:
2328: {
2329:
2330: if (!isset($opacity)) {
2331: return false;
2332: }
2333:
2334: if ($opacity == 100) {
2335: return true;
2336: }
2337:
2338: $opacity /= 100;
2339:
2340:
2341: $w = imagesx($img);
2342: $h = imagesy($img);
2343:
2344:
2345: imagealphablending($img, false);
2346:
2347:
2348: $minalpha = 127;
2349: for ($x = 0; $x < $w; $x++)
2350: for ($y = 0; $y < $h; $y++) {
2351: $alpha = ( imagecolorat($img, $x, $y) >> 24 ) & 0xFF;
2352: if ($alpha < $minalpha) {
2353: $minalpha = $alpha;
2354: }
2355: }
2356:
2357:
2358: for ($x = 0; $x < $w; $x++) {
2359: for ($y = 0; $y < $h; $y++) {
2360:
2361: $colorxy = imagecolorat($img, $x, $y);
2362: $alpha = ( $colorxy >> 24 ) & 0xFF;
2363:
2364: if ($minalpha !== 127) {
2365: $alpha = 127 + 127 * $opacity * ( $alpha - 127 ) / ( 127 - $minalpha );
2366: } else {
2367: $alpha += 127 * $opacity;
2368: }
2369:
2370: $alphacolorxy = imagecolorallocatealpha($img, ( $colorxy >> 16 ) & 0xFF, ( $colorxy >> 8 ) & 0xFF, $colorxy & 0xFF, $alpha);
2371:
2372: if (!imagesetpixel($img, $x, $y, $alphacolorxy)) {
2373:
2374: return false;
2375: }
2376: }
2377: }
2378:
2379: return true;
2380: }
2381:
2382:
2383:
2384: private function openImage($file)
2385:
2386:
2387:
2388:
2389:
2390:
2391:
2392:
2393: {
2394:
2395: if (!file_exists($file) && !$this->checkStringStartsWith('http://', $file)) { if ($this->debug) { throw new Exception('Image not found.'); }else{ throw new Exception(); }};
2396:
2397:
2398: $extension = strrchr($file, '.');
2399: $extension = fix_strtolower($extension);
2400: switch($extension)
2401: {
2402: case '.jpg':
2403: case '.jpeg':
2404: $img = @imagecreatefromjpeg($file);
2405: break;
2406: case '.gif':
2407: $img = @imagecreatefromgif($file);
2408: break;
2409: case '.png':
2410: $img = @imagecreatefrompng($file);
2411: break;
2412: case '.bmp':
2413: $img = @$this->imagecreatefrombmp($file);
2414: break;
2415: case '.psd':
2416: $img = @$this->imagecreatefrompsd($file);
2417: break;
2418:
2419:
2420:
2421:
2422: default:
2423: $img = false;
2424: break;
2425: }
2426:
2427: return $img;
2428: }
2429:
2430:
2431:
2432: public function reset()
2433:
2434:
2435:
2436:
2437:
2438:
2439:
2440:
2441: {
2442: $this->__construct($this->fileName);
2443: }
2444:
2445:
2446:
2447: public function saveImage($savePath, $imageQuality="100")
2448:
2449:
2450:
2451:
2452:
2453:
2454:
2455:
2456:
2457:
2458:
2459:
2460:
2461: {
2462:
2463:
2464: if (!is_resource($this->imageResized)) { if ($this->debug) { throw new Exception('saveImage: This is not a resource.'); }else{ throw new Exception(); }}
2465: $fileInfoArray = pathinfo($savePath);
2466: clearstatcache();
2467: if (!is_writable($fileInfoArray['dirname'])) { if ($this->debug) { throw new Exception('The path is not writable. Please check your permissions.'); }else{ throw new Exception(); }}
2468:
2469:
2470: $extension = strrchr($savePath, '.');
2471: $extension = fix_strtolower($extension);
2472:
2473: $error = '';
2474:
2475: switch($extension)
2476: {
2477: case '.jpg':
2478: case '.jpeg':
2479: $this->checkInterlaceImage($this->isInterlace);
2480: if (imagetypes() & IMG_JPG) {
2481: imagejpeg($this->imageResized, $savePath, $imageQuality);
2482: } else { $error = 'jpg'; }
2483: break;
2484:
2485: case '.gif':
2486: $this->checkInterlaceImage($this->isInterlace);
2487: if (imagetypes() & IMG_GIF) {
2488: imagegif($this->imageResized, $savePath);
2489: } else { $error = 'gif'; }
2490: break;
2491:
2492: case '.png':
2493:
2494: $scaleQuality = round(($imageQuality/100) * 9);
2495:
2496:
2497: $invertScaleQuality = 9 - $scaleQuality;
2498:
2499: $this->checkInterlaceImage($this->isInterlace);
2500: if (imagetypes() & IMG_PNG) {
2501: imagepng($this->imageResized, $savePath, $invertScaleQuality);
2502: } else { $error = 'png'; }
2503: break;
2504:
2505: case '.bmp':
2506: file_put_contents($savePath, $this->GD2BMPstring($this->imageResized));
2507: break;
2508:
2509:
2510:
2511:
2512: default:
2513:
2514: $this->errorArray[] = 'This file type (' . $extension . ') is not supported. File not saved.';
2515: break;
2516: }
2517:
2518:
2519:
2520:
2521: if ($error != '') {
2522: $this->errorArray[] = $error . ' support is NOT enabled. File not saved.';
2523: }
2524: }
2525:
2526:
2527:
2528: public function displayImage($fileType = 'jpg', $imageQuality="100")
2529:
2530:
2531:
2532:
2533:
2534:
2535:
2536:
2537: {
2538:
2539: if (!is_resource($this->imageResized)) { if ($this->debug) { throw new Exception('saveImage: This is not a resource.'); }else{ throw new Exception(); }}
2540:
2541: switch($fileType)
2542: {
2543: case 'jpg':
2544: case 'jpeg':
2545: header('Content-type: image/jpeg');
2546: imagejpeg($this->imageResized, '', $imageQuality);
2547: break;
2548: case 'gif':
2549: header('Content-type: image/gif');
2550: imagegif($this->imageResized);
2551: break;
2552: case 'png':
2553: header('Content-type: image/png');
2554:
2555:
2556: $scaleQuality = round(($imageQuality/100) * 9);
2557:
2558:
2559: $invertScaleQuality = 9 - $scaleQuality;
2560:
2561: imagepng($this->imageResized, '', $invertScaleQuality);
2562: break;
2563: case 'bmp':
2564: echo 'bmp file format is not supported.';
2565: break;
2566:
2567:
2568:
2569: default:
2570:
2571: break;
2572: }
2573:
2574:
2575:
2576: }
2577:
2578:
2579:
2580: public function setTransparency($bool)
2581:
2582: {
2583: $this->keepTransparency = $bool;
2584: }
2585:
2586:
2587:
2588: public function setFillColor($value)
2589:
2590:
2591:
2592:
2593:
2594:
2595:
2596:
2597:
2598:
2599: {
2600: $colorArray = $this->formatColor($value);
2601: $this->fillColorArray = $colorArray;
2602: }
2603:
2604:
2605:
2606: public function setCropFromTop($value)
2607:
2608: {
2609: $this->cropFromTopPercent = $value;
2610: }
2611:
2612:
2613:
2614: public function testGDInstalled()
2615:
2616:
2617:
2618:
2619:
2620:
2621:
2622:
2623: {
2624: if(extension_loaded('gd') && function_exists('gd_info'))
2625: {
2626: $gdInstalled = true;
2627: }
2628: else
2629: {
2630: $gdInstalled = false;
2631: }
2632:
2633: return $gdInstalled;
2634: }
2635:
2636:
2637:
2638: public function testEXIFInstalled()
2639:
2640:
2641:
2642:
2643:
2644:
2645:
2646:
2647: {
2648: if(extension_loaded('exif'))
2649: {
2650: $exifInstalled = true;
2651: }
2652: else
2653: {
2654: $exifInstalled = false;
2655: }
2656:
2657: return $exifInstalled;
2658: }
2659:
2660:
2661:
2662: public function testIsImage($image)
2663:
2664:
2665:
2666:
2667:
2668:
2669:
2670:
2671: {
2672: if ($image)
2673: {
2674: $fileIsImage = true;
2675: }
2676: else
2677: {
2678: $fileIsImage = false;
2679: }
2680:
2681: return $fileIsImage;
2682: }
2683:
2684:
2685:
2686: public function testFunct()
2687:
2688:
2689:
2690:
2691:
2692:
2693:
2694:
2695: {
2696: echo $this->height;
2697: }
2698:
2699:
2700:
2701: public function setForceStretch($value)
2702:
2703:
2704:
2705:
2706:
2707:
2708:
2709:
2710: {
2711: $this->forceStretch = $value;
2712: }
2713:
2714:
2715:
2716: public function setFile($fileName)
2717:
2718:
2719:
2720:
2721:
2722:
2723:
2724:
2725: {
2726: self::__construct($fileName);
2727: }
2728:
2729:
2730:
2731: public function getFileName()
2732:
2733:
2734:
2735:
2736:
2737:
2738:
2739:
2740: {
2741: return $this->fileName;
2742: }
2743:
2744:
2745:
2746: public function getHeight()
2747: {
2748: return $this->height;
2749: }
2750:
2751:
2752:
2753: public function getWidth()
2754: {
2755: return $this->width;
2756: }
2757:
2758:
2759:
2760: public function getOriginalHeight()
2761: {
2762: return $this->heightOriginal;
2763: }
2764:
2765:
2766:
2767: public function getOriginalWidth()
2768: {
2769: return $this->widthOriginal;
2770: }
2771:
2772:
2773:
2774: public function getErrors()
2775:
2776:
2777:
2778:
2779:
2780:
2781:
2782:
2783: {
2784: return $this->errorArray;
2785: }
2786:
2787:
2788:
2789: private function checkInterlaceImage($isEnabled)
2790:
2791: {
2792: if ($isEnabled) {
2793: imageinterlace($this->imageResized, $isEnabled);
2794: }
2795: }
2796:
2797:
2798:
2799: protected function formatColor($value)
2800:
2801:
2802:
2803:
2804:
2805:
2806:
2807:
2808:
2809: {
2810: $rgbArray = array();
2811:
2812:
2813: if (is_array($value)) {
2814:
2815: if (key($value) == 0 && count($value) == 3) {
2816:
2817: $rgbArray['r'] = $value[0];
2818: $rgbArray['g'] = $value[1];
2819: $rgbArray['b'] = $value[2];
2820:
2821: } else {
2822: $rgbArray = $value;
2823: }
2824: } else if (fix_strtolower($value) === 'transparent') {
2825:
2826: $rgbArray = array(
2827: 'r' => 255,
2828: 'g' => 255,
2829: 'b' => 255,
2830: 'a' => 127
2831: );
2832:
2833: } else {
2834:
2835:
2836: $rgbArray = $this -> hex2dec($value);
2837: }
2838:
2839: return $rgbArray;
2840: }
2841:
2842:
2843:
2844: function hex2dec($hex)
2845:
2846: {
2847: $color = str_replace('#', '', $hex);
2848:
2849: if (strlen($color) == 3) {
2850: $color = $color . $color;
2851: }
2852:
2853: $rgb = array(
2854: 'r' => hexdec(substr($color, 0, 2)),
2855: 'g' => hexdec(substr($color, 2, 2)),
2856: 'b' => hexdec(substr($color, 4, 2)),
2857: 'a' => 0
2858: );
2859: return $rgb;
2860: }
2861:
2862:
2863:
2864: private function createImageColor ($colorArray)
2865: {
2866: $r = $colorArray['r'];
2867: $g = $colorArray['g'];
2868: $b = $colorArray['b'];
2869:
2870: return imagecolorallocate($this->imageResized, $r, $g, $b);
2871: }
2872:
2873:
2874:
2875: private function testColorExists($colorArray)
2876: {
2877: $r = $colorArray['r'];
2878: $g = $colorArray['g'];
2879: $b = $colorArray['b'];
2880:
2881: if (imagecolorexact($this->imageResized, $r, $g, $b) == -1) {
2882: return false;
2883: } else {
2884: return true;
2885: }
2886: }
2887:
2888:
2889:
2890: private function findUnusedGreen()
2891:
2892:
2893: {
2894: $green = 255;
2895:
2896: do {
2897:
2898: $greenChroma = array(0, $green, 0);
2899: $colorArray = $this->formatColor($greenChroma);
2900: $match = $this->testColorExists($colorArray);
2901: $green--;
2902:
2903: } while ($match == false && $green > 0);
2904:
2905:
2906: if (!$match) {
2907: $greenChroma = array(0, $green, 0);
2908: }
2909:
2910: return $greenChroma;
2911: }
2912:
2913:
2914:
2915: private function findUnusedBlue()
2916:
2917:
2918: {
2919: $blue = 255;
2920:
2921: do {
2922:
2923: $blueChroma = array(0, 0, $blue);
2924: $colorArray = $this->formatColor($blueChroma);
2925: $match = $this->testColorExists($colorArray);
2926: $blue--;
2927:
2928: } while ($match == false && $blue > 0);
2929:
2930:
2931: if (!$match) {
2932: $blueChroma = array(0, 0, $blue);
2933: }
2934:
2935: return $blueChroma;
2936: }
2937:
2938:
2939:
2940: private function invertTransparency($value, $originalMax, $invert=true)
2941:
2942:
2943:
2944:
2945: {
2946:
2947: if ($value > $originalMax) {
2948: $value = $originalMax;
2949: }
2950:
2951:
2952: if ($value < 0) {
2953: $value = 0;
2954: }
2955:
2956: if ($invert) {
2957: return $originalMax - (($value/100) * $originalMax);
2958: } else {
2959: return ($value/100) * $originalMax;
2960: }
2961: }
2962:
2963:
2964:
2965: private function transparentImage($src)
2966: {
2967:
2968: $r1 = 0;
2969: $g1 = 255;
2970: $b1 = 0;
2971: for ($x = 0; $x < imagesx($src); ++$x) {
2972: for ($y = 0; $y < imagesy($src); ++$y) {
2973: $color = imagecolorat($src, $x, $y);
2974: $r = ($color >> 16) & 0xFF;
2975: $g = ($color >> 8) & 0xFF;
2976: $b = $color & 0xFF;
2977: for ($i = 0; $i < 270; $i++) {
2978:
2979: if ($r == 0 && $g == 255 && $b == 0) {
2980:
2981: $trans_colour = imagecolorallocatealpha($src, 0, 0, 0, 127);
2982: imagefill($src, $x, $y, $trans_colour);
2983: }
2984: }
2985: }
2986: }
2987:
2988: return $src;
2989: }
2990:
2991:
2992:
2993: function checkStringStartsWith($needle, $haystack)
2994:
2995: {
2996: return (substr($haystack, 0, strlen($needle))==$needle);
2997: }
2998:
2999:
3000: 3001: 3002:
3003:
3004: private function GD2BMPstring(&$gd_image)
3005:
3006:
3007:
3008:
3009:
3010:
3011:
3012:
3013:
3014: {
3015: $imageX = imagesx($gd_image);
3016: $imageY = imagesy($gd_image);
3017:
3018: $BMP = '';
3019: for ($y = ($imageY - 1); $y >= 0; $y--) {
3020: $thisline = '';
3021: for ($x = 0; $x < $imageX; $x++) {
3022: $argb = $this->GetPixelColor($gd_image, $x, $y);
3023: $thisline .= chr($argb['blue']).chr($argb['green']).chr($argb['red']);
3024: }
3025: while (strlen($thisline) % 4) {
3026: $thisline .= "\x00";
3027: }
3028: $BMP .= $thisline;
3029: }
3030:
3031: $bmpSize = strlen($BMP) + 14 + 40;
3032:
3033: $BITMAPFILEHEADER = 'BM';
3034: $BITMAPFILEHEADER .= $this->LittleEndian2String($bmpSize, 4);
3035: $BITMAPFILEHEADER .= $this->LittleEndian2String( 0, 2);
3036: $BITMAPFILEHEADER .= $this->LittleEndian2String( 0, 2);
3037: $BITMAPFILEHEADER .= $this->LittleEndian2String( 54, 4);
3038:
3039:
3040: $BITMAPINFOHEADER = $this->LittleEndian2String( 40, 4);
3041: $BITMAPINFOHEADER .= $this->LittleEndian2String( $imageX, 4);
3042: $BITMAPINFOHEADER .= $this->LittleEndian2String( $imageY, 4);
3043: $BITMAPINFOHEADER .= $this->LittleEndian2String( 1, 2);
3044: $BITMAPINFOHEADER .= $this->LittleEndian2String( 24, 2);
3045: $BITMAPINFOHEADER .= $this->LittleEndian2String( 0, 4);
3046: $BITMAPINFOHEADER .= $this->LittleEndian2String( 0, 4);
3047: $BITMAPINFOHEADER .= $this->LittleEndian2String( 2835, 4);
3048: $BITMAPINFOHEADER .= $this->LittleEndian2String( 2835, 4);
3049: $BITMAPINFOHEADER .= $this->LittleEndian2String( 0, 4);
3050: $BITMAPINFOHEADER .= $this->LittleEndian2String( 0, 4);
3051:
3052: return $BITMAPFILEHEADER.$BITMAPINFOHEADER.$BMP;
3053: }
3054:
3055:
3056:
3057: private function GetPixelColor(&$img, $x, $y)
3058:
3059:
3060:
3061:
3062:
3063:
3064:
3065: {
3066: if (!is_resource($img)) {
3067: return false;
3068: }
3069: return @imagecolorsforindex($img, @imagecolorat($img, $x, $y));
3070: }
3071:
3072:
3073:
3074: private function LittleEndian2String($number, $minbytes=1)
3075:
3076:
3077:
3078:
3079:
3080:
3081:
3082: {
3083: $intstring = '';
3084: while ($number > 0) {
3085: $intstring = $intstring.chr($number & 255);
3086: $number >>= 8;
3087: }
3088: return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
3089: }
3090:
3091:
3092: 3093: 3094:
3095:
3096: private function ImageCreateFromBMP($filename)
3097:
3098:
3099:
3100:
3101:
3102:
3103:
3104:
3105:
3106:
3107:
3108:
3109: {
3110:
3111:
3112: if (! $f1 = fopen($filename,"rb")) return FALSE;
3113:
3114:
3115: $FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14));
3116: if ($FILE['file_type'] != 19778) return FALSE;
3117:
3118:
3119: $BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
3120: '/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
3121: '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40));
3122: $BMP['colors'] = pow(2,$BMP['bits_per_pixel']);
3123:
3124: if ($BMP['size_bitmap'] == 0) $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
3125:
3126: $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8;
3127: $BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);
3128: $BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4);
3129: $BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4);
3130: $BMP['decal'] = 4-(4*$BMP['decal']);
3131:
3132: if ($BMP['decal'] == 4) $BMP['decal'] = 0;
3133:
3134:
3135: $PALETTE = array();
3136: if ($BMP['colors'] < 16777216)
3137: {
3138: $PALETTE = unpack('V'.$BMP['colors'], fread($f1,$BMP['colors']*4));
3139: }
3140:
3141:
3142: $IMG = fread($f1,$BMP['size_bitmap']);
3143: $VIDE = chr(0);
3144:
3145: $res = imagecreatetruecolor($BMP['width'],$BMP['height']);
3146: $P = 0;
3147: $Y = $BMP['height']-1;
3148: while ($Y >= 0)
3149: {
3150: $X=0;
3151: while ($X < $BMP['width'])
3152: {
3153: if ($BMP['bits_per_pixel'] == 24)
3154: $COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
3155: elseif ($BMP['bits_per_pixel'] == 16)
3156: {
3157:
3158: 3159: 3160: 3161: 3162: 3163: 3164: 3165: 3166: 3167: 3168:
3169:
3170:
3171:
3172:
3173:
3174: $COLOR = unpack("v",substr($IMG,$P,2));
3175: $blue = ($COLOR[1] & 0x001f) << 3;
3176: $green = ($COLOR[1] & 0x07e0) >> 3;
3177: $red = ($COLOR[1] & 0xf800) >> 8;
3178: $COLOR[1] = $red * 65536 + $green * 256 + $blue;
3179:
3180: }
3181: elseif ($BMP['bits_per_pixel'] == 8)
3182: {
3183: $COLOR = unpack("n",$VIDE.substr($IMG,$P,1));
3184: $COLOR[1] = $PALETTE[$COLOR[1]+1];
3185: }
3186: elseif ($BMP['bits_per_pixel'] == 4)
3187: {
3188: $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
3189: if (($P*2)%2 == 0) $COLOR[1] = ($COLOR[1] >> 4) ; else $COLOR[1] = ($COLOR[1] & 0x0F);
3190: $COLOR[1] = $PALETTE[$COLOR[1]+1];
3191: }
3192: elseif ($BMP['bits_per_pixel'] == 1)
3193: {
3194: $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
3195: if (($P*8)%8 == 0) $COLOR[1] = $COLOR[1] >>7;
3196: elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6;
3197: elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5;
3198: elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4;
3199: elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3;
3200: elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2;
3201: elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1;
3202: elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1);
3203: $COLOR[1] = $PALETTE[$COLOR[1]+1];
3204: }
3205: else
3206: return FALSE;
3207:
3208: imagesetpixel($res,$X,$Y,$COLOR[1]);
3209: $X++;
3210: $P += $BMP['bytes_per_pixel'];
3211: }
3212:
3213: $Y--;
3214: $P+=$BMP['decal'];
3215: }
3216:
3217: fclose($f1);
3218:
3219: return $res;
3220: }
3221:
3222:
3223: 3224: 3225:
3226:
3227: private function imagecreatefrompsd($fileName)
3228:
3229:
3230:
3231:
3232:
3233:
3234:
3235:
3236: {
3237: if (file_exists($this->psdReaderPath)) {
3238:
3239:
3240: include_once($this->psdReaderPath);
3241:
3242: $psdReader = new PhpPsdReader($fileName);
3243:
3244: if (isset($psdReader->infoArray['error'])) return '';
3245: else return $psdReader->getImage();
3246: } else {
3247: return false;
3248: }
3249: }
3250:
3251:
3252:
3253: public function __destruct() {
3254: if (is_resource($this->imageResized)) {
3255: imagedestroy($this->imageResized);
3256: }
3257: }
3258:
3259:
3260:
3261: }
3262:
3263:
3264:
3265:
3266: 3267: 3268: 3269: 3270: 3271: 3272: 3273: 3274: 3275: 3276: 3277: 3278: 3279: 3280: 3281: 3282: 3283: 3284: 3285: 3286: 3287: 3288: 3289: 3290: 3291: 3292: 3293: 3294: 3295: 3296: 3297: 3298: 3299: 3300: 3301: 3302: 3303: 3304: 3305: 3306: 3307: 3308: 3309: 3310: 3311: 3312: 3313: 3314: 3315: 3316: 3317: 3318: 3319:
3320: