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

Classes

  • cAjaxRequest
  • cAutoload
  • cBackend
  • cEffectiveSetting
  • cGuiScrollListAlltranslations
  • cHTMLValidator
  • cMailer
  • cModuleFileTranslation
  • cModuleHandler
  • cModuleSearch
  • cModuleSynchronizer
  • cModuleTemplateHandler
  • CodeMirror
  • cPasswordRequest
  • cPermission
  • cRegistry
  • cSystemPurge
  • cSystemtest
  • cTinymce4Configuration
  • cTinyMCE4Editor
  • cTinyMCEEditor
  • cWYSIWYGEditor
  • FrontendList
  • HtmlParser
  • TODOBackendList
  • TreeItem
  • UploadList
  • UploadSearchResultList

Functions

  • addArtspec
  • addSortImages
  • backToMainArea
  • buildArticleSelect
  • buildCategorySelect
  • buildCategorySelectRights
  • buildHeapTable
  • buildStackString
  • buildTree
  • buildUserOrGroupPermsFromRequest
  • callPluginStore
  • cApiCatGetLevelNode
  • cApiImageCheckCachedImageValidity
  • cApiImageCheckImageEditingPosibility
  • cApiImageGetCacheFileName
  • cApiImageGetTargetDimensions
  • cApiImageIsAnimGif
  • cApiImgScale
  • cApiImgScaleGetMD5CacheFile
  • cApiImgScaleHQ
  • cApiImgScaleImageMagick
  • cApiImgScaleLQ
  • cApiIsImageMagickAvailable
  • cApiStrCleanURLCharacters
  • cApiStrNormalizeLineEndings
  • cApiStrRecodeString
  • cApiStrReplaceDiacritics
  • cApiStrTrimAfterWord
  • cApiStrTrimHard
  • cApiStrTrimSentence
  • cDeprecated
  • cDie
  • cError
  • checkLangInClients
  • checkPathInformation
  • cInclude
  • compareUrlStrings
  • conChangeTemplateForCat
  • conCopyArticle
  • conCopyArtLang
  • conCopyContainerConf
  • conCopyContent
  • conCopyMetaTags
  • conCopyTemplateConfiguration
  • conCreateLocationString
  • conDeeperCategoriesArray
  • conDeleteart
  • conEditArt
  • conEditFirstTime
  • conFetchCategoryTree
  • conFlagOnOffline
  • conGenerateCode
  • conGenerateCodeForAllArts
  • conGenerateCodeForAllArtsInCategory
  • conGenerateCodeForAllartsUsingLayout
  • conGenerateCodeForAllartsUsingMod
  • conGenerateCodeForAllArtsUsingTemplate
  • conGenerateCodeForArtInAllCategories
  • conGenerateCodeForClient
  • conGenerateKeywords
  • conGetAvailableMetaTagTypes
  • conGetCategoryArticleId
  • conGetCategoryAssignments
  • conGetContainerConfiguration
  • conGetContentFromArticle
  • conGetHtmlTranslationTable
  • conGetMetaValue
  • conGetTemplateConfigurationIdForArticle
  • conGetTemplateConfigurationIdForCategory
  • conGetTopmostCat
  • conGetUsedModules
  • conHtmlentities
  • conHtmlEntityDecode
  • conHtmlSpecialChars
  • conIsLocked
  • conLock
  • conLockBulkEditing
  • conMakeArticleIndex
  • conMakeCatOnline
  • conMakeInlineScript
  • conMakeOnline
  • conMakeOnlineBulkEditing
  • conMakePublic
  • conMakeStart
  • conMoveArticles
  • conPhp54Check
  • conRemoveOldCategoryArticle
  • conSaveContentEntry
  • conSetCodeFlag
  • conSetCodeFlagBulkEditing
  • conSetMetaValue
  • conSyncArticle
  • copyRightsForElement
  • createBulkEditingFunction
  • createRandomName
  • createRightsForElement
  • cWarning
  • dbGetColumns
  • dbGetIndexes
  • dbGetPrimaryKeyName
  • dbTableExists
  • dbUpgradeTable
  • defineIfNotDefined
  • deleteArtspec
  • deleteRightsForElement
  • deleteSystemProperty
  • displayDatetime
  • emptyLogFile
  • endAndLogTiming
  • extractNumber
  • generateDisplayFilePath
  • generateJs
  • getAllClientsAndLanguages
  • getArtLang
  • getArtspec
  • getAvailableContentTypes
  • getCanonicalDay
  • getCanonicalMonth
  • getDirectorySize
  • getEffectiveSetting
  • getEffectiveSettingsByType
  • getEncodingByLanguage
  • getFileContents
  • getFileInformation
  • getFileType
  • getGroupOrUserName
  • getIDForArea
  • getJsHelpContext
  • getLanguageNamesByClient
  • getLanguagesByClient
  • getmicrotime
  • getNamedFrame
  • getParam
  • getParentAreaId
  • getSearchResults
  • getStrExpandCollapseButton
  • getSystemProperties
  • getSystemPropertiesByType
  • getSystemProperty
  • getTemplateSelect
  • getUplExpandCollapseButton
  • htmldecode
  • htmlentities_iso88592
  • humanReadableSize
  • includePlugins
  • insertEmptyStrRow
  • ipMatch
  • isAlphanumeric
  • isArchive
  • isArtInMultipleUse
  • isFunctionDisabled
  • isGroup
  • isIPv4
  • isRunningFromWeb
  • isStartArticle
  • isUtf8
  • isValidMail
  • langActivateDeactivateLanguage
  • langDeleteLanguage
  • langEditLanguage
  • langGetTextDirection
  • langNewLanguage
  • langRenameLanguage
  • layDeleteLayout
  • layEditLayout
  • machineReadableSize
  • mailLogBulkEditingFunctions
  • mailLogDecodeAddresses
  • markSubMenuItem
  • mask
  • modDeleteModule
  • modEditModule
  • phpInfoToHtml
  • plugin_include
  • prCreateURLNameLocationString
  • prDeleteCacheFileContent
  • prGetCacheFileContent
  • prResolvePathViaCategoryNames
  • prResolvePathViaURLNames
  • prWriteCacheFileContent
  • putFileContents
  • recursiveCopy
  • removeFileInformation
  • renderBackendBreadcrumb
  • renderLabel
  • renderSelectProperty
  • renderTextProperty
  • saveGroupRights
  • saveRights
  • scanDirectory
  • scanPlugins
  • sendEncodingHeader
  • set_magic_quotes_gpc
  • setArtspecDefault
  • setArtspecOnline
  • setSystemProperty
  • showTree
  • startTiming
  • statCreateLocationString
  • statDisplayTopChooser
  • statDisplayYearlyTopChooser
  • statGetAvailableMonths
  • statGetAvailableYears
  • statResetStatistic
  • statsArchive
  • statsDisplayInfo
  • statsOverviewAll
  • statsOverviewTop
  • statsOverviewTopYear
  • statsOverviewYear
  • strAssignTemplate
  • strBuildSqlValues
  • strCheckTreeForErrors
  • strCopyCategory
  • strCopyTree
  • strDeeperCategoriesArray
  • strDeleteCategory
  • strHasArticles
  • strHasStartArticle
  • strMakePublic
  • strMakeVisible
  • strMoveCatTargetallowed
  • strMoveDownCategory
  • strMoveSubtree
  • strMoveUpCategory
  • strNewCategory
  • strNewTree
  • strNextBackwards
  • strNextDeeper
  • strNextDeeperAll
  • strNextPost
  • strOrderedPostTreeList
  • strRemakeTreeTable
  • strRenameCategory
  • strRenameCategoryAlias
  • strSortPrePost
  • strSyncCategory
  • systemHavePerm
  • tplAutoFillModules
  • tplBrowseLayoutForContainers
  • tplcfgDuplicate
  • tplDeleteTemplate
  • tplDuplicateTemplate
  • tplEditTemplate
  • tplGetContainerDefault
  • tplGetContainerMode
  • tplGetContainerName
  • tplGetContainerNumbersInLayout
  • tplGetContainerTypes
  • tplGetInUsedData
  • tplIsTemplateInUse
  • tplPreparseLayout
  • tplProcessSendContainerConfiguration
  • updateClientCache
  • updateFileInformation
  • uplCreateFriendlyName
  • uplDirectoryListRecursive
  • uplGetDirectoriesToExclude
  • uplGetFileExtension
  • uplGetFileIcon
  • uplGetFileTypeDescription
  • uplGetThumbnail
  • uplHasFiles
  • uplHasSubdirs
  • uplmkdir
  • uplRecursiveDBDirectoryList
  • uplRecursiveDirectoryList
  • uplRenameDirectory
  • uplSearch
  • uplSyncDirectory
  • uplSyncDirectoryDBFS
  • Overview
  • Package
  • Function
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: /**
  4:  * This file contains CONTENIDO Image API functions.
  5:  *
  6:  * If you are planning to add a function, please make sure that:
  7:  * 1.) The function is in the correct place
  8:  * 2.) The function is documented
  9:  * 3.) The function makes sense and is generally usable
 10:  *
 11:  * @package Core
 12:  * @subpackage Backend
 13:  * @author Timo Hummel
 14:  * @copyright four for business AG <www.4fb.de>
 15:  * @license http://www.contenido.org/license/LIZENZ.txt
 16:  * @link http://www.4fb.de
 17:  * @link http://www.contenido.org
 18:  */
 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
 26:  *         Path to upload image
 27:  * @param int $iMaxX
 28:  *         Maximum image x size
 29:  * @param int $iMaxY
 30:  *         Maximum image y size
 31:  * @param bool $bCrop
 32:  *         Flag to crop image
 33:  * @param bool $bExpand
 34:  *         Flag to expand image
 35:  * @return string
 36:  *         Path to the resulting image
 37:  */
 38: function cApiImgScaleGetMD5CacheFile($sImg, $iMaxX, $iMaxY, $bCrop, $bExpand) {
 39:     if (!cFileHandler::exists($sImg)) {
 40:         return false;
 41:     }
 42: 
 43:     $iFilesize = filesize($sImg);
 44: 
 45:     if (function_exists('md5_file')) {
 46:         $sMD5 = md5(implode('', array(
 47:             $sImg,
 48:             md5_file($sImg),
 49:             $iFilesize,
 50:             $iMaxX,
 51:             $iMaxY,
 52:             $bCrop,
 53:             $bExpand
 54:         )));
 55:     } else {
 56:         $sMD5 = md5(implode('', array(
 57:             $sImg,
 58:             $iFilesize,
 59:             $iMaxX,
 60:             $iMaxY,
 61:             $bCrop,
 62:             $bExpand
 63:         )));
 64:     }
 65: 
 66:     return $sMD5;
 67: }
 68: 
 69: /**
 70:  * Scales (or crops) an image.
 71:  * If scaling, the aspect ratio is maintained.
 72:  *
 73:  * Returns the path to the scaled temporary image.
 74:  *
 75:  * Note that this function does some very poor caching;
 76:  * it calculates an md5 hash out of the image plus the
 77:  * maximum X and Y sizes, and uses that as the file name.
 78:  * If the file is older than 10 minutes it will be regenerated.
 79:  *
 80:  * @param string $img
 81:  *         The path to the image (relative to the frontend)
 82:  * @param int $maxX
 83:  *         The maximum size in x-direction
 84:  * @param int $maxY
 85:  *         The maximum size in y-direction
 86:  * @param bool $crop [optional]
 87:  *         If true, the image is cropped and not scaled.
 88:  * @param bool $expand [optional]
 89:  *         If true, the image is expanded (e.g. really scaled).
 90:  *         If false, the image will only be made smaller.
 91:  * @param int $cacheTime [optional]
 92:  *         The number of minutes to cache the image, use 0 for unlimited
 93:  * @param int $quality [optional]
 94:  *         The quality of the output file
 95:  * @param bool $keepType [optional]
 96:  *         If true and a png file is source, output file is also png
 97:  * @return string
 98:  *         url to the resulting image (http://...
 99:  */
100: function cApiImgScaleLQ($img, $maxX, $maxY, $crop = false, $expand = false, $cacheTime = 10, $quality = 0, $keepType = false) {
101:     global $cfgClient, $client, $cfg;
102: 
103:     if (!cFileHandler::exists($img)) {
104:         return false;
105:     }
106: 
107:     if ($quality == 0 && isset($cfg['images']['image_quality']['compression_rate'])) {
108:         $quality = (int) $cfg['images']['image_quality']['compression_rate'];
109:     }
110: 
111:     if ($quality == 0) {
112:         $quality = 75;
113:     }
114: 
115:     $filename = $img;
116:     $maxX = (int) $maxX;
117:     $maxY = (int) $maxY;
118:     $cacheTime = (int) $cacheTime;
119:     $quality = (int) $quality;
120: 
121:     if ($quality <= 0 || $quality > 100) {
122:         $quality = 75;
123:     }
124: 
125:     $frontendURL = cRegistry::getFrontendUrl();
126:     $filetype = cFileHandler::getExtension($filename);
127:     $md5 = cApiImgScaleGetMD5CacheFile($img, $maxX, $maxY, $crop, $expand);
128:     $cfileName = cApiImageGetCacheFileName($md5, $filetype, $keepType);
129:     $cacheFile = $cfgClient[$client]['cache']['path'] . $cfileName;
130:     $webFile = $frontendURL . 'cache/' . $cfileName;
131: 
132:     if (cApiImageCheckCachedImageValidity($cacheFile, $cacheTime)) {
133:         return $webFile;
134:     }
135: 
136:     // Get out which file we have
137:     switch (strtolower($filetype)) {
138:         case 'gif':
139:             $function = 'imagecreatefromgif';
140:             break;
141:         case 'png':
142:             $function = 'imagecreatefrompng';
143:             break;
144:         case 'jpg':
145:             $function = 'imagecreatefromjpeg';
146:             break;
147:         case 'jpeg':
148:             $function = 'imagecreatefromjpeg';
149:             break;
150:         default:
151:             return false;
152:     }
153: 
154:     if (function_exists($function)) {
155:         $imageHandle = @$function($filename);
156:     }
157: 
158:     // If we can't open the image, return false
159:     if (!$imageHandle) {
160:         return false;
161:     }
162: 
163:     $x = imagesx($imageHandle);
164:     $y = imagesy($imageHandle);
165: 
166:     list($targetX, $targetY) = cApiImageGetTargetDimensions($x, $y, $maxX, $maxY, $expand);
167: 
168:     // Create the target image with the target size, resize it afterwards.
169:     if ($crop) {
170:         // Create the target image with the max size, crop it afterwards.
171:         $targetImage = imagecreate($maxX, $maxY);
172:         imagecopy($targetImage, $imageHandle, 0, 0, 0, 0, $maxX, $maxY);
173:     } else {
174:         // Create the target image with the target size, resize it afterwards.
175:         $targetImage = imagecreate($targetX, $targetY);
176:         imagecopyresized($targetImage, $imageHandle, 0, 0, 0, 0, $targetX, $targetY, $x, $y);
177:     }
178: 
179:     // Output the file
180:     if ($keepType) {
181:         switch (strtolower($filetype)) {
182:             case 'png':
183:                 // no quality option available
184:                 imagepng($targetImage, $cacheFile);
185:                 break;
186:             case 'gif':
187:                 // no quality option available
188:                 imagegif($targetImage, $cacheFile);
189:                 break;
190:             default:
191:                 imagejpeg($targetImage, $cacheFile, $quality);
192:         }
193:     } else {
194:         imagejpeg($targetImage, $cacheFile, $quality);
195:     }
196: 
197:     return ($webFile);
198: }
199: 
200: /**
201:  * Scales (or crops) an image in high quality.
202:  * If scaling, the aspect ratio is maintained.
203:  *
204:  * Note: GDLib 2.x is required!
205:  * Note: Image cropping calculates center of the image!
206:  *
207:  * Returns the path to the scaled temporary image.
208:  *
209:  * Note that this function does some very poor caching;
210:  * it calculates an md5 hash out of the image plus the
211:  * maximum X and Y sizes, and uses that as the file name.
212:  * If the file is older than the specified cache time, regenerate it.
213:  *
214:  * @param string $img
215:  *         The path to the image (relative to the frontend)
216:  * @param int $maxX
217:  *         The maximum size in x-direction
218:  * @param int $maxY
219:  *         The maximum size in y-direction
220:  * @param bool $crop [optional]
221:  *         If true, the image is cropped and not scaled.
222:  * @param bool $expand [optional]
223:  *         If true, the image is expanded (e.g. really scaled).
224:  *         If false, the image will only be made smaller.
225:  * @param int $cacheTime [optional]
226:  *         The number of minutes to cache the image, use 0 for unlimited
227:  * @param int $quality [optional]
228:  *         The quality of the output file
229:  * @param bool $keepType [optional]
230:  *         If true and a png file is source, output file is also png
231:  * @return string
232:  *         Url to the resulting image (http://...)
233:  */
234: function cApiImgScaleHQ($img, $maxX, $maxY, $crop = false, $expand = false, $cacheTime = 10, $quality = 0, $keepType = true) {
235:     global $cfgClient, $client, $cfg;
236: 
237:     if (!cFileHandler::exists($img)) {
238:         return false;
239:     }
240: 
241:     if ($quality == 0 && isset($cfg['images']['image_quality']['compression_rate'])) {
242:         $quality = (int) $cfg['images']['image_quality']['compression_rate'];
243:     }
244: 
245:     if ($quality == 0) {
246:         $quality = 75;
247:     }
248: 
249:     $filename = $img;
250:     $maxX = (int) $maxX;
251:     $maxY = (int) $maxY;
252:     $cacheTime = (int) $cacheTime;
253:     $quality = (int) $quality;
254: 
255:     if ($quality <= 0 || $quality > 100) {
256:         $quality = 75;
257:     }
258: 
259:     $frontendURL = cRegistry::getFrontendUrl();
260:     $filetype = cFileHandler::getExtension($filename);
261:     $md5 = cApiImgScaleGetMD5CacheFile($img, $maxX, $maxY, $crop, $expand);
262:     $cfileName = cApiImageGetCacheFileName($md5, $filetype, $keepType);
263:     $cacheFile = $cfgClient[$client]['cache']['path'] . $cfileName;
264:     $webFile = $frontendURL . 'cache/' . $cfileName;
265: 
266:     if (cApiImageCheckCachedImageValidity($cacheFile, $cacheTime)) {
267:         return $webFile;
268:     }
269: 
270:     // Get out which file we have
271:     switch (strtolower($filetype)) {
272:         case 'gif':
273:             $function = 'imagecreatefromgif';
274:             break;
275:         case 'png':
276:             $function = 'imagecreatefrompng';
277:             break;
278:         case 'jpg':
279:             $function = 'imagecreatefromjpeg';
280:             break;
281:         case 'jpeg':
282:             $function = 'imagecreatefromjpeg';
283:             break;
284:         default:
285:             return false;
286:     }
287: 
288:     if (function_exists($function)) {
289:         $imageHandle = @$function($filename);
290:     }
291: 
292:     // If we can't open the image, return false
293:     if (!$imageHandle) {
294:         return false;
295:     }
296: 
297:     $x = imagesx($imageHandle);
298:     $y = imagesy($imageHandle);
299: 
300:     list($targetX, $targetY) = cApiImageGetTargetDimensions($x, $y, $maxX, $maxY, $expand);
301: 
302:     // Create the target image with the target size, resize it afterwards.
303:     if ($crop) {
304:         // Create the target image with the max size, crop it afterwards.
305:         $targetImage = imagecreatetruecolor($maxX, $maxY);
306:         // calculate canter of the image
307:         $srcX = ($x - $maxX) / 2;
308:         $srcY = ($y - $maxY) / 2;
309: 
310:         // Preserve transparency
311:         if (strtolower($filetype) == 'gif' || strtolower($filetype) == 'png') {
312:             imagecolortransparent($targetImage, imagecolorallocatealpha($targetImage, 0, 0, 0, 127));
313:             imagealphablending($targetImage, false);
314:             imagesavealpha($targetImage, true);
315:         }
316: 
317:         // crop image from center
318:         imagecopy($targetImage, $imageHandle, 0, 0, $srcX, $srcY, $maxX, $maxY);
319:     } else {
320:         // Create the target image with the target size, resize it afterwards.
321:         $targetImage = imagecreatetruecolor($targetX, $targetY);
322: 
323:         // Preserve transparency
324:         if (strtolower($filetype) == 'gif' || strtolower($filetype) == 'png') {
325:             imagecolortransparent($targetImage, imagecolorallocatealpha($targetImage, 0, 0, 0, 127));
326:             imagealphablending($targetImage, false);
327:             imagesavealpha($targetImage, true);
328:         }
329: 
330:         imagecopyresampled($targetImage, $imageHandle, 0, 0, 0, 0, $targetX, $targetY, $x, $y);
331:     }
332: 
333:     // Output the file
334:     if ($keepType) {
335:         switch (strtolower($filetype)) {
336:             case 'png':
337:                 // no quality option available
338:                 imagepng($targetImage, $cacheFile);
339:                 break;
340:             case 'gif':
341:                 imagegif($targetImage, $cacheFile);
342:                 break;
343:             default:
344:                 imagejpeg($targetImage, $cacheFile, $quality);
345:         }
346:     } else {
347:         imagejpeg($targetImage, $cacheFile, $quality);
348:     }
349: 
350:     return $webFile;
351: }
352: 
353: /**
354:  * Scales (or crops) an image using ImageMagick.
355:  * If scaling, the aspect ratio is maintained.
356:  *
357:  * Note: ImageMagick is required!
358:  *
359:  * Returns the path to the scaled temporary image.
360:  *
361:  * Note that this function does some very poor caching;
362:  * it calculates an md5 hash out of the image plus the
363:  * maximum X and Y sizes, and uses that as the file name.
364:  * If the file is older than the specified cache time, regenerate it.
365:  *
366:  * @param string $img
367:  *         The path to the image (relative to the frontend)
368:  * @param int $maxX
369:  *         The maximum size in x-direction
370:  * @param int $maxY
371:  *         The maximum size in y-direction
372:  * @param bool $crop [optional]
373:  *         If true, the image is cropped and not scaled.
374:  * @param bool $expand [optional]
375:  *         If true, the image is expanded (e.g. really scaled).
376:  *         If false, the image will only be made smaller.
377:  * @param int $cacheTime [optional]
378:  *         The number of minutes to cache the image, use 0 for unlimited
379:  * @param int $quality [optional]
380:  *         The quality of the output file
381:  * @param bool $keepType [optional]
382:  *         If true and a png file is source, output file is also png
383:  * @return string
384:  *         Url to the resulting image (http://...)
385:  */
386: function cApiImgScaleImageMagick($img, $maxX, $maxY, $crop = false, $expand = false, $cacheTime = 10, $quality = 0, $keepType = false) {
387:     global $cfg, $cfgClient, $client;
388: 
389:     if (!cFileHandler::exists($img)) {
390:         return false;
391:     } elseif (isFunctionDisabled('escapeshellarg') || isFunctionDisabled('exec')) {
392:         return false;
393:     }
394: 
395:     if ($quality == 0 && isset($cfg['images']['image_quality']['compression_rate'])) {
396:         $quality = (int) $cfg['images']['image_quality']['compression_rate'];
397:     }
398: 
399:     if ($quality == 0) {
400:         $quality = 75;
401:     }
402: 
403:     $filename = $img;
404:     $maxX = (int) $maxX;
405:     $maxY = (int) $maxY;
406:     $cacheTime = (int) $cacheTime;
407:     $quality = (int) $quality;
408: 
409:     if ($quality <= 0 || $quality > 100) {
410:         $quality = 75;
411:     }
412: 
413:     $frontendURL = cRegistry::getFrontendUrl();
414:     $filetype = cFileHandler::getExtension($filename);
415:     $md5 = cApiImgScaleGetMD5CacheFile($img, $maxX, $maxY, $crop, $expand);
416:     $cfileName = cApiImageGetCacheFileName($md5, $filetype, $keepType);
417:     $cacheFile = $cfgClient[$client]['cache']['path'] . $cfileName;
418:     $webFile = $frontendURL . 'cache/' . $cfileName;
419: 
420:     if (cApiImageCheckCachedImageValidity($cacheFile, $cacheTime)) {
421:         return $webFile;
422:     }
423: 
424:     list($x, $y) = @getimagesize($filename);
425:     if ($x == 0 || $y == 0) {
426:         return false;
427:     }
428: 
429:     list($targetX, $targetY) = cApiImageGetTargetDimensions($x, $y, $maxX, $maxY, $expand);
430: 
431:     // If is animated gif resize first frame
432:     if ($filetype == 'gif') {
433:         if (cApiImageIsAnimGif($filename)) {
434:             $filename .= '[0]';
435:         }
436:     }
437: 
438:     // Try to execute convert
439:     $output = array();
440:     $retVal = 0;
441:     $program = escapeshellarg($cfg['images']['image_magick']['path'] . 'convert');
442:     $source = escapeshellarg($filename);
443:     $destination = escapeshellarg($cacheFile);
444:     if ($crop) {
445:         $cmd = "'{$program}' -gravity center -quality {$quality} -crop {$maxX}x{$maxY}+1+1 '{$source}' '{$destination}'";
446:     } else {
447:         $cmd = "'{$program}' -quality {$quality} -geometry {$targetX}x{$targetY} '{$source}' '{$destination}'";
448:     }
449: 
450:     exec($cmd, $output, $retVal);
451: 
452:     if (!cFileHandler::exists($cacheFile)) {
453:         return false;
454:     } else {
455:         return $webFile;
456:     }
457: }
458: 
459: /**
460:  * Check if gif is animated using ImageMagicks "identify".
461:  *
462:  * If the PHP functions "escapeshellarg" or "exec" are not available
463:  * false will be returned.
464:  *
465:  * If ImageMagick is not available false will be returned.
466:  *
467:  * @param string $sFile
468:  *         file path
469:  * @return bool
470:  *         True (gif is animated)/ false (single frame gif)
471:  */
472: function cApiImageIsAnimGif($sFile) {
473:     global $cfg;
474: 
475:     // check if functions escapeshellarg or exec are disabled
476:     if (isFunctionDisabled('escapeshellarg') || isFunctionDisabled('exec')) {
477:         return false;
478:     }
479: 
480:     // check if ImageMagick is available
481:     if ('im' != cApiImageCheckImageEditingPosibility()) {
482:         return false;
483:     }
484: 
485:     $output = array();
486:     $retval = 0;
487:     $program = escapeshellarg($cfg['images']['image_magick']['path'] . 'identify');
488:     $source = escapeshellarg($sFile);
489: 
490:     exec("'{$program}' '{$source}'", $output, $retval);
491: 
492:     if (count($output) == 1) {
493:         return false;
494:     }
495: 
496:     return true;
497: }
498: 
499: /**
500:  * Scales (or crops) an image.
501:  * If scaling, the aspect ratio is maintained.
502:  *
503:  * This function chooses the best method to scale, depending on the system
504:  * environment and/or the parameters.
505:  *
506:  * Returns the path to the scaled temporary image.
507:  *
508:  * Note that this function does some very poor caching;
509:  * it calculates an md5 hash out of the image plus the
510:  * maximum X and Y sizes, and uses that as the file name.
511:  * If the file is older than 10 minutes, regenerate it.
512:  *
513:  * @param string $img
514:  *         The path to the image (relative to the frontend)
515:  * @param int $maxX
516:  *         The maximum size in x-direction
517:  * @param int $maxY
518:  *         The maximum size in y-direction
519:  * @param bool $crop [optional]
520:  *         If true, the image is cropped and not scaled.
521:  * @param bool $expand [optional]
522:  *         If true, the image is expanded (e.g. really scaled).
523:  *         If false, the image will only be made smaller.
524:  * @param int $cacheTime [optional]
525:  *         The number of minutes to cache the image, use 0 for unlimited
526:  * @param bool $wantHQ [optional]
527:  *         If true, try to force high quality mode
528:  * @param int $quality [optional]
529:  *         The quality of the output file
530:  * @param bool $keepType [optional]
531:  *         If true and a png file is source, output file is also png
532:  * @return string
533:  *         Path to the resulting image
534:  */
535: function cApiImgScale($img, $maxX, $maxY, $crop = false, $expand = false, $cacheTime = 10, $wantHQ = false, $quality = 0, $keepType = true) {
536:     global $client, $cfgClient, $cfg;
537: 
538:     $deleteAfter = false;
539: 
540:     if ($quality == 0 && isset($cfg['images']['image_quality']['compression_rate'])) {
541:         $quality = (int) $cfg['images']['image_quality']['compression_rate'];
542:     }
543: 
544:     if ($quality == 0) {
545:         $quality = 75;
546:     }
547: 
548:     $sRelativeImg = str_replace($cfgClient[$client]['upl']['path'], '', $img);
549:     if (cApiDbfs::isDbfs($sRelativeImg)) {
550:         // This check should be faster than a file existance check
551:         $dbfs = new cApiDbfsCollection();
552: 
553:         $file = basename($sRelativeImg);
554: 
555:         $dbfs->writeToFile($sRelativeImg, $cfgClient[$client]['cache']['path'] . $file);
556: 
557:         $img = $cfgClient[$client]['cache']['path'] . $file;
558:         $deleteAfter = true;
559:     } else if (!cFileHandler::exists($img)) {
560:         // Try with upload string
561:         if (cFileHandler::exists($cfgClient[$client]['upl']['path'] . $img) && !is_dir($cfgClient[$client]['upl']['path'] . $img)) {
562:             $img = $cfgClient[$client]['upl']['path'] . $img;
563:         } else {
564:             // No, it's neither in the upload directory nor in the dbfs. return.
565:             return false;
566:         }
567:     }
568: 
569:     $filename = $img;
570:     $filetype = substr($filename, strlen($filename) - 4, 4);
571: 
572:     $mxdAvImgEditingPosibility = cApiImageCheckImageEditingPosibility();
573:     switch ($mxdAvImgEditingPosibility) {
574:         case '1': // gd1
575:             $method = 'gd1';
576:             if (!function_exists('imagecreatefromgif') && $filetype == '.gif') {
577:                 $method = 'failure';
578:             }
579:             break;
580:         case '2': // gd2
581:             $method = 'gd2';
582:             if (!function_exists('imagecreatefromgif') && $filetype == '.gif') {
583:                 $method = 'failure';
584:             }
585:             break;
586:         case 'im': // imagemagick
587:             $method = 'im';
588:             break;
589:         case '0':
590:             $method = 'failure';
591:             break;
592:         default:
593:             $method = 'failure';
594:             break;
595:     }
596: 
597:     switch ($method) {
598:         case 'gd1':
599:             $return = cApiImgScaleLQ($img, $maxX, $maxY, $crop, $expand, $cacheTime, $quality, $keepType);
600:             break;
601:         case 'gd2':
602:             $return = cApiImgScaleHQ($img, $maxX, $maxY, $crop, $expand, $cacheTime, $quality, $keepType);
603:             break;
604:         case 'im':
605:             $return = cApiImgScaleImageMagick($img, $maxX, $maxY, $crop, $expand, $cacheTime, $quality, $keepType);
606:             break;
607:         case 'failure':
608:             $frontendURL = cRegistry::getFrontendUrl();
609:             $return = str_replace(cRegistry::getFrontendPath(), $frontendURL, $img);
610:             break;
611:     }
612: 
613:     if ($deleteAfter == true) {
614:         unlink($img);
615:     }
616: 
617:     return $return;
618: }
619: 
620: /**
621:  * Check possible image editing functionality.
622:  *
623:  * @return mixed
624:  *         Information about installed image editing extensions/tools
625:  *         <pre>
626:  *         - 'im' ImageMagick is available and usage is enabled
627:  *         - '2' GD library version 2 is available
628:  *         - '1' GD library version 1 is available
629:  *         - '0' Nothing could detected
630:  *         </pre>
631:  */
632: function cApiImageCheckImageEditingPosibility() {
633:     global $cfg;
634: 
635:     if ($cfg['images']['image_magick']['use']) {
636:         if (cApiIsImageMagickAvailable()) {
637:             return 'im';
638:         }
639:     }
640: 
641:     if (!extension_loaded('gd')) {
642:         return '0';
643:     }
644: 
645:     if (function_exists('gd_info')) {
646:         $sGDVersion = '';
647:         $aGDInformations = gd_info();
648:         if (preg_match('#([0-9\.])+#', $aGDInformations['GD Version'], $sGDVersion)) {
649:             if ($sGDVersion[0] >= '2') {
650:                 return '2';
651:             }
652:             return '1';
653:         }
654:         return '1';
655:     }
656:     return '1';
657: }
658: 
659: /**
660:  * Returns new calculated dimensions of a target image.
661:  *
662:  * @param int $x
663:  * @param int $y
664:  * @param int $maxX
665:  * @param int $maxY
666:  * @param bool $expand
667:  * @return array
668:  *         Index 0 is target X and index 1 is target Y
669:  */
670: function cApiImageGetTargetDimensions($x, $y, $maxX, $maxY, $expand) {
671:     if (($maxX / $x) < ($maxY / $y)) {
672:         $targetY = $y * ($maxX / $x);
673:         $targetX = round($maxX);
674: 
675:         // Force wished height
676:         if ($targetY < $maxY) {
677:             $targetY = ceil($targetY);
678:         } else {
679:             $targetY = floor($targetY);
680:         }
681:     } else {
682:         $targetX = $x * ($maxY / $y);
683:         $targetY = round($maxY);
684: 
685:         // Force wished width
686:         if ($targetX < $maxX) {
687:             $targetX = ceil($targetX);
688:         } else {
689:             $targetX = floor($targetX);
690:         }
691:     }
692: 
693:     if ($expand == false && (($targetX > $x) || ($targetY > $y))) {
694:         $targetX = $x;
695:         $targetY = $y;
696:     }
697: 
698:     $targetX = ($targetX != 0) ? $targetX : 1;
699:     $targetY = ($targetY != 0) ? $targetY : 1;
700: 
701:     return array(
702:         $targetX,
703:         $targetY
704:     );
705: }
706: 
707: /**
708:  * Returns cache file name.
709:  *
710:  * @param string $md5
711:  * @param string $filetype
712:  * @param bool $keepType
713:  * @return string
714:  */
715: function cApiImageGetCacheFileName($md5, $filetype, $keepType) {
716:     // Create the target file names for web and server
717: 
718:     // Should we keep the file type?
719:     if ($keepType) {
720: 
721:         // Just using switch if someone likes to add other types
722:         switch (strtolower($filetype)) {
723:             case 'png':
724:                 $fileName = $md5 . '.png';
725:                 break;
726:             case 'gif':
727:                 $fileName = $md5 . '.gif';
728:                 break;
729:             default:
730:                 $fileName = $md5 . '.jpg';
731:         }
732: 
733:     } else {
734: 
735:         // No... use .jpg
736:         $fileName = $md5 . '.jpg';
737: 
738:     }
739: 
740:     return $fileName;
741: }
742: 
743: /**
744:  * Validates cache version of a image.
745:  *
746:  * @param string $cacheFile
747:  * @param int $cacheTime
748:  * @return bool
749:  *         Returns true, if cache file exists and7or is still valid or false
750:  */
751: function cApiImageCheckCachedImageValidity($cacheFile, $cacheTime) {
752: 
753:     // Check if the file exists. If it does, check if the file is valid.
754:     if (cFileHandler::exists($cacheFile)) {
755: 
756:         if ($cacheTime == 0) {
757: 
758:             // Do not check expiration date
759:             return true;
760: 
761:         } else if (!function_exists('md5_file')) {
762: 
763:             // TODO: Explain why this is still needed ... or remove it
764:             if ((filemtime($cacheFile) + (60 * $cacheTime)) < time()) {
765:                 // Cache time expired, unlink the file
766:                 unlink($cacheFile);
767:             } else {
768:                 // Return the web file name
769:                 return true;
770:             }
771: 
772:         } else {
773: 
774:             return true;
775:         }
776: 
777:     }
778: 
779:     return false;
780: }
781: 
782: /**
783:  * Checks if ImageMagick is available.
784:  *
785:  * This info will be cached after being detected once.
786:  *
787:  * @return bool
788:  *         true if ImageMagick is available
789:  */
790: function cApiIsImageMagickAvailable() {
791:     global $cfg;
792:     static $imagemagickAvailable = NULL;
793: 
794:     // if the check has already been executed, just return the result
795:     if (is_bool($imagemagickAvailable)) {
796:         return $imagemagickAvailable;
797:     }
798: 
799:     // check, if escapeshellarg or exec function is disabled, we need both
800:     if (isFunctionDisabled('escapeshellarg') || isFunctionDisabled('exec')) {
801:         $imagemagickAvailable = false;
802:         return $imagemagickAvailable;
803:     }
804: 
805:     // otherwise execute the IM check
806:     $program = escapeshellarg($cfg['images']['image_magick']['path'] . 'convert');
807:     $output = array();
808:     $retval = 0;
809:     @exec("'{$program}' -version", $output, $retval);
810: 
811:     // exec is probably disabled, so we assume IM to be unavailable
812:     // otherwise output contains the output of the command "convert version"
813:     // if IM is available, it contains the string "ImageMagick"
814:     if (!is_array($output) || count($output) == 0) {
815:         $imagemagickAvailable = false;
816:     } else if (false === strpos($output[0], 'ImageMagick')) {
817:         $imagemagickAvailable = false;
818:     } else {
819:         $imagemagickAvailable = true;
820:     }
821: 
822:     return $imagemagickAvailable;
823: }
824: 
CMS CONTENIDO 4.9.11 API documentation generated by ApiGen 2.8.0