1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
21:
22: 23: 24: 25: 26: 27: 28: 29: 30: 31:
32: function cApiImgScaleGetMD5CacheFile($sImg, $iMaxX, $iMaxY, $bCrop, $bExpand) {
33: if (!cFileHandler::exists($sImg)) {
34: return false;
35: }
36:
37: $iFilesize = filesize($sImg);
38:
39: if (function_exists('md5_file')) {
40: $sMD5 = md5(implode('', array(
41: $sImg,
42: md5_file($sImg),
43: $iFilesize,
44: $iMaxX,
45: $iMaxY,
46: $bCrop,
47: $bExpand
48: )));
49: } else {
50: $sMD5 = md5(implode('', array(
51: $sImg,
52: $iFilesize,
53: $iMaxX,
54: $iMaxY,
55: $bCrop,
56: $bExpand
57: )));
58: }
59:
60: return $sMD5;
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: function cApiImgScaleLQ($img, $maxX, $maxY, $crop = false, $expand = false, $cacheTime = 10, $quality = 0, $keepType = false) {
88: global $cfgClient, $client, $cfg;
89:
90: if (!cFileHandler::exists($img)) {
91: return false;
92: }
93:
94: if ($quality == 0 && isset($cfg['images']['image_quality']['compression_rate'])) {
95: $quality = (int) $cfg['images']['image_quality']['compression_rate'];
96: }
97:
98: if ($quality == 0) {
99: $quality = 75;
100: }
101:
102: $filename = $img;
103: $maxX = (int) $maxX;
104: $maxY = (int) $maxY;
105: $cacheTime = (int) $cacheTime;
106: $quality = (int) $quality;
107:
108: if ($quality <= 0 || $quality > 100) {
109: $quality = 75;
110: }
111:
112: $frontendURL = cRegistry::getFrontendUrl();
113: $filetype = substr($filename, strlen($filename) - 4, 4);
114: $md5 = cApiImgScaleGetMD5CacheFile($img, $maxX, $maxY, $crop, $expand);
115: $cfileName = cApiImageGetCacheFileName($md5, $filetype, $keepType);
116: $cacheFile = $cfgClient[$client]['cache']['path'] . $cfileName;
117: $webFile = $frontendURL . 'cache/' . $cfileName;
118:
119: if (cApiImageCheckCachedImageValidity($cacheFile, $cacheTime)) {
120: return $webFile;
121: }
122:
123:
124: switch (strtolower($filetype)) {
125: case '.gif':
126: $function = 'imagecreatefromgif';
127: break;
128: case '.png':
129: $function = 'imagecreatefrompng';
130: break;
131: case '.jpg':
132: $function = 'imagecreatefromjpeg';
133: break;
134: case 'jpeg':
135: $function = 'imagecreatefromjpeg';
136: break;
137: default:
138: return false;
139: }
140:
141: if (function_exists($function)) {
142: $imageHandle = @$function($filename);
143: }
144:
145:
146: if (!$imageHandle) {
147: return false;
148: }
149:
150: $x = imagesx($imageHandle);
151: $y = imagesy($imageHandle);
152:
153: list($targetX, $targetY) = cApiImageGetTargetDimensions($x, $y, $maxX, $maxY, $expand);
154:
155:
156: if ($crop) {
157:
158: $targetImage = imagecreate($maxX, $maxY);
159: imagecopy($targetImage, $imageHandle, 0, 0, 0, 0, $maxX, $maxY);
160: } else {
161:
162: $targetImage = imagecreate($targetX, $targetY);
163: imagecopyresized($targetImage, $imageHandle, 0, 0, 0, 0, $targetX, $targetY, $x, $y);
164: }
165:
166:
167: if ($keepType) {
168: switch (strtolower($filetype)) {
169: case '.png':
170: imagepng($targetImage, $cacheFile);
171:
172: break;
173: case '.gif':
174: imagegif($targetImage, $cacheFile);
175:
176: break;
177: default:
178: imagejpeg($targetImage, $cacheFile, $quality);
179: }
180: } else {
181: imagejpeg($targetImage, $cacheFile, $quality);
182: }
183:
184: return ($webFile);
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: function cApiImgScaleHQ($img, $maxX, $maxY, $crop = false, $expand = false, $cacheTime = 10, $quality = 0, $keepType = true) {
215: global $cfgClient, $client, $cfg;
216:
217: if (!cFileHandler::exists($img)) {
218: return false;
219: }
220:
221: if ($quality == 0 && isset($cfg['images']['image_quality']['compression_rate'])) {
222: $quality = (int) $cfg['images']['image_quality']['compression_rate'];
223: }
224:
225: if ($quality == 0) {
226: $quality = 75;
227: }
228:
229: $filename = $img;
230: $maxX = (int) $maxX;
231: $maxY = (int) $maxY;
232: $cacheTime = (int) $cacheTime;
233: $quality = (int) $quality;
234:
235: if ($quality <= 0 || $quality > 100) {
236: $quality = 75;
237: }
238:
239: $frontendURL = cRegistry::getFrontendUrl();
240: $filetype = cFileHandler::getExtension($filename);
241: $md5 = cApiImgScaleGetMD5CacheFile($img, $maxX, $maxY, $crop, $expand);
242: $cfileName = cApiImageGetCacheFileName($md5, $filetype, $keepType);
243: $cacheFile = $cfgClient[$client]['cache']['path'] . $cfileName;
244: $webFile = $frontendURL . 'cache/' . $cfileName;
245:
246: if (cApiImageCheckCachedImageValidity($cacheFile, $cacheTime)) {
247: return $webFile;
248: }
249:
250:
251: switch (strtolower($filetype)) {
252: case 'gif':
253: $function = 'imagecreatefromgif';
254: break;
255: case 'png':
256: $function = 'imagecreatefrompng';
257: break;
258: case 'jpg':
259: $function = 'imagecreatefromjpeg';
260: break;
261: case 'jpeg':
262: $function = 'imagecreatefromjpeg';
263: break;
264: default:
265: return false;
266: }
267:
268: if (function_exists($function)) {
269: $imageHandle = @$function($filename);
270: }
271:
272:
273: if (!$imageHandle) {
274: return false;
275: }
276:
277: $x = imagesx($imageHandle);
278: $y = imagesy($imageHandle);
279:
280: list($targetX, $targetY) = cApiImageGetTargetDimensions($x, $y, $maxX, $maxY, $expand);
281:
282:
283: if ($crop) {
284:
285: $targetImage = imagecreatetruecolor($maxX, $maxY);
286:
287: $srcX = ($x - $maxX) / 2;
288: $srcY = ($y - $maxY) / 2;
289:
290: imagecopy($targetImage, $imageHandle, 0, 0, $srcX, $srcY, $maxX, $maxY);
291: } else {
292:
293: $targetImage = imagecreatetruecolor($targetX, $targetY);
294:
295:
296: if (strtolower($filetype) == '.gif' or strtolower($filetype) == '.png') {
297: imagecolortransparent($targetImage, imagecolorallocatealpha($targetImage, 0, 0, 0, 127));
298: imagealphablending($targetImage, false);
299: imagesavealpha($targetImage, true);
300: }
301:
302: imagecopyresampled($targetImage, $imageHandle, 0, 0, 0, 0, $targetX, $targetY, $x, $y);
303: }
304:
305:
306: if ($keepType) {
307: switch (strtolower($filetype)) {
308: case '.png':
309: imagepng($targetImage, $cacheFile);
310:
311: break;
312: case '.gif':
313: imagegif($targetImage, $cacheFile);
314: break;
315: default:
316: imagejpeg($targetImage, $cacheFile, $quality);
317: }
318: } else {
319: imagejpeg($targetImage, $cacheFile, $quality);
320: }
321:
322: return $webFile;
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: function cApiImgScaleImageMagick($img, $maxX, $maxY, $crop = false, $expand = false, $cacheTime = 10, $quality = 0, $keepType = false) {
352: global $cfg, $cfgClient, $client;
353:
354: if (!cFileHandler::exists($img)) {
355: return false;
356: } elseif (isFunctionDisabled('escapeshellarg') || isFunctionDisabled('exec')) {
357: return false;
358: }
359:
360: if ($quality == 0 && isset($cfg['images']['image_quality']['compression_rate'])) {
361: $quality = (int) $cfg['images']['image_quality']['compression_rate'];
362: }
363:
364: if ($quality == 0) {
365: $quality = 75;
366: }
367:
368: $filename = $img;
369: $maxX = (int) $maxX;
370: $maxY = (int) $maxY;
371: $cacheTime = (int) $cacheTime;
372: $quality = (int) $quality;
373:
374: if ($quality <= 0 || $quality > 100) {
375: $quality = 75;
376: }
377:
378: $frontendURL = cRegistry::getFrontendUrl();
379: $filetype = substr($filename, strlen($filename) - 4, 4);
380: $md5 = cApiImgScaleGetMD5CacheFile($img, $maxX, $maxY, $crop, $expand);
381: $cfileName = cApiImageGetCacheFileName($md5, $filetype, $keepType);
382: $cacheFile = $cfgClient[$client]['cache']['path'] . $cfileName;
383: $webFile = $frontendURL . 'cache/' . $cfileName;
384:
385: if (cApiImageCheckCachedImageValidity($cacheFile, $cacheTime)) {
386: return $webFile;
387: }
388:
389: list($x, $y) = @getimagesize($filename);
390: if ($x == 0 || $y == 0) {
391: return false;
392: }
393:
394: list($targetX, $targetY) = cApiImageGetTargetDimensions($x, $y, $maxX, $maxY, $expand);
395:
396:
397: if ($filetype == '.gif') {
398: if (cApiImageIsAnimGif($filename)) {
399: $filename .= '[0]';
400: }
401: }
402:
403:
404: $output = array();
405: $retVal = 0;
406: $program = escapeshellarg($cfg['images']['image_magick']['path'] . 'convert');
407: $source = escapeshellarg($filename);
408: $destination = escapeshellarg($cacheFile);
409: if ($crop) {
410: $cmd = "'{$program}' -gravity center -quality {$quality} -crop {$maxX}x{$maxY}+1+1 '{$source}' '{$destination}'";
411: } else {
412: $cmd = "'{$program}' -quality {$quality} -geometry {$targetX}x{$targetY} '{$source}' '{$destination}'";
413: }
414:
415: exec($cmd, $output, $retVal);
416:
417: if (!cFileHandler::exists($cacheFile)) {
418: return false;
419: } else {
420: return $webFile;
421: }
422: }
423:
424: 425: 426: 427: 428: 429:
430: function cApiImageIsAnimGif($sFile) {
431: global $cfg;
432:
433: if (isFunctionDisabled('escapeshellarg') || isFunctionDisabled('exec')) {
434:
435: return false;
436: } elseif ('im' != cApiImageCheckImageEditingPosibility()) {
437:
438: return false;
439: }
440:
441: $output = array();
442: $retval = 0;
443: $program = escapeshellarg($cfg['images']['image_magick']['path'] . 'identify');
444: $source = escapeshellarg($sFile);
445:
446: exec("'{$program}' '{$source}'", $output, $retval);
447:
448: if (count($output) == 1) {
449: return false;
450: }
451:
452: return true;
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: function cApiImgScale($img, $maxX, $maxY, $crop = false, $expand = false, $cacheTime = 10, $wantHQ = false, $quality = 0, $keepType = true) {
484: global $client, $cfgClient, $cfg;
485:
486: $deleteAfter = false;
487:
488: if ($quality == 0 && isset($cfg['images']['image_quality']['compression_rate'])) {
489: $quality = (int) $cfg['images']['image_quality']['compression_rate'];
490: }
491:
492: if ($quality == 0) {
493: $quality = 75;
494: }
495:
496: $sRelativeImg = str_replace($cfgClient[$client]['upl']['path'], '', $img);
497: if (cApiDbfs::isDbfs($sRelativeImg)) {
498:
499: $dbfs = new cApiDbfsCollection();
500:
501: $file = basename($sRelativeImg);
502:
503: $dbfs->writeToFile($sRelativeImg, $cfgClient[$client]['cache']['path'] . $file);
504:
505: $img = $cfgClient[$client]['cache']['path'] . $file;
506: $deleteAfter = true;
507: } else if (!cFileHandler::exists($img)) {
508:
509: if (cFileHandler::exists($cfgClient[$client]['upl']['path'] . $img) && !is_dir($cfgClient[$client]['upl']['path'] . $img)) {
510: $img = $cfgClient[$client]['upl']['path'] . $img;
511: } else {
512:
513: return false;
514: }
515: }
516:
517: $filename = $img;
518: $filetype = substr($filename, strlen($filename) - 4, 4);
519:
520: $mxdAvImgEditingPosibility = cApiImageCheckImageEditingPosibility();
521: switch ($mxdAvImgEditingPosibility) {
522: case '1':
523: $method = 'gd1';
524: if (!function_exists('imagecreatefromgif') && $filetype == '.gif') {
525: $method = 'failure';
526: }
527: break;
528: case '2':
529: $method = 'gd2';
530: if (!function_exists('imagecreatefromgif') && $filetype == '.gif') {
531: $method = 'failure';
532: }
533: break;
534: case 'im':
535: $method = 'im';
536: break;
537: case '0':
538: $method = 'failure';
539: break;
540: default:
541: $method = 'failure';
542: break;
543: }
544:
545: switch ($method) {
546: case 'gd1':
547: $return = cApiImgScaleLQ($img, $maxX, $maxY, $crop, $expand, $cacheTime, $quality, $keepType);
548: break;
549: case 'gd2':
550: $return = cApiImgScaleHQ($img, $maxX, $maxY, $crop, $expand, $cacheTime, $quality, $keepType);
551: break;
552: case 'im':
553: $return = cApiImgScaleImageMagick($img, $maxX, $maxY, $crop, $expand, $cacheTime, $quality, $keepType);
554: break;
555: case 'failure':
556: $frontendURL = cRegistry::getFrontendUrl();
557: $return = str_replace(cRegistry::getFrontendPath(), $frontendURL, $img);
558: break;
559: }
560:
561: if ($deleteAfter == true) {
562: unlink($img);
563: }
564:
565: return $return;
566: }
567:
568: 569: 570: 571: 572: 573: 574: 575: 576: 577: 578:
579: function cApiImageCheckImageEditingPosibility() {
580: global $cfg;
581:
582: if ($cfg['images']['image_magick']['use']) {
583: if (cApiIsImageMagickAvailable()) {
584: return 'im';
585: }
586: }
587:
588: if (!extension_loaded('gd')) {
589: return '0';
590: }
591:
592: if (function_exists('gd_info')) {
593: $sGDVersion = '';
594: $aGDInformations = gd_info();
595: if (preg_match('#([0-9\.])+#', $aGDInformations['GD Version'], $sGDVersion)) {
596: if ($sGDVersion[0] >= '2') {
597: return '2';
598: }
599: return '1';
600: }
601: return '1';
602: }
603: return '1';
604: }
605:
606: 607: 608: 609: 610: 611: 612: 613: 614: 615:
616: function cApiImageGetTargetDimensions($x, $y, $maxX, $maxY, $expand) {
617: if (($maxX / $x) < ($maxY / $y)) {
618: $targetY = $y * ($maxX / $x);
619: $targetX = round($maxX);
620:
621:
622: if ($targetY < $maxY) {
623: $targetY = ceil($targetY);
624: } else {
625: $targetY = floor($targetY);
626: }
627: } else {
628: $targetX = $x * ($maxY / $y);
629: $targetY = round($maxY);
630:
631:
632: if ($targetX < $maxX) {
633: $targetX = ceil($targetX);
634: } else {
635: $targetX = floor($targetX);
636: }
637: }
638:
639: if ($expand == false && (($targetX > $x) || ($targetY > $y))) {
640: $targetX = $x;
641: $targetY = $y;
642: }
643:
644: $targetX = ($targetX != 0) ? $targetX : 1;
645: $targetY = ($targetY != 0) ? $targetY : 1;
646:
647: return array(
648: $targetX,
649: $targetY
650: );
651: }
652:
653: 654: 655: 656: 657: 658: 659: 660:
661: function cApiImageGetCacheFileName($md5, $filetype, $keepType) {
662:
663: if ($keepType) {
664:
665:
666: switch (strtolower($filetype)) {
667: case '.png':
668: $fileName = $md5 . '.png';
669: break;
670: case '.gif':
671: $fileName = $md5 . '.gif';
672: break;
673: default:
674: $fileName = $md5 . '.jpg';
675: }
676: } else {
677: $fileName = $md5 . '.jpg';
678: }
679: return $fileName;
680: }
681:
682: 683: 684: 685: 686: 687: 688: 689:
690: function cApiImageCheckCachedImageValidity($cacheFile, $cacheTime) {
691:
692: if (cFileHandler::exists($cacheFile)) {
693: if ($cacheTime == 0) {
694:
695: return true;
696: } else if (!function_exists('md5_file')) {
697:
698: if ((filemtime($cacheFile) + (60 * $cacheTime)) < time()) {
699:
700: unlink($cacheFile);
701: } else {
702:
703: return true;
704: }
705: } else {
706: return true;
707: }
708: }
709:
710: return false;
711: }
712:
713: 714: 715: 716: 717:
718: function cApiIsImageMagickAvailable() {
719: global $cfg;
720: static $imagemagickAvailable = NULL;
721:
722:
723: if (is_bool($imagemagickAvailable)) {
724: return $imagemagickAvailable;
725: }
726:
727:
728: if (isFunctionDisabled('escapeshellarg') || isFunctionDisabled('exec')) {
729: $imagemagickAvailable = false;
730: return $imagemagickAvailable;
731: }
732:
733:
734: $output = array();
735: $retval = 0;
736: $imPath = $cfg['images']['image_magick']['path'];
737: $program = escapeshellarg($cfg['images']['image_magick']['path'] . 'convert');
738: @exec("'{$program}' -version", $output, $retval);
739:
740: if (!is_array($output) || count($output) == 0) {
741:
742: $imagemagickAvailable = false;
743: } else {
744:
745:
746: if (strpos($output[0], 'ImageMagick') !== false) {
747: $imagemagickAvailable = true;
748: } else {
749: $imagemagickAvailable = false;
750: }
751: }
752:
753: return $imagemagickAvailable;
754: }
755:
756: ?>