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
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • PHP
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • 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
  • cApiImageCheckImageEditingPossibility
  • 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
  • conGetUniqueArticleUrlname
  • conGetUsedModules
  • conHtmlentities
  • conHtmlEntityDecode
  • conHtmlSpecialChars
  • conIsArticleUrlnameUnique
  • conIsLocked
  • conLock
  • conLockBulkEditing
  • conMakeArticleIndex
  • conMakeCatOnline
  • conMakeInlineScript
  • conMakeOnline
  • conMakeOnlineBulkEditing
  • conMakePublic
  • conMakeStart
  • conMoveArticles
  • conPhp54Check
  • conRemoveOldCategoryArticle
  • conSaveContentEntry
  • conSetCodeFlag
  • conSetCodeFlagBulkEditing
  • conSetMetaValue
  • conSetStartArticle
  • consoleLog
  • 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
  • getFileInformation
  • getFileType
  • getGroupOrUserName
  • getIDForArea
  • getJsHelpContext
  • getLanguageNamesByClient
  • getLanguagesByClient
  • getmicrotime
  • getNamedFrame
  • getParentAreaId
  • getRightsList
  • 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
  • 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
  • strCheckAlias
  • 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:     $cacheFileName = cApiImageGetCacheFileName($md5, $fileType, $keepType);
129:     $cacheFile = $cfgClient[$client]['cache']['path'] . $cacheFileName;
130:     $webFile = $frontendURL . 'cache/' . $cacheFileName;
131: 
132:     if (cApiImageCheckCachedImageValidity($cacheFile, $cacheTime)) {
133:         return $webFile;
134:     }
135: 
136:     // Get out which file we have
137:     switch (cString::toLowerCase($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:     $imageHandle = (function_exists($function)) ? @$function($fileName) : null;
155: 
156:     // If we can't open the image, return false
157:     if (!$imageHandle) {
158:         return false;
159:     }
160: 
161:     $x = imagesx($imageHandle);
162:     $y = imagesy($imageHandle);
163: 
164:     list($targetX, $targetY) = cApiImageGetTargetDimensions($x, $y, $maxX, $maxY, $expand);
165: 
166:     // Create the target image with the target size, resize it afterwards.
167:     if ($crop) {
168:         // Create the target image with the max size, crop it afterwards.
169:         $targetImage = imagecreate($maxX, $maxY);
170:         imagecopy($targetImage, $imageHandle, 0, 0, 0, 0, $maxX, $maxY);
171:     } else {
172:         // Create the target image with the target size, resize it afterwards.
173:         $targetImage = imagecreate($targetX, $targetY);
174:         imagecopyresized($targetImage, $imageHandle, 0, 0, 0, 0, $targetX, $targetY, $x, $y);
175:     }
176: 
177:     // Output the file
178:     if ($keepType) {
179:         switch (cString::toLowerCase($fileType)) {
180:             case 'png':
181:                 // no quality option available
182:                 imagepng($targetImage, $cacheFile);
183:                 break;
184:             case 'gif':
185:                 // no quality option available
186:                 imagegif($targetImage, $cacheFile);
187:                 break;
188:             default:
189:                 imagejpeg($targetImage, $cacheFile, $quality);
190:         }
191:     } else {
192:         imagejpeg($targetImage, $cacheFile, $quality);
193:     }
194: 
195:     return ($webFile);
196: }
197: 
198: /**
199:  * Scales (or crops) an image in high quality.
200:  * If scaling, the aspect ratio is maintained.
201:  *
202:  * Note: GDLib 2.x is required!
203:  * Note: Image cropping calculates center of the image!
204:  *
205:  * Returns the path to the scaled temporary image.
206:  *
207:  * Note that this function does some very poor caching;
208:  * it calculates an md5 hash out of the image plus the
209:  * maximum X and Y sizes, and uses that as the file name.
210:  * If the file is older than the specified cache time, regenerate it.
211:  *
212:  * @param string $img
213:  *         The path to the image (relative to the frontend)
214:  * @param int $maxX
215:  *         The maximum size in x-direction
216:  * @param int $maxY
217:  *         The maximum size in y-direction
218:  * @param bool $crop [optional]
219:  *         If true, the image is cropped and not scaled.
220:  * @param bool $expand [optional]
221:  *         If true, the image is expanded (e.g. really scaled).
222:  *         If false, the image will only be made smaller.
223:  * @param int $cacheTime [optional]
224:  *         The number of minutes to cache the image, use 0 for unlimited
225:  * @param int $quality [optional]
226:  *         The quality of the output file
227:  * @param bool $keepType [optional]
228:  *         If true and a png file is source, output file is also png
229:  * @return string
230:  *         Url to the resulting image (http://...)
231:  */
232: function cApiImgScaleHQ($img, $maxX, $maxY, $crop = false, $expand = false, $cacheTime = 10, $quality = 0, $keepType = true) {
233:     global $cfgClient, $client, $cfg;
234: 
235:     if (!cFileHandler::exists($img)) {
236:         return false;
237:     }
238: 
239:     if ($quality == 0 && isset($cfg['images']['image_quality']['compression_rate'])) {
240:         $quality = (int) $cfg['images']['image_quality']['compression_rate'];
241:     }
242: 
243:     if ($quality == 0) {
244:         $quality = 75;
245:     }
246: 
247:     $fileName = $img;
248:     $maxX = (int) $maxX;
249:     $maxY = (int) $maxY;
250:     $cacheTime = (int) $cacheTime;
251:     $quality = (int) $quality;
252: 
253:     if ($quality <= 0 || $quality > 100) {
254:         $quality = 75;
255:     }
256: 
257:     $frontendURL = cRegistry::getFrontendUrl();
258:     $fileType = cFileHandler::getExtension($fileName);
259:     $md5 = cApiImgScaleGetMD5CacheFile($img, $maxX, $maxY, $crop, $expand);
260:     $cacheFileName = cApiImageGetCacheFileName($md5, $fileType, $keepType);
261:     $cacheFile = $cfgClient[$client]['cache']['path'] . $cacheFileName;
262:     $webFile = $frontendURL . 'cache/' . $cacheFileName;
263: 
264:     if (cApiImageCheckCachedImageValidity($cacheFile, $cacheTime)) {
265:         return $webFile;
266:     }
267: 
268:     // Get out which file we have
269:     switch (cString::toLowerCase($fileType)) {
270:         case 'gif':
271:             $function = 'imagecreatefromgif';
272:             break;
273:         case 'png':
274:             $function = 'imagecreatefrompng';
275:             break;
276:         case 'jpg':
277:             $function = 'imagecreatefromjpeg';
278:             break;
279:         case 'jpeg':
280:             $function = 'imagecreatefromjpeg';
281:             break;
282:         default:
283:             return false;
284:     }
285: 
286:     $imageHandle = (function_exists($function)) ? @$function($fileName) : null;
287: 
288:     // If we can't open the image, return false
289:     if (!$imageHandle) {
290:         return false;
291:     }
292: 
293:     $x = imagesx($imageHandle);
294:     $y = imagesy($imageHandle);
295: 
296:     list($targetX, $targetY) = cApiImageGetTargetDimensions($x, $y, $maxX, $maxY, $expand);
297: 
298:     // Create the target image with the target size, resize it afterwards.
299:     if ($crop) {
300:         // Create the target image with the max size, crop it afterwards.
301:         $targetImage = imagecreatetruecolor($maxX, $maxY);
302:         // calculate canter of the image
303:         $srcX = ($x - $maxX) / 2;
304:         $srcY = ($y - $maxY) / 2;
305: 
306:         // Preserve transparency
307:         if (cString::toLowerCase($fileType) == 'gif' || cString::toLowerCase($fileType) == 'png') {
308:             imagecolortransparent($targetImage, imagecolorallocatealpha($targetImage, 0, 0, 0, 127));
309:             imagealphablending($targetImage, false);
310:             imagesavealpha($targetImage, true);
311:         }
312: 
313:         // crop image from center
314:         imagecopy($targetImage, $imageHandle, 0, 0, $srcX, $srcY, $maxX, $maxY);
315:     } else {
316:         // Create the target image with the target size, resize it afterwards.
317:         $targetImage = imagecreatetruecolor($targetX, $targetY);
318: 
319:         // Preserve transparency
320:         if (cString::toLowerCase($fileType) == 'gif' || cString::toLowerCase($fileType) == 'png') {
321:             imagecolortransparent($targetImage, imagecolorallocatealpha($targetImage, 0, 0, 0, 127));
322:             imagealphablending($targetImage, false);
323:             imagesavealpha($targetImage, true);
324:         }
325: 
326:         imagecopyresampled($targetImage, $imageHandle, 0, 0, 0, 0, $targetX, $targetY, $x, $y);
327:     }
328: 
329:     // Output the file
330:     if ($keepType) {
331:         switch (cString::toLowerCase($fileType)) {
332:             case 'png':
333:                 // no quality option available
334:                 imagepng($targetImage, $cacheFile);
335:                 break;
336:             case 'gif':
337:                 imagegif($targetImage, $cacheFile);
338:                 break;
339:             default:
340:                 imagejpeg($targetImage, $cacheFile, $quality);
341:         }
342:     } else {
343:         imagejpeg($targetImage, $cacheFile, $quality);
344:     }
345: 
346:     return $webFile;
347: }
348: 
349: /**
350:  * Scales (or crops) an image using ImageMagick.
351:  * If scaling, the aspect ratio is maintained.
352:  *
353:  * Note: ImageMagick is required!
354:  *
355:  * Returns the path to the scaled temporary image.
356:  *
357:  * Note that this function does some very poor caching;
358:  * it calculates an md5 hash out of the image plus the
359:  * maximum X and Y sizes, and uses that as the file name.
360:  * If the file is older than the specified cache time, regenerate it.
361:  *
362:  * @param string $img
363:  *         The path to the image (relative to the frontend)
364:  * @param int $maxX
365:  *         The maximum size in x-direction
366:  * @param int $maxY
367:  *         The maximum size in y-direction
368:  * @param bool $crop [optional]
369:  *         If true, the image is cropped and not scaled.
370:  * @param bool $expand [optional]
371:  *         If true, the image is expanded (e.g. really scaled).
372:  *         If false, the image will only be made smaller.
373:  * @param int $cacheTime [optional]
374:  *         The number of minutes to cache the image, use 0 for unlimited
375:  * @param int $quality [optional]
376:  *         The quality of the output file
377:  * @param bool $keepType [optional]
378:  *         If true and a png file is source, output file is also png
379:  * @return string
380:  *         Url to the resulting image (http://...)
381:  */
382: function cApiImgScaleImageMagick($img, $maxX, $maxY, $crop = false, $expand = false, $cacheTime = 10, $quality = 0, $keepType = false) {
383:     global $cfg, $cfgClient, $client;
384: 
385:     if (!cFileHandler::exists($img)) {
386:         return false;
387:     } elseif (isFunctionDisabled('escapeshellarg') || isFunctionDisabled('exec')) {
388:         return false;
389:     }
390: 
391:     if ($quality == 0 && isset($cfg['images']['image_quality']['compression_rate'])) {
392:         $quality = (int) $cfg['images']['image_quality']['compression_rate'];
393:     }
394: 
395:     if ($quality == 0) {
396:         $quality = 75;
397:     }
398: 
399:     $fileName = $img;
400:     $maxX = (int) $maxX;
401:     $maxY = (int) $maxY;
402:     $cacheTime = (int) $cacheTime;
403:     $quality = (int) $quality;
404: 
405:     if ($quality <= 0 || $quality > 100) {
406:         $quality = 75;
407:     }
408: 
409:     $frontendURL = cRegistry::getFrontendUrl();
410:     $fileType = cFileHandler::getExtension($fileName);
411:     $md5 = cApiImgScaleGetMD5CacheFile($img, $maxX, $maxY, $crop, $expand);
412:     $cacheFileName = cApiImageGetCacheFileName($md5, $fileType, $keepType);
413:     $cacheFile = $cfgClient[$client]['cache']['path'] . $cacheFileName;
414:     $webFile = $frontendURL . 'cache/' . $cacheFileName;
415: 
416:     if (cApiImageCheckCachedImageValidity($cacheFile, $cacheTime)) {
417:         return $webFile;
418:     }
419: 
420:     list($x, $y) = @getimagesize($fileName);
421:     if ($x == 0 || $y == 0) {
422:         return false;
423:     }
424: 
425:     list($targetX, $targetY) = cApiImageGetTargetDimensions($x, $y, $maxX, $maxY, $expand);
426: 
427:     // If is animated gif resize first frame
428:     if ($fileType == 'gif') {
429:         if (cApiImageIsAnimGif($fileName)) {
430:             $fileName .= '[0]';
431:         }
432:     }
433: 
434:     // Try to execute convert
435:     $output = array();
436:     $retVal = 0;
437:     $program = escapeshellarg($cfg['images']['image_magick']['path'] . 'convert');
438:     $source = escapeshellarg($fileName);
439:     $destination = escapeshellarg($cacheFile);
440:     if ($crop) {
441:         $cmd = "'{$program}' -gravity center -quality {$quality} -crop {$maxX}x{$maxY}+1+1 '{$source}' '{$destination}'";
442:     } else {
443:         $cmd = "'{$program}' -quality {$quality} -geometry {$targetX}x{$targetY} '{$source}' '{$destination}'";
444:     }
445: 
446:     exec($cmd, $output, $retVal);
447: 
448:     if (!cFileHandler::exists($cacheFile)) {
449:         return false;
450:     } else {
451:         return $webFile;
452:     }
453: }
454: 
455: /**
456:  * Check if gif is animated using ImageMagicks "identify".
457:  *
458:  * If the PHP functions "escapeshellarg" or "exec" are not available
459:  * false will be returned.
460:  *
461:  * If ImageMagick is not available false will be returned.
462:  *
463:  * @param string $sFile
464:  *         file path
465:  * @return bool
466:  *         True (gif is animated)/ false (single frame gif)
467:  */
468: function cApiImageIsAnimGif($sFile) {
469:     global $cfg;
470: 
471:     // check if functions escapeshellarg or exec are disabled
472:     if (isFunctionDisabled('escapeshellarg') || isFunctionDisabled('exec')) {
473:         return false;
474:     }
475: 
476:     // check if ImageMagick is available
477:     if ('im' != cApiImageCheckImageEditingPossibility()) {
478:         return false;
479:     }
480: 
481:     $output = array();
482:     $retVal = 0;
483:     $program = escapeshellarg($cfg['images']['image_magick']['path'] . 'identify');
484:     $source = escapeshellarg($sFile);
485: 
486:     exec("'{$program}' '{$source}'", $output, $retVal);
487: 
488:     if (count($output) == 1) {
489:         return false;
490:     }
491: 
492:     return true;
493: }
494: 
495: /**
496:  * Scales (or crops) an image.
497:  * If scaling, the aspect ratio is maintained.
498:  *
499:  * This function chooses the best method to scale, depending on the system
500:  * environment and/or the parameters.
501:  *
502:  * Returns the path to the scaled temporary image.
503:  *
504:  * Note that this function does some very poor caching;
505:  * it calculates an md5 hash out of the image plus the
506:  * maximum X and Y sizes, and uses that as the file name.
507:  * If the file is older than 10 minutes, regenerate it.
508:  *
509:  * @param string $img
510:  *                          The path to the image (relative to the frontend)
511:  * @param int    $maxX
512:  *                          The maximum size in x-direction
513:  * @param int    $maxY
514:  *                          The maximum size in y-direction
515:  * @param bool   $crop      [optional]
516:  *                          If true, the image is cropped and not scaled.
517:  * @param bool   $expand    [optional]
518:  *                          If true, the image is expanded (e.g. really scaled).
519:  *                          If false, the image will only be made smaller.
520:  * @param int    $cacheTime [optional]
521:  *                          The number of minutes to cache the image, use 0 for unlimited
522:  * @param bool   $wantHQ    [optional]
523:  *                          If true, try to force high quality mode
524:  *                          @deprecated 4.8.* This is not used anymore.
525:  *                          Configure the quality via following setting:
526:  *                          $cfg['images']['image_quality']['compression_rate']
527:  * @param int    $quality   [optional]
528:  *                          The quality of the output file
529:  * @param bool   $keepType  [optional]
530:  *                          If true and a png file is source, output file is also png
531:  *
532:  * @return string
533:  *         Path to the resulting image
534:  *
535:  * @throws cDbException
536:  * @throws cException
537:  * @throws cInvalidArgumentException
538:  */
539: function cApiImgScale($img, $maxX, $maxY, $crop = false, $expand = false, $cacheTime = 10, $wantHQ = false, $quality = 0, $keepType = true) {
540:     global $client, $cfgClient, $cfg;
541: 
542:     $deleteAfter = false;
543: 
544:     if ($quality == 0 && isset($cfg['images']['image_quality']['compression_rate'])) {
545:         $quality = (int) $cfg['images']['image_quality']['compression_rate'];
546:     }
547: 
548:     if ($quality == 0) {
549:         $quality = 75;
550:     }
551: 
552:     $sRelativeImg = str_replace($cfgClient[$client]['upl']['path'], '', $img);
553:     if (cApiDbfs::isDbfs($sRelativeImg)) {
554:         // This check should be faster than a file existence check
555:         $dbfs = new cApiDbfsCollection();
556: 
557:         $file = basename($sRelativeImg);
558: 
559:         $dbfs->writeToFile($sRelativeImg, $cfgClient[$client]['cache']['path'] . $file);
560: 
561:         $img = $cfgClient[$client]['cache']['path'] . $file;
562:         $deleteAfter = true;
563:     } else if (!cFileHandler::exists($img)) {
564:         // Try with upload string
565:         if (cFileHandler::exists($cfgClient[$client]['upl']['path'] . $img) && !is_dir($cfgClient[$client]['upl']['path'] . $img)) {
566:             $img = $cfgClient[$client]['upl']['path'] . $img;
567:         } else {
568:             // No, it's neither in the upload directory nor in the dbfs. return.
569:             return false;
570:         }
571:     }
572: 
573:     $fileName = $img;
574:     $fileType = cString::getPartOfString($fileName, cString::getStringLength($fileName) - 4, 4);
575: 
576:     $mxdAvImgEditingPossibility = cApiImageCheckImageEditingPossibility();
577:     switch ($mxdAvImgEditingPossibility) {
578:         case '1': // gd1
579:             $method = 'gd1';
580:             if (!function_exists('imagecreatefromgif') && $fileType == '.gif') {
581:                 $method = 'failure';
582:             }
583:             break;
584:         case '2': // gd2
585:             $method = 'gd2';
586:             if (!function_exists('imagecreatefromgif') && $fileType == '.gif') {
587:                 $method = 'failure';
588:             }
589:             break;
590:         case 'im': // ImageMagick
591:             $method = 'im';
592:             break;
593:         case '0':
594:         default:
595:             $method = 'failure';
596:             break;
597:     }
598: 
599:     switch ($method) {
600:         case 'gd1':
601:             $return = cApiImgScaleLQ($img, $maxX, $maxY, $crop, $expand, $cacheTime, $quality, $keepType);
602:             break;
603:         case 'gd2':
604:             $return = cApiImgScaleHQ($img, $maxX, $maxY, $crop, $expand, $cacheTime, $quality, $keepType);
605:             break;
606:         case 'im':
607:             $return = cApiImgScaleImageMagick($img, $maxX, $maxY, $crop, $expand, $cacheTime, $quality, $keepType);
608:             break;
609:         case 'failure':
610:         default:
611:             $frontendURL = cRegistry::getFrontendUrl();
612:             $return = str_replace(cRegistry::getFrontendPath(), $frontendURL, $img);
613:             break;
614:     }
615: 
616:     if ($deleteAfter == true) {
617:         unlink($img);
618:     }
619: 
620:     return $return;
621: }
622: 
623: /**
624:  * Check possible image editing functionality.
625:  *
626:  * @return mixed
627:  *         Information about installed image editing extensions/tools
628:  *         <pre>
629:  *         - 'im' ImageMagick is available and usage is enabled
630:  *         - '2' GD library version 2 is available
631:  *         - '1' GD library version 1 is available
632:  *         - '0' Nothing could detected
633:  *         </pre>
634:  */
635: function cApiImageCheckImageEditingPossibility() {
636:     global $cfg;
637: 
638:     if ($cfg['images']['image_magick']['use']) {
639:         if (cApiIsImageMagickAvailable()) {
640:             return 'im';
641:         }
642:     }
643: 
644:     if (!extension_loaded('gd')) {
645:         return '0';
646:     }
647: 
648:     if (function_exists('gd_info')) {
649:         $sGDVersion = '';
650:         $aGDInformation = gd_info();
651:         if (preg_match('#([0-9\.])+#', $aGDInformation['GD Version'], $sGDVersion)) {
652:             if ($sGDVersion[0] >= '2') {
653:                 return '2';
654:             }
655:             return '1';
656:         }
657:         return '1';
658:     }
659:     return '1';
660: }
661: 
662: /**
663:  * @deprecated Use cApiImageCheckImageEditingPossibility()
664:  * @return mixed @see \cApiImageCheckImageEditingPossibility()
665:  */
666: function cApiImageCheckImageEditingPosibility() {
667:     return cApiImageCheckImageEditingPossibility();
668: }
669: 
670: /**
671:  * Returns new calculated dimensions of a target image.
672:  *
673:  * @param int $x
674:  * @param int $y
675:  * @param int $maxX
676:  * @param int $maxY
677:  * @param bool $expand
678:  * @return array
679:  *         Index 0 is target X and index 1 is target Y
680:  */
681: function cApiImageGetTargetDimensions($x, $y, $maxX, $maxY, $expand) {
682:     if (($maxX / $x) < ($maxY / $y)) {
683:         $targetY = $y * ($maxX / $x);
684:         $targetX = round($maxX);
685: 
686:         // Force wished height
687:         if ($targetY < $maxY) {
688:             $targetY = ceil($targetY);
689:         } else {
690:             $targetY = floor($targetY);
691:         }
692:     } else {
693:         $targetX = $x * ($maxY / $y);
694:         $targetY = round($maxY);
695: 
696:         // Force wished width
697:         if ($targetX < $maxX) {
698:             $targetX = ceil($targetX);
699:         } else {
700:             $targetX = floor($targetX);
701:         }
702:     }
703: 
704:     if ($expand == false && (($targetX > $x) || ($targetY > $y))) {
705:         $targetX = $x;
706:         $targetY = $y;
707:     }
708: 
709:     $targetX = ($targetX != 0) ? $targetX : 1;
710:     $targetY = ($targetY != 0) ? $targetY : 1;
711: 
712:     return array(
713:         $targetX,
714:         $targetY
715:     );
716: }
717: 
718: /**
719:  * Returns cache file name.
720:  *
721:  * @param string $md5
722:  * @param string $fileType
723:  * @param bool $keepType
724:  * @return string
725:  */
726: function cApiImageGetCacheFileName($md5, $fileType, $keepType) {
727:     // Create the target file names for web and server
728: 
729:     // Should we keep the file type?
730:     if ($keepType) {
731:         // Just using switch if someone likes to add other types
732:         switch (cString::toLowerCase($fileType)) {
733:             case 'png':
734:                 $fileName = $md5 . '.png';
735:                 break;
736:             case 'gif':
737:                 $fileName = $md5 . '.gif';
738:                 break;
739:             default:
740:                 $fileName = $md5 . '.jpg';
741:         }
742:     } else {
743:         // No... use .jpg
744:         $fileName = $md5 . '.jpg';
745:     }
746: 
747:     return $fileName;
748: }
749: 
750: /**
751:  * Validates cache version of a image.
752:  *
753:  * @param string $cacheFile
754:  * @param int $cacheTime
755:  * @return bool
756:  *         Returns true, if cache file exists and7or is still valid or false
757:  */
758: function cApiImageCheckCachedImageValidity($cacheFile, $cacheTime) {
759: 
760:     // Check if the file exists. If it does, check if the file is valid.
761:     if (cFileHandler::exists($cacheFile)) {
762:         if ($cacheTime == 0) {
763:             // Do not check expiration date
764:             return true;
765:         } else if (!function_exists('md5_file')) {
766:             // TODO: Explain why this is still needed ... or remove it
767:             if ((filemtime($cacheFile) + (60 * $cacheTime)) < time()) {
768:                 // Cache time expired, unlink the file
769:                 unlink($cacheFile);
770:             } else {
771:                 // Return the web file name
772:                 return true;
773:             }
774:         } else {
775:             return true;
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 === cString::findFirstPos($output[0], 'ImageMagick')) {
817:         $imagemagickAvailable = false;
818:     } else {
819:         $imagemagickAvailable = true;
820:     }
821: 
822:     return $imagemagickAvailable;
823: }
824: 
CMS CONTENIDO 4.10.0 API documentation generated by ApiGen 2.8.0