Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob
  • Smarty
    • Cacher
    • Compiler
    • Config
    • Debug
    • PluginsBlock
    • PluginsFilter
    • PluginsFunction
    • PluginsInternal
    • PluginsModifier
    • PluginsModifierCompiler
    • PluginsShared
    • Security
    • Template
    • TemplateResources
  • Swift
    • ByteStream
    • CharacterStream
    • Encoder
    • Events
    • KeyCache
    • Mailer
    • Mime
    • Plugins
    • Transport

Classes

  • Swift_FailoverTransport
  • Swift_LoadBalancedTransport
  • Swift_MailTransport
  • Swift_Plugins_Loggers_ArrayLogger
  • Swift_Plugins_Loggers_EchoLogger
  • Swift_SendmailTransport
  • Swift_SmtpTransport
  • Swift_Transport_AbstractSmtpTransport
  • Swift_Transport_Esmtp_Auth_CramMd5Authenticator
  • Swift_Transport_Esmtp_Auth_LoginAuthenticator
  • Swift_Transport_Esmtp_Auth_PlainAuthenticator
  • Swift_Transport_Esmtp_AuthHandler
  • Swift_Transport_EsmtpTransport
  • Swift_Transport_FailoverTransport
  • Swift_Transport_LoadBalancedTransport
  • Swift_Transport_MailTransport
  • Swift_Transport_SendmailTransport
  • Swift_Transport_SimpleMailInvoker
  • Swift_Transport_StreamBuffer

Interfaces

  • Swift_Plugins_Logger
  • Swift_Plugins_Pop_Pop3Exception
  • Swift_Transport
  • Swift_Transport_Esmtp_Authenticator
  • Swift_Transport_EsmtpHandler
  • Swift_Transport_IoBuffer
  • Swift_Transport_MailInvoker
  • Swift_Transport_SmtpAgent
  • Swift_TransportException
  • Overview
  • Package
  • Function
  • Todo
  • Download
  1: <?php
  2: /**
  3:  * This file contains CONTENIDO Image API functions.
  4:  *
  5:  * If you are planning to add a function, please make sure that:
  6:  * 1.) The function is in the correct place
  7:  * 2.) The function is documented
  8:  * 3.) The function makes sense and is generically usable
  9:  *
 10:  * @package Core
 11:  * @subpackage Backend
 12:  * @version SVN Revision $Rev:$
 13:  *
 14:  * @author Timo Hummel
 15:  * @copyright four for business AG <www.4fb.de>
 16:  * @license http://www.contenido.org/license/LIZENZ.txt
 17:  * @link http://www.4fb.de
 18:  * @link http://www.contenido.org
 19:  */
 20: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 21: 
 22: /**
 23:  * Returns the MD5 filename used for caching.
 24:  *
 25:  * @param string $sImg Path to upload image
 26:  * @param int $iMaxX Maximum image x size
 27:  * @param int $iMaxY Maximum image y size
 28:  * @param bool $bCrop Flag to crop image
 29:  * @param bool $bExpand Flag to expand image
 30:  * @return string Path to the resulting image
 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:  * Scales (or crops) an image.
 65:  * If scaling, the aspect ratio is maintained.
 66:  *
 67:  * Returns the path to the scaled temporary image.
 68:  *
 69:  * Note that this function does some very poor caching;
 70:  * it calculates an md5 hash out of the image plus the
 71:  * maximum X and Y sizes, and uses that as the file name.
 72:  * If the file is older than 10 minutes, regenerate it.
 73:  *
 74:  * @param string $img The path to the image (relative to the frontend)
 75:  * @param int $maxX The maximum size in x-direction
 76:  * @param int $maxY The maximum size in y-direction
 77:  * @param bool $crop If true, the image is cropped and not scaled.
 78:  * @param bool $expand If true, the image is expanded (e.g. really scaled).
 79:  *        If false, the image will only be made smaller.
 80:  * @param int $cacheTime The number of minutes to cache the image, use 0 for
 81:  *        unlimited
 82:  * @param int $quality The quality of the output file
 83:  * @param bool $keepType If true and a png file is source, output file is also
 84:  *        png
 85:  * @return string url to the resulting image (http://...
 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 = cFileHandler::getExtension($filename);
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:     // Get out which file we have
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:     // If we can't open the image, return false
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:     // Create the target image with the target size, resize it afterwards.
156:     if ($crop) {
157:         // Create the target image with the max size, crop it afterwards.
158:         $targetImage = imagecreate($maxX, $maxY);
159:         imagecopy($targetImage, $imageHandle, 0, 0, 0, 0, $maxX, $maxY);
160:     } else {
161:         // Create the target image with the target size, resize it afterwards.
162:         $targetImage = imagecreate($targetX, $targetY);
163:         imagecopyresized($targetImage, $imageHandle, 0, 0, 0, 0, $targetX, $targetY, $x, $y);
164:     }
165: 
166:     // Output the file
167:     if ($keepType) {
168:         switch (strtolower($filetype)) {
169:             case 'png':
170:                 imagepng($targetImage, $cacheFile); // no quality option
171:                                                     // available
172:                 break;
173:             case 'gif':
174:                 imagegif($targetImage, $cacheFile); // no quality option
175:                                                     // available
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:  * Scales (or crops) an image in high quality.
189:  * If scaling, the aspect ratio is maintained.
190:  *
191:  * Note: GDLib 2.x is required!
192:  * Note: Image cropping calculates center of the image!
193:  *
194:  * Returns the path to the scaled temporary image.
195:  *
196:  * Note that this function does some very poor caching;
197:  * it calculates an md5 hash out of the image plus the
198:  * maximum X and Y sizes, and uses that as the file name.
199:  * If the file is older than the specified cache time, regenerate it.
200:  *
201:  * @param string $img The path to the image (relative to the frontend)
202:  * @param int $maxX The maximum size in x-direction
203:  * @param int $maxY The maximum size in y-direction
204:  * @param bool $crop If true, the image is cropped and not scaled.
205:  * @param bool $expand If true, the image is expanded (e.g. really scaled).
206:  *        If false, the image will only be made smaller.
207:  * @param int $cacheTime The number of minutes to cache the image, use 0 for
208:  *        unlimited
209:  * @param int $quality The quality of the output file
210:  * @param bool $keepType If true and a png file is source, output file is also
211:  *        png
212:  * @return string Url to the resulting image (http://...)
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:     // Get out which file we have
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:     // If we can't open the image, return false
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:     // Create the target image with the target size, resize it afterwards.
283:     if ($crop) {
284:         // Create the target image with the max size, crop it afterwards.
285:         $targetImage = imagecreatetruecolor($maxX, $maxY);
286:         // calculate canter of the image
287:         $srcX = ($x - $maxX) / 2;
288:         $srcY = ($y - $maxY) / 2;
289:         // crop image from center
290:         imagecopy($targetImage, $imageHandle, 0, 0, $srcX, $srcY, $maxX, $maxY);
291:     } else {
292:         // Create the target image with the target size, resize it afterwards.
293:         $targetImage = imagecreatetruecolor($targetX, $targetY);
294: 
295:         // Preserve transparency
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:     // Output the file
306:     if ($keepType) {
307:         switch (strtolower($filetype)) {
308:             case 'png':
309:                 imagepng($targetImage, $cacheFile); // no quality option
310:                                                     // available
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:  * Scales (or crops) an image using ImageMagick.
327:  * If scaling, the aspect ratio is maintained.
328:  *
329:  * Note: ImageMagick is required!
330:  *
331:  * Returns the path to the scaled temporary image.
332:  *
333:  * Note that this function does some very poor caching;
334:  * it calculates an md5 hash out of the image plus the
335:  * maximum X and Y sizes, and uses that as the file name.
336:  * If the file is older than the specified cache time, regenerate it.
337:  *
338:  * @param string $img The path to the image (relative to the frontend)
339:  * @param int $maxX The maximum size in x-direction
340:  * @param int $maxY The maximum size in y-direction
341:  * @param bool $crop If true, the image is cropped and not scaled.
342:  * @param bool $expand If true, the image is expanded (e.g. really scaled).
343:  *        If false, the image will only be made smaller.
344:  * @param int $cacheTime The number of minutes to cache the image, use 0 for
345:  *        unlimited
346:  * @param int $quality The quality of the output file
347:  * @param bool $keepType If true and a png file is source, output file is also
348:  *        png
349:  * @return string Url to the resulting image (http://...)
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 = cFileHandler::getExtension($filename);
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:     // If is animated gif resize first frame
397:     if ($filetype == 'gif') {
398:         if (cApiImageIsAnimGif($filename)) {
399:             $filename .= '[0]';
400:         }
401:     }
402: 
403:     // Try to execute convert
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:  * Check if gif is animated, uses "identify" of ImageMagick.
426:  *
427:  * @param string $sFile file path
428:  * @return bool True (gif is animated)/ false (single frame gif)
429:  */
430: function cApiImageIsAnimGif($sFile) {
431:     global $cfg;
432: 
433:     if (isFunctionDisabled('escapeshellarg') || isFunctionDisabled('exec')) {
434:         // Function escapeshellarg or exec is disabled
435:         return false;
436:     } elseif ('im' != cApiImageCheckImageEditingPosibility()) {
437:         // ImageMagick ist not available
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:  * Scales (or crops) an image.
457:  * If scaling, the aspect ratio is maintained.
458:  *
459:  * This function chooses the best method to scale, depending on the system
460:  * environment and/or the parameters.
461:  *
462:  * Returns the path to the scaled temporary image.
463:  *
464:  * Note that this function does some very poor caching;
465:  * it calculates an md5 hash out of the image plus the
466:  * maximum X and Y sizes, and uses that as the file name.
467:  * If the file is older than 10 minutes, regenerate it.
468:  *
469:  * @param string $img The path to the image (relative to the frontend)
470:  * @param int $maxX The maximum size in x-direction
471:  * @param int $maxY The maximum size in y-direction
472:  * @param bool $crop If true, the image is cropped and not scaled.
473:  * @param bool $expand If true, the image is expanded (e.g. really scaled).
474:  *        If false, the image will only be made smaller.
475:  * @param int $cacheTime The number of minutes to cache the image, use 0 for
476:  *        unlimited
477:  * @param bool $wantHQ If true, try to force high quality mode
478:  * @param int $quality The quality of the output file
479:  * @param bool $keepType If true and a png file is source, output file is also
480:  *        png
481:  * @return string Path to the resulting image
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:         // This check should be faster than a file existance check
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:         // Try with upload string
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:             // No, it's neither in the upload directory nor in the dbfs. return.
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': // gd1
523:             $method = 'gd1';
524:             if (!function_exists('imagecreatefromgif') && $filetype == '.gif') {
525:                 $method = 'failure';
526:             }
527:             break;
528:         case '2': // gd2
529:             $method = 'gd2';
530:             if (!function_exists('imagecreatefromgif') && $filetype == '.gif') {
531:                 $method = 'failure';
532:             }
533:             break;
534:         case 'im': // imagemagick
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:  * Check possible image editing functionality
570:  *
571:  * @return mixed Information about installed image editing extensions/tools
572:  *         <pre>
573:  *         - 'im' ImageMagick is available and usage is enabled
574:  *         - '2' GD library version 2 is available
575:  *         - '1' GD library version 1 is available
576:  *         - '0' Nothing could detected
577:  *         </pre>
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:  * Returns new calculated dimensions of a target image.
608:  *
609:  * @param int $x
610:  * @param int $y
611:  * @param int $maxX
612:  * @param int $maxY
613:  * @param bool $expand
614:  * @return array Index 0 is target X and index 1 is target Y
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:         // Force wished height
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:         // Force wished width
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:  * Returns cache file name.
655:  *
656:  * @param string $md5
657:  * @param string $filetype
658:  * @param bool $keepType
659:  * @return string
660:  */
661: function cApiImageGetCacheFileName($md5, $filetype, $keepType) {
662:     // Create the target file names for web and server
663:     if ($keepType) {
664:         // Should we keep the file type?
665:         // Just using switch if someone likes to add other types
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 { // No... use .jpg
677:         $fileName = $md5 . '.jpg';
678:     }
679:     return $fileName;
680: }
681: 
682: /**
683:  * Validates cache version of a image.
684:  *
685:  * @param string $cacheFile
686:  * @param int $cacheTime
687:  * @return bool Returns true, if cache file exists and7or is still valid or
688:  *         false
689:  */
690: function cApiImageCheckCachedImageValidity($cacheFile, $cacheTime) {
691:     // Check if the file exists. If it does, check if the file is valid.
692:     if (cFileHandler::exists($cacheFile)) {
693:         if ($cacheTime == 0) {
694:             // Do not check expiration date
695:             return true;
696:         } else if (!function_exists('md5_file')) {
697:             // TODO: Explain why this is still needed ... or remove it
698:             if ((filemtime($cacheFile) + (60 * $cacheTime)) < time()) {
699:                 // Cache time expired, unlink the file
700:                 unlink($cacheFile);
701:             } else {
702:                 // Return the web file name
703:                 return true;
704:             }
705:         } else {
706:             return true;
707:         }
708:     }
709: 
710:     return false;
711: }
712: 
713: /**
714:  * Checks if ImageMagick is available
715:  *
716:  * @return bool true if ImageMagick is available
717:  */
718: function cApiIsImageMagickAvailable() {
719:     global $cfg;
720:     static $imagemagickAvailable = NULL;
721: 
722:     // if the check has already been executed, just return the result
723:     if (is_bool($imagemagickAvailable)) {
724:         return $imagemagickAvailable;
725:     }
726: 
727:     // check, if escapeshellarg or exec function is disabled, we need both
728:     if (isFunctionDisabled('escapeshellarg') || isFunctionDisabled('exec')) {
729:         $imagemagickAvailable = false;
730:         return $imagemagickAvailable;
731:     }
732: 
733:     // otherwise execute the IM check
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:         // exec is probably disabled, so we assume IM to be unavailable
742:         $imagemagickAvailable = false;
743:     } else {
744:         // otherwise output contains the output of the command "convert version"
745:         // if IM is available, it contains the string "ImageMagick"
746:         if (strpos($output[0], 'ImageMagick') !== false) {
747:             $imagemagickAvailable = true;
748:         } else {
749:             $imagemagickAvailable = false;
750:         }
751:     }
752: 
753:     return $imagemagickAvailable;
754: }
755: 
756: ?>
CMS CONTENIDO 4.9.7 API documentation generated by ApiGen