| [ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of eZImageManager class 4 // 5 // Created on: <01-Mar-2002 14:23:49 amos> 6 // 7 // SOFTWARE NAME: eZ publish 8 // SOFTWARE RELEASE: 3.9.0 9 // BUILD VERSION: 17785 10 // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS 11 // SOFTWARE LICENSE: GNU General Public License v2.0 12 // NOTICE: > 13 // This program is free software; you can redistribute it and/or 14 // modify it under the terms of version 2.0 of the GNU General 15 // Public License as published by the Free Software Foundation. 16 // 17 // This program is distributed in the hope that it will be useful, 18 // but WITHOUT ANY WARRANTY; without even the implied warranty of 19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 // GNU General Public License for more details. 21 // 22 // You should have received a copy of version 2.0 of the GNU General 23 // Public License along with this program; if not, write to the Free 24 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 25 // MA 02110-1301, USA. 26 // 27 // 28 29 /*! \defgroup eZImage Image conversion and scaling */ 30 31 /*! 32 \class eZImageManager ezimagemanager.php 33 \ingroup eZImage 34 \brief Manages image operations using delegates to do the work 35 36 The manager allows for transparent conversion of one image format 37 to another. The conversion may be done in one step if the required 38 conversion type is available or it may build a tree of conversion 39 rules which is needed to reach the desired end format. 40 41 It's also possible to run operations on images. It's up to each conversion 42 rule to report whether or not the operation is supported, the manager will 43 then distribute the operations on the available rules which can handle them. 44 Examples of operations are scaling and grayscale. 45 46 The scale operation is special and is known to the manager directly while 47 the other operations must be recognized by the converter. 48 49 In determing what image rules to be used the manager must first know 50 which output types are allowed, this is set with setOutputTypes(). 51 It takes an array of mimetypes which are allowed. 52 53 The manager must then be fed conversion rules, these tell which conversion 54 type is used for converting from the source mimetype to the destination 55 mimetype. The rules are set with setRules() which accepts an array of 56 rules and a default rule as paremeter. The default rule is used when no 57 other mimetype match is found. 58 To create a rule you should use the createRule() function, it takes the source 59 and destination mimetype as well as the conversion type name. Optionally 60 it can specified whether the rule can scale or run operations. 61 62 The last thing that needs to be done is to specify the mimetypes. The manager 63 uses mimetypes internally to know what type of image it's working on. 64 To go from a filename to a mimetype a set of matches must be setup. The matches 65 are created with createMIMEType() which takes the mimetype, regex filename match 66 and suffix as parameter. The mimetypes are then registered with setMIMETypes(). 67 68 See <a href="http://www.iana.org/">www.iana.org</a> for information on MIME types. 69 70 Now the manager is ready and you can convert images with convert(). 71 72 Example: 73 \code 74 $img =& eZImageManager::instance(); 75 $img->registerType( "convert", new eZImageShell( '', "convert", array(), array(), 76 array( eZImageShell::createRule( "-geometry %wx%h>", // Scale rule 77 "modify/scale" ), 78 eZImageShell::createRule( "-colorspace GRAY", // Grayscale rule 79 "colorspace/gray" ) ) ) ); // Register shell program convert 80 $img->registerType( "gd", new eZImageGD() ); // Register PHP converter GD 81 82 $img->setOutputTypes( array( "image/jpeg", 83 "image/png" ) ); // We only want jpeg and png, gif is not recommended due to licencing issues. 84 $rules = array( $img->createRule( "image/jpeg", "image/jpeg", "GD", true, false ), // Required for scaling jpeg images 85 $img->createRule( "image/gif", "image/png", "convert", true, false ) ); // Convert GIF to png 86 $img->setRules( $rules, $img->createRule( "*", "image/png", "convert", true, false ) ); // Convert all other images to PNG with convert 87 88 $mime_rules = array( $img->createMIMEType( "image/jpeg", "\.jpe?g$", "jpg" ), 89 $img->createMIMEType( "image/png", "\.png$", "png" ), 90 $img->createMIMEType( "image/gif", "\.gif$", "gif" ) ); 91 $img->setMIMETypes( $mime_rules ); // Register mimetypes 92 93 $img1 = $img->convert( "image1.gif", "cache/" ); // Convert GIF and places it in cache dir 94 $img1 = $img->convert( "image1.png", "cache/", // Scale PNG image and place in cache dir 95 array( "width" => 200, "height" => 200 ), // Scale parameter 96 array( array( "rule-type" => "colorspace/gray" ) ) ); // Gray scale conversion 97 \endcode 98 99 100 */ 101 102 include_once ( 'lib/ezutils/classes/ezini.php' ); 103 104 class eZImageManager 105 { 106 /*! 107 Initializes the manager by registering a application/octet-stream mimetype 108 which is applied for all unknown files. 109 */ 110 function eZImageManager() 111 { 112 // $this->MIMEOctet =& $this->createMIMEType( "application/octet-stream", "^.+$", "" ); 113 $this->SupportedFormats = array(); 114 $this->SupportedMIMEMap = array(); 115 $this->ImageHandlers = array(); 116 $this->AliasList = array(); 117 $this->Factories = array(); 118 $this->ImageFilters = array(); 119 $this->MIMETypeSettings = array(); 120 $this->MIMETypeSettingsMap = array(); 121 $this->QualityValues = array(); 122 $this->QualityValueMap = array(); 123 124 $ini =& eZINI::instance( 'image.ini' ); 125 $this->TemporaryImageDirPath = eZSys::cacheDirectory() . '/' . $ini->variable( 'FileSettings', 'TemporaryDir' ); 126 } 127 128 /*! 129 Sets which MIME-Types are allowed to use for destination format, this is an array of MIME-Type names. 130 e.g. 131 \code 132 $manager->setOutputTypes( array( 'image/jpeg', 'image/gif' ) ); 133 \endcode 134 */ 135 function setSupportedFormats( $mimeList ) 136 { 137 $this->SupportedFormats = $mimeList; 138 $this->SupportedMIMEMap = array(); 139 foreach ( $mimeList as $mimeName ) 140 { 141 $this->SupportedMIMEMap[$mimeName] = true; 142 } 143 } 144 145 /*! 146 Sets which MIME-Types are allowed to use for destination format, this is an array of MIME-Type names. 147 e.g. 148 \code 149 $manager->setOutputTypes( array( 'image/jpeg', 'image/gif' ) ); 150 \endcode 151 */ 152 function appendSupportedFormat( $mimeName ) 153 { 154 $this->SupportedFormats[] = $mimeName; 155 $this->SupportedMIMEMap[$mimeName] = true; 156 } 157 158 /*! 159 Appends the image handler \a $handler to the list of known handlers in the image system. 160 Onces it is added the supported image filters for that handler is extracted. 161 162 \note If the handler is not available (isAvailable()) it will not be added. 163 */ 164 function appendImageHandler( &$handler ) 165 { 166 if ( !$handler ) 167 return false; 168 if ( !$handler->isAvailable() ) 169 return false; 170 $this->ImageHandlers[] =& $handler; 171 $this->ImageFilters = array_merge( $this->ImageFilters, $handler->supportedImageFilters() ); 172 $this->ImageFilters = array_unique( $this->ImageFilters ); 173 return true; 174 } 175 176 /*! 177 \return \c true if the filtername \a $filtername is supported by any of the image handlers. 178 */ 179 function isFilterSupported( $filterName ) 180 { 181 return in_array( $filterName, $this->ImageFilters ); 182 } 183 184 /*! 185 Returns a list of defined image aliases in the image system. 186 Each entry in the list is an associative array with the following keys: 187 - name - The name of the alias 188 - reference - The name of the alias it refers to or \c false if no reference 189 - mime_type - Controls which MIME-Type the alias will be in, or \c false if not defined. 190 - filters - An array with filters which applies to this alias 191 - alias_key - The CRC key for this alias, it is created from the current values of the alias 192 and will change each time the alias values changes 193 */ 194 function aliasList() 195 { 196 $aliasList = $this->AliasList; 197 if ( !isset( $aliasList['original'] ) ) 198 { 199 $alias = array( 'name' => 'original', 200 'reference' => false, 201 'mime_type' => false, 202 'filters' => array() ); 203 $alias['alias_key'] = $this->createImageAliasKey( $alias ); 204 $aliasList['original'] = $alias; 205 } 206 return $aliasList; 207 } 208 209 /*! 210 \return \c true if the image alias \a $aliasName exists. 211 */ 212 function hasAlias( $aliasName ) 213 { 214 $aliasList = $this->aliasList(); 215 return array_key_exists( $aliasName, $aliasList ); 216 } 217 218 /*! 219 \return the definition for the Image Alias named \a $aliasName. 220 */ 221 function alias( $aliasName ) 222 { 223 $aliasList = $this->aliasList(); 224 if ( !array_key_exists( $aliasName, $aliasList ) ) 225 return false; 226 return $aliasList[$aliasName]; 227 } 228 229 /*! 230 Appends the image alias \a $alias to the list of defined aliases. 231 */ 232 function appendImageAlias( $alias ) 233 { 234 $key = $this->createImageAliasKey( $alias ); 235 $alias['alias_key'] = $key; 236 $this->AliasList[$alias['name']] = $alias; 237 return $key; 238 } 239 240 /*! 241 Creates a unique key for the image alias and returns it. 242 \note The key is an MD5 of the alias settings and is used to determine if alias settings has changed. 243 */ 244 function createImageAliasKey( $alias ) 245 { 246 $keyData = array( $alias['name'], 247 $alias['reference'], 248 $alias['mime_type'] ); 249 if ( $alias['reference'] ) 250 { 251 $referenceAlias = $this->alias( $alias['reference'] ); 252 if ( $referenceAlias ) 253 $keyData[] = $referenceAlias['alias_key']; 254 } 255 foreach ( $alias['filters'] as $filter ) 256 { 257 $filterData = $filter['name']; 258 if ( is_array( $filter['data'] ) ) 259 $filterData .= '=' . implode( ',', $filter['data'] ); 260 $keyData[] = $filterData; 261 } 262 263 include_once ( 'lib/ezutils/classes/ezsys.php' ); 264 $key = eZSys::ezcrc32( implode( "\n", $keyData ) ); 265 266 return $key; 267 } 268 269 /*! 270 \return \c true if the Image Alias \a $alias is valid for use. 271 This is tested by checking the key against the key for current Image Alias settings. 272 */ 273 function isImageAliasValid( $alias ) 274 { 275 $aliasName = $alias['name']; 276 if ( isset( $this->AliasList[$aliasName] ) ) 277 { 278 $aliasKey = $alias['alias_key']; 279 $aliasInfo = $this->AliasList[$aliasName]; 280 $checkKey = $aliasInfo['alias_key']; 281 $isValid = ( $aliasKey == $checkKey ); 282 if ( $isValid ) 283 { 284 $aliasTimestamp = $alias['timestamp']; 285 $isValid = $this->isImageTimestampValid( $aliasTimestamp ); 286 } 287 return $isValid; 288 } 289 return false; 290 } 291 292 /*! 293 \return \c true if the timestamp \a $timestamp is newer than the image alias expiry timestamp. 294 The image alias expiry timestamp will be set whenever the image aliases must be recreated. 295 296 \note Normally the expiry timestamp is not set. 297 */ 298 function isImageTimestampValid( $timestamp ) 299 { 300 include_once ( 'lib/ezutils/classes/ezexpiryhandler.php' ); 301 $expiryHandler = eZExpiryHandler::instance(); 302 if ( $expiryHandler->hasTimestamp( 'image-manager-alias' ) ) 303 { 304 $aliasTimestamp = $expiryHandler->timestamp( 'image-manager-alias' ); 305 return ( $timestamp > $aliasTimestamp ); 306 } 307 return true; 308 } 309 310 /*! 311 Reads all image aliases from the INI file 'image.ini' 312 and appends them to the image system. 313 \param $iniFile The INI file to read from or if \c false use 'image.ini' 314 */ 315 function readImageAliasesFromINI( $iniFile = false ) 316 { 317 if ( !$iniFile ) 318 $iniFile = 'image.ini'; 319 $ini =& eZINI::instance( $iniFile ); 320 if ( !$ini ) 321 return false; 322 $aliasNames = $ini->variable( 'AliasSettings', 'AliasList' ); 323 foreach ( $aliasNames as $aliasName ) 324 { 325 $alias = $this->createAliasFromINI( $aliasName ); 326 if ( $alias ) 327 { 328 $this->appendImageAlias( $alias ); 329 } 330 else 331 eZDebug::writeWarning( "Failed reading Image Alias $aliasName from $iniFile", 332 'eZImageManager::readImageAliasFromINI' ); 333 } 334 $aliasName = 'original'; 335 if ( !in_array( $aliasName, $aliasNames ) ) 336 { 337 include_once ( 'lib/ezutils/classes/ezini.php' ); 338 $ini =& eZINI::instance( 'image.ini' ); 339 if ( $ini->hasGroup( $aliasName ) ) 340 { 341 $alias = $this->createAliasFromINI( $aliasName ); 342 if ( $alias ) 343 { 344 $alias['reference'] = false; 345 $this->appendImageAlias( $alias ); 346 } 347 else 348 eZDebug::writeWarning( "Failed reading Image Alias $aliasName from $iniFile", 349 'eZImageManager::readImageAliasFromINI' ); 350 } 351 } 352 } 353 354 /*! 355 Reads all supported image formats from the INI file 'image.ini' 356 and appends them to the image system. 357 \param $iniFile The INI file to read from or if \c false use 'image.ini' 358 */ 359 function readSupportedFormatsFromINI( $iniFile = false ) 360 { 361 if ( !$iniFile ) 362 $iniFile = 'image.ini'; 363 $ini =& eZINI::instance( $iniFile ); 364 if ( !$ini ) 365 return false; 366 $allowedOutputFormats = $ini->variable( 'OutputSettings', 'AllowedOutputFormat' ); 367 foreach ( $allowedOutputFormats as $allowedOutputFormat ) 368 { 369 $this->appendSupportedFormat( $allowedOutputFormat ); 370 } 371 } 372 373 /*! 374 \return \c true if the MIME-Type defined in \a $mimeData exists in the image system. 375 */ 376 function hasMIMETypeSetting( $mimeData ) 377 { 378 return isset( $this->MIMETypeSettingsMap[$mimeData['name']] ); 379 } 380 381 /*! 382 \return The setting for the MIME-Type defined in \a $mimeData. 383 */ 384 function mimeTypeSetting( $mimeData ) 385 { 386 if ( !isset( $this->MIMETypeSettingsMap[$mimeData['name']] ) ) 387 return false; 388 $list = $this->MIMETypeSettingsMap[$mimeData['name']]; 389 foreach ( $list as $item ) 390 { 391 if ( is_array( $item['match'] ) ) 392 { 393 if ( is_array( $mimeData['info'] ) ) 394 { 395 $isMatch = true; 396 $info =& $mimeData['info']; 397 foreach ( $item['match'] as $matchKey => $matchValue ) 398 { 399 if ( !isset( $info[$matchKey] ) or 400 $info[$matchKey] != $matchValue ) 401 { 402 $isMatch = false; 403 break; 404 } 405 } 406 if ( $isMatch ) 407 return $item; 408 } 409 } 410 else 411 return $item; 412 } 413 return false; 414 } 415 416 /*! 417 Calls eZImageHandler::wildcardToRegexp() to generate 418 a regular expression out of the wilcard and return it. 419 */ 420 function wildcardToRegexp( $wildcard, $separatorCharacter = false ) 421 { 422 return eZImageHandler::wildcardToRegexp( $wildcard, $separatorCharacter ); 423 } 424 425 /*! 426 \return The override MIME-Type for the MIME structure \a $mimeData or \c false if no override. 427 */ 428 function mimeTypeOverride( $mimeData ) 429 { 430 if ( $this->hasMIMETypeSetting( $mimeData ) ) 431 { 432 $settings = $this->mimeTypeSetting( $mimeData ); 433 if ( $settings ) 434 { 435 return $settings['override_mime_type']; 436 } 437 } 438 return false; 439 } 440 441 /*! 442 \return An array with extra filters for the MIME-Type defined in \a $mimeData 443 or \c false if no filters. 444 */ 445 function mimeTypeFilters( $mimeData ) 446 { 447 if ( $this->hasMIMETypeSetting( $mimeData ) ) 448 { 449 $settings = $this->mimeTypeSetting( $mimeData ); 450 if ( $settings ) 451 { 452 return $settings['extra_filters']; 453 } 454 } 455 return false; 456 } 457 458 /*! 459 \return \c true if the filtername \a $filtername is allowed to be used on the type defined in \a $mimeData. 460 */ 461 function isFilterAllowed( $filterName, $mimeData ) 462 { 463 if ( $this->hasMIMETypeSetting( $mimeData ) ) 464 { 465 $settings = $this->mimeTypeSetting( $mimeData ); 466 if ( $settings ) 467 { 468 if ( is_array( $settings['disallowed_filters'] ) ) 469 { 470 foreach ( $settings['disallowed_filters'] as $filter ) 471 { 472 $regexp = eZImageManager::wildcardToRegexp( $filter ); 473 if ( preg_match( '#' . $regexp . '#', $filterName ) ) 474 { 475 return false; 476 } 477 } 478 } 479 if ( is_array( $settings['allowed_filters'] ) ) 480 { 481 foreach ( $settings['allowed_filters'] as $filter ) 482 { 483 $regexp = eZImageManager::wildcardToRegexp( $filter ); 484 if ( preg_match( '#' . $regexp . '#', $filterName ) ) 485 { 486 return true; 487 } 488 } 489 return false; 490 } 491 return true; 492 } 493 } 494 return true; 495 } 496 497 /*! 498 Binds the quality value \a $qualityValue to the MIME-Type \a $mimeType. 499 */ 500 function appendQualityValue( $mimeType, $qualityValue ) 501 { 502 $element = array( 'name' => $mimeType, 503 'value' => $qualityValue ); 504 $this->QualityValues[] = $element; 505 $this->QualityValueMap[$mimeType] = $element; 506 } 507 508 /*! 509 \return the quality value for MIME-Type \a $mimeType or \c false if none exists. 510 */ 511 function qualityValue( $mimeType ) 512 { 513 if ( isset( $this->QualityValueMap[$mimeType] ) ) 514 return $this->QualityValueMap[$mimeType]['value']; 515 return false; 516 } 517 518 /*! 519 Appends the MIME-Type setting \a $settings to the image system. 520 */ 521 function appendMIMETypeSetting( $settings ) 522 { 523 $this->MIMETypeSettings[] =& $settings; 524 if ( !isset( $this->MIMETypeSettingsMap[$settings['mime_type']] ) ) 525 $this->MIMETypeSettingsMap[$settings['mime_type']] = array(); 526 $this->MIMETypeSettingsMap[$settings['mime_type']][] =& $settings; 527 } 528 529 /*! 530 Reads all MIME-Type settings from the INI file 'image.ini' 531 and appends them to the image system. 532 \param $iniFile The INI file to read from or if \c false use 'image.ini' 533 */ 534 function readMIMETypeSettingsFromINI( $iniFile = false ) 535 { 536 if ( !$iniFile ) 537 $iniFile = 'image.ini'; 538 $ini =& eZINI::instance( $iniFile ); 539 if ( !$ini ) 540 return false; 541 $overrideList = $ini->variable( 'MIMETypeSettings', 'OverrideList' ); 542 foreach ( $overrideList as $mimeType ) 543 { 544 $settings = eZImageManager::readMIMETypeSettingFromINI( $mimeType ); 545 if ( $settings ) 546 $this->appendMIMETypeSetting( $settings ); 547 } 548 } 549 550 /*! 551 Reads MIME-Type quality settings and appends them. 552 */ 553 function readMIMETypeQualitySettingFromINI( $iniFile = false ) 554 { 555 if ( !$iniFile ) 556 $iniFile = 'image.ini'; 557 $ini =& eZINI::instance( $iniFile ); 558 if ( !$ini ) 559 return false; 560 if ( !$ini->hasVariable( 'MIMETypeSettings', 'Quality' ) ) 561 return false; 562 $values = $ini->variable( 'MIMETypeSettings', 'Quality' ); 563 foreach ( $values as $value ) 564 { 565 $elements = explode( ';', $value ); 566 $mimeType = $elements[0]; 567 $qualityValue = $elements[1]; 568 $this->appendQualityValue( $mimeType, $qualityValue ); 569 } 570 } 571 572 /*! 573 Reads in global conversion rules from INI file. 574 */ 575 function readConversionRuleSettingsFromINI( $iniFile = false ) 576 { 577 if ( !$iniFile ) 578 $iniFile = 'image.ini'; 579 $ini =& eZINI::instance( $iniFile ); 580 if ( !$ini ) 581 return false; 582 if ( $ini->hasVariable( 'MIMETypeSettings', 'ConversionRules' ) ) 583 { 584 $conversionRules = array(); 585 $rules = $ini->variable( 'MIMETypeSettings', 'ConversionRules' ); 586 foreach ( $rules as $ruleString ) 587 { 588 $ruleItems = explode( ';', $ruleString ); 589 if ( count( $ruleItems ) >= 2 ) 590 { 591 $conversionRule = array( 'from' => $ruleItems[0], 592 'to' => $ruleItems[1] ); 593 $this->appendConversionRule( $conversionRule ); 594 } 595 } 596 } 597 } 598 599 /*! 600 Will read in all required INI settings. 601 */ 602 function readINISettings() 603 { 604 $this->readImageHandlersFromINI(); 605 $this->readSupportedFormatsFromINI(); 606 $this->readImageAliasesFromINI(); 607 $this->readMIMETypeSettingsFromINI(); 608 $this->readMIMETypeQualitySettingFromINI(); 609 $this->readConversionRuleSettingsFromINI(); 610 } 611 612 /*! 613 Appends a new global conversion rule. 614 */ 615 function appendConversionRule( $conversionRule ) 616 { 617 $this->ConversionRules[] = $conversionRule; 618 } 619 620 /*! 621 \return The global conversion rules. 622 */ 623 function conversionRules() 624 { 625 return $this->ConversionRules; 626 } 627 628 /*! 629 Reads a single MIME-Type setting from the INI file 'image.ini' 630 and appends them to the image system. 631 \param $mimeGroup Which INI group to read settings from. 632 \param $iniFile The INI file to read from or if \c false use 'image.ini' 633 \return The settings that were read. 634 */ 635 function readMIMETypeSettingFromINI( $mimeGroup, $iniFile = false ) 636 { 637 if ( !$iniFile ) 638 $iniFile = 'image.ini'; 639 $ini =& eZINI::instance( $iniFile ); 640 if ( !$ini ) 641 return false; 642 if ( !$ini->hasGroup( $mimeGroup ) ) 643 return false; 644 if ( !$ini->hasVariable( $mimeGroup, 'MIMEType' ) ) 645 return false; 646 $settings = array( 'name' => $mimeGroup, 647 'match' => false, 648 'mime_type' => false, 649 'override_mime_type' => false, 650 'allowed_filters' => false, 651 'disallowed_filters' => false, 652 'extra_filters' => false ); 653 $settings['mime_type'] = $ini->variable( $mimeGroup, 'MIMEType' ); 654 $ini->assign( $mimeGroup, 'Match', $settings['match'] ); 655 $ini->assign( $mimeGroup, 'OverrideMIMEType', $settings['override_mime_type'] ); 656 $ini->assign( $mimeGroup, 'AllowedFilters', $settings['allowed_filters'] ); 657 $ini->assign( $mimeGroup, 'DisallowedFilters', $settings['disallowed_filters'] ); 658 if ( $ini->hasVariable( $mimeGroup, 'ExtraFilters' ) ) 659 { 660 $filters = array(); 661 $filterRawList = $ini->variable( $mimeGroup, 'ExtraFilters' ); 662 foreach ( $filterRawList as $filterRawItem ) 663 { 664 $filters[] = $this->createFilterDataFromINI( $filterRawItem ); 665 } 666 if ( count( $filters ) > 0 ) 667 $settings['extra_filters'] = $filters; 668 } 669 return $settings; 670 } 671 672 /*! 673 Reads all settings for image handlers from the INI file 'image.ini' 674 and appends them to the image system. 675 \param $iniFile The INI file to read from or if \c false use 'image.ini' 676 */ 677 function readImageHandlersFromINI( $iniFile = false ) 678 { 679 if ( !$iniFile ) 680 $iniFile = 'image.ini'; 681 $ini =& eZINI::instance( $iniFile ); 682 if ( !$ini ) 683 return false; 684 $handlerList = $ini->variable( 'ImageConverterSettings', 'ImageConverters' ); 685 foreach ( $handlerList as $handlerName ) 686 { 687 if ( $ini->hasGroup( $handlerName ) ) 688 { 689 if ( $ini->hasVariable( $handlerName, 'Handler' ) ) 690 { 691 $factoryName = $ini->variable( $handlerName, 'Handler' ); 692 $factory =& $this->factoryFor( $factoryName, $iniFile ); 693 if ( $factory ) 694 { 695 $convertHandler =& $factory->produceFromINI( $handlerName, $iniFile ); 696 $this->appendImageHandler( $convertHandler ); 697 } 698 } 699 else 700 { 701 eZDebug::writeWarning( "INI group $handlerName does not have a Handler setting, cannot instantiate handler without it", 702 'eZImageManager::readImageHandlersFromINI' ); 703 } 704 } 705 else 706 { 707 eZDebug::writeWarning( "No INI group $handlerName for Image Handler $handlerName, cannot instantiate", 708 'eZImageManager::readImageHandlersFromINI' ); 709 } 710 } 711 } 712 713 /*! 714 Finds the image handler factory with the name \a $factoryName and returns it. 715 \param $iniFile The INI file to read from or if \c false use 'image.ini' 716 */ 717 function &factoryFor( $factoryName, $iniFile = false ) 718 { 719 if ( !$iniFile ) 720 $iniFile = 'image.ini'; 721 if ( isset( $this->Factories[$factoryName] ) ) 722 { 723 return $this->Factories[$factoryName]; 724 } 725 else 726 { 727 include_once ( 'lib/ezutils/classes/ezextension.php' ); 728 if ( eZExtension::findExtensionType( array( 'ini-name' => $iniFile, 729 'repository-group' => 'ImageConverterSettings', 730 'repository-variable' => 'RepositoryList', 731 'extension-group' => 'ImageConverterSettings', 732 'extension-variable' => 'ExtensionList', 733 'extension-subdir' => 'imagehandler', 734 'alias-group' => 'ImageConverterSettings', 735 'alias-variable' => 'ImageHandlerAlias', 736 'suffix-name' => 'handler.php', 737 'type-directory' => false, 738 'type' => $factoryName ), 739 $result ) ) 740 { 741 $filepath = $result['found-file-path']; 742 include_once( $filepath ); 743 $className = $result['type'] . 'factory'; 744 if ( class_exists( $className ) ) 745 { 746 $factory = new $className(); 747 $this->Factories[$factoryName] =& $factory; 748 return $factory; 749 } 750 else 751 { 752 eZDebug::writeWarning( "The Image Factory class $className was not found, cannot create factory", 753 'eZImageManager::factoryFor' ); 754 } 755 } 756 else 757 { 758 eZDebug::writeWarning( "Could not locate Image Factory for $factoryName", 759 'eZImageManager::factoryFor' ); 760 } 761 } 762 $retValue = false; 763 return $retValue; 764 } 765 766 /*! 767 Parses the filter text \a $filterText which is taken from an INI file 768 and returns a filter data structure for it. 769 */ 770 function createFilterDataFromINI( $filterText ) 771 { 772 $equalPosition = strpos( $filterText, '=' ); 773 $filterData = false; 774 if ( $equalPosition !== false ) 775 { 776 $filterName = substr( $filterText, 0, $equalPosition ); 777 $filterDataText = substr( $filterText, $equalPosition + 1 ); 778 $filterData = explode( ';', $filterDataText ); 779 } 780 else 781 $filterName = $filterText; 782 return array( 'name' => $filterName, 783 'data' => $filterData ); 784 } 785 786 /*! 787 Parses the INI group \a $iniGroup and creates an Image Alias from it. 788 \return the Image Alias structure. 789 */ 790 function createAliasFromINI( $iniGroup ) 791 { 792 include_once ( 'lib/ezutils/classes/ezini.php' ); 793 $ini =& eZINI::instance( 'image.ini' ); 794 if ( !$ini->hasGroup( $iniGroup ) ) 795 { 796 eZDebug::writeError( "No such group $iniGroup in ini file image.ini", 797 'eZImageManager::createAliasFromINI' ); 798 return false; 799 } 800 $alias = array( 'name' => $iniGroup, 801 'reference' => false, 802 'mime_type' => false, 803 'filters' => array() ); 804 if ( $ini->hasVariable( $iniGroup, 'Name' ) ) 805 $alias['name'] = $ini->variable( $iniGroup, 'Name' ); 806 if ( $ini->hasVariable( $iniGroup, 'MIMEType' ) ) 807 $alias['mime_type'] = $ini->variable( $iniGroup, 'MIMEType' ); 808 if ( $ini->hasVariable( $iniGroup, 'Filters' ) ) 809 { 810 $filters = array(); 811 $filterRawList = $ini->variable( $iniGroup, 'Filters' ); 812 foreach ( $filterRawList as $filterRawItem ) 813 { 814 $filters[] = $this->createFilterDataFromINI( $filterRawItem ); 815 } 816 $alias['filters'] = $filters; 817 } 818 if ( $ini->hasVariable( $iniGroup, 'Reference' ) ) 819 $alias['reference'] = $ini->variable( $iniGroup, 'Reference' ); 820 return $alias; 821 } 822 823 /*! 824 Makes sure the Image Alias \a $aliasName is created. It will check if referenced 825 image aliases exists and if not create those also. 826 \return \c true if successful 827 */ 828 function createImageAlias( $aliasName, &$existingAliasList, $parameters = array() ) 829 { 830 $aliasList = $this->aliasList(); 831 if ( !isset( $aliasList[$aliasName] ) ) 832 { 833 eZDebug::writeWarning( "Alias name $aliasName does not exist, cannot create it" ); 834 return false; 835 } 836 $currentAliasInfo = $aliasList[$aliasName]; 837 $referenceAlias = $currentAliasInfo['reference']; 838 if ( $referenceAlias and !$this->hasAlias( $referenceAlias ) ) 839 { 840 eZDebug::writeError( "The referenced alias '$referenceAlias' for image alias '$aliasName' does not exist, cannot use it for reference.\n" . 841 "Will use 'original' alias instead.", 842 'eZImageManager::createImageAlias' ); 843 $referenceAlias = false; 844 } 845 if ( !$referenceAlias ) 846 $referenceAlias = 'original'; 847 $hasReference = false; 848 if ( array_key_exists( $referenceAlias, $existingAliasList ) ) 849 { 850 // VS-DBFILE 851 852 require_once ( 'kernel/classes/ezclusterfilehandler.php' ); 853 $fileHandler = eZClusterFileHandler::instance(); 854 if ( $fileHandler->fileExists( $existingAliasList[$referenceAlias]['url'] ) ) 855 { 856 $hasReference = true; 857 } 858 else 859 { 860 eZDebug::writeError( "The reference alias $referenceAlias file " . $existingAliasList[$referenceAlias]['url'] . " does not exist", 861 'eZImageManager::createImageAlias' ); 862 } 863 } 864 if ( !$hasReference ) 865 { 866 if ( $referenceAlias == 'original' ) 867 { 868 eZDebug::writeError( "Original alias does not exists, cannot create other aliases without it" ); 869 return false; 870 } 871 if ( !$this->createImageAlias( $referenceAlias, $existingAliasList, $parameters ) ) 872 { 873 eZDebug::writeError( "Failed creating the referenced alias $referenceAlias, cannot create alias $aliasName", 874 'eZImageManager::createImageAlias' ); 875 return false; 876 } 877 } 878 if ( array_key_exists( $referenceAlias, $existingAliasList ) ) 879 { 880 $aliasInfo = $existingAliasList[$referenceAlias]; 881 $aliasFilePath = $aliasInfo['url']; 882 $aliasKey = $currentAliasInfo['alias_key']; 883 884 // VS-DBFILE 885 886 require_once ( 'kernel/classes/ezclusterfilehandler.php' ); 887 $aliasFile = eZClusterFileHandler::instance( $aliasFilePath ); 888 889 if ( $aliasFile->exists() ) 890 { 891 $aliasFile->fetch(); 892 include_once ( 'lib/ezutils/classes/ezmimetype.php' ); 893 $sourceMimeData = eZMimeType::findByFileContents( $aliasFilePath ); 894 $destinationMimeData = $sourceMimeData; 895 if ( isset( $parameters['basename'] ) ) 896 { 897 $sourceMimeData['basename'] = $parameters['basename']; 898 eZMimeType::changeBasename( $destinationMimeData, $parameters['basename'] ); 899 } 900 $destinationMimeData['is_valid'] = false; 901 if ( !$this->convert( $sourceMimeData, $destinationMimeData, $aliasName, $parameters ) ) 902 { 903 $sourceFile = $sourceMimeData['url']; 904 $destinationDir = $destinationMimeData['dirpath']; 905 eZDebug::writeError( "Failed converting $sourceFile to alias $referenceAlias in directory $destinationDir", 906 'eZImageManager::createImageAlias' ); 907 // VS-DBFILE 908 $aliasFile->deleteLocal(); 909 return false; 910 } 911 $currentAliasData = array( 'url' => $destinationMimeData['url'], 912 'dirpath' => $destinationMimeData['dirpath'], 913 'filename' => $destinationMimeData['filename'], 914 'suffix' => $destinationMimeData['suffix'], 915 'basename' => $destinationMimeData['basename'], 916 'alternative_text' => $aliasInfo['alternative_text'], 917 'name' => $aliasName, 918 'sub_type' => false, 919 'timestamp' => time(), 920 'alias_key' => $aliasKey, 921 'mime_type' => $destinationMimeData['name'], 922 'override_mime_type' => false, 923 'info' => false, 924 'width' => false, 925 'height' => false, 926 'is_valid' => true, 927 'is_new' => true ); 928 if ( isset( $destinationMimeData['override_mime_type'] ) ) 929 $currentAliasData['override_mime_type'] = $destinationMimeData['override_mime_type']; 930 if ( isset( $destinationMimeData['info'] ) ) 931 $currentAliasData['info'] = $destinationMimeData['info']; 932 $currentAliasData['full_path'] =& $currentAliasData['url']; 933 if ( function_exists( 'getimagesize' ) ) 934 { 935 // VS-DBFILE 936 937 $fileHandler = eZClusterFileHandler::instance(); 938 $fileHandler->fileFetch( $destinationMimeData['url'] ); 939 940 if ( file_exists( $destinationMimeData['url'] ) ) 941 { 942 $info = getimagesize( $destinationMimeData['url'] ); 943 if ( $info ) 944 { 945 $width = $info[0]; 946 $height = $info[1]; 947 $currentAliasData['width'] = $width; 948 $currentAliasData['height'] = $height; 949 } 950 951 // VS-DBFILE 952 953 $fileHandler = eZClusterFileHandler::instance( $aliasFilePath ); 954 $fileHandler->fileStore( $destinationMimeData['url'], 'image', true, $destinationMimeData['name'] ); 955 } 956 else 957 { 958 eZDebug::writeError( "The destination image " . $destinationMimeData['url'] . " does not exist, cannot figure out image size", 'eZImageManager::createImageAlias' ); 959 } 960 } 961 else 962 eZDebug::writeError( "Unknown function 'getimagesize' cannot get image size", 'eZImageManager::createImageAlias' ); 963 $existingAliasList[$aliasName] = $currentAliasData; 964 // VS-DBFILE 965 $aliasFile->deleteLocal(); 966 return true; 967 } 968 } 969 return false; 970 } 971 972 /*! 973 \static 974 Analyzes the image in the MIME structure \a $mimeData and fills in extra information if found. 975 \return \c true if the image was succesfully analyzed, \c false otherwise. 976 \note It will return \c true if there is no analyzer for the image type. 977 */ 978 function analyzeImage( &$mimeData, $parameters = array() ) 979 { 980 $file = $mimeData['url']; 981 if ( !file_exists( $file ) ) 982 return false; 983 $analyzer = eZImageAnalyzer::createForMIME( $mimeData ); 984 $status = true; 985 if ( is_object( $analyzer ) ) 986 { 987 $imageInformation = $analyzer->process( $mimeData, $parameters ); 988 if ( $imageInformation ) 989 { 990 $mimeData['info'] = $imageInformation; 991 } 992 else 993 $status = false; 994 } 995 return $status; 996 } 997 998 /*! 999 Converts the source image \a $sourceMimeData into the destination image \a $destinationMimeData. 1000 The source image can be supplied with the full path to the image instead of the MIME structure. 1001 The destination image can be supplied with full path or dirpath to the destination image instead of the MIME structure. 1002 \param $aliasName determines the Image Alias to use for conversion, this usually adds some filters to the conversion. 1003 */ 1004 function convert( $sourceMimeData, &$destinationMimeData, $aliasName = false, $parameters = array() ) 1005 { 1006 // VS-DBFILE 1007 1008 require_once ( 'kernel/classes/ezclusterfilehandler.php' ); 1009 $sourceFile = eZClusterFileHandler::instance( $sourceMimeData['url'] ); 1010 $sourceFile->fetch(); 1011 1012 include_once ( 'lib/ezutils/classes/ezmimetype.php' ); 1013 if ( is_string( $sourceMimeData ) ) 1014 $sourceMimeData = eZMimeType::findByFileContents( $sourceMimeData ); 1015 $this->analyzeImage( $sourceMimeData ); 1016 $currentMimeData = $sourceMimeData; 1017 $handlers =& $this->ImageHandlers; 1018 $supportedMIMEMap = $this->SupportedMIMEMap; 1019 if ( is_string( $destinationMimeData ) ) 1020 { 1021 $destinationPath = $destinationMimeData; 1022 $destinationMimeData = eZMimeType::findByFileContents( $destinationPath ); 1023 } 1024 $filters = array(); 1025 $alias = false; 1026 if ( $aliasName ) 1027 { 1028 $aliasList = $this->aliasList(); 1029 if ( isset( $aliasList[$aliasName] ) ) 1030 { 1031 $alias = $aliasList[$aliasName]; 1032 $filters = $alias['filters']; 1033 if ( $alias['mime_type'] ) 1034 { 1035 eZMimeType::changeMIMEType( $destinationMimeData, $alias['mime_type'] ); 1036 } 1037 } 1038 } 1039 $mimeTypeOverride = $this->mimeTypeOverride( $sourceMimeData ); 1040 if ( $mimeTypeOverride ) 1041 $alias['override_mime_type'] = $mimeTypeOverride; 1042 1043 if ( isset( $parameters['filters'] ) ) 1044 { 1045 $filters = array_merge( $filters, $parameters['filters'] ); 1046 } 1047 1048 $wantedFilters = $filters; 1049 $mimeTypeFilters = $this->mimeTypeFilters( $sourceMimeData ); 1050 if ( is_array( $mimeTypeFilters ) ) 1051 $wantedFilters = array_merge( $wantedFilters, $mimeTypeFilters ); 1052 $filters = array(); 1053 foreach ( array_keys( $wantedFilters ) as $wantedFilterKey ) 1054 { 1055 $wantedFilter = $wantedFilters[$wantedFilterKey]; 1056 if ( !$this->isFilterSupported( $wantedFilter['name'] ) ) 1057 { 1058 eZDebug::writeWarning( "The filter '" . $wantedFilter['name'] . "' is not supported by any of the image handlers, will ignore this filter", 1059 'eZImageManager::convert' ); 1060 continue; 1061 } 1062 $filters[] = $wantedFilter; 1063 } 1064 if ( !$destinationMimeData['is_valid'] ) 1065 { 1066 $destinationDirPath = $destinationMimeData['dirpath']; 1067 $destinationBasename = $destinationMimeData['basename']; 1068 if ( isset( $supportedMIMEMap[$sourceMimeData['name']] ) ) 1069 { 1070 $destinationMimeData = $sourceMimeData; 1071 if ( $alias['mime_type'] ) 1072 { 1073 eZMimeType::changeMIMEType( $destinationMimeData, $alias['mime_type'] ); 1074 } 1075 eZMimeType::changeFileData( $destinationMimeData, $destinationDirPath, $destinationBasename ); 1076 } 1077 else 1078 { 1079 $hasDestination = false; 1080 foreach ( array_keys( $handlers ) as $handlerKey ) 1081 { 1082 $handler =& $handlers[$handlerKey]; 1083 $gotMimeData = true; 1084 while( $gotMimeData ) 1085 { 1086 $gotMimeData = false; 1087 $outputMimeData = $handler->outputMIMEType( $this, $sourceMimeData, false, $this->SupportedFormats, $aliasName ); 1088 if ( $outputMimeData and 1089 isset( $supportedMIMEMap[$outputMimeData['name']] ) ) 1090 { 1091 $destinationMimeData = $outputMimeData; 1092 eZMimeType::changeFileData( $destinationMimeData, $destinationDirPath, $destinationBasename ); 1093 $hasDestination = true; 1094 $gotMimeData = true; 1095 break; 1096 } 1097 } 1098 } 1099 if ( !$hasDestination ) 1100 { 1101 // VS-DBFILE 1102 $sourceFile->deleteLocal(); 1103 return false; 1104 } 1105 } 1106 } 1107 1108 $wantedFilters = $filters; 1109 $filters = array(); 1110 foreach ( array_keys( $wantedFilters ) as $wantedFilterKey ) 1111 { 1112 $wantedFilter = $wantedFilters[$wantedFilterKey]; 1113 if ( !$this->isFilterAllowed( $wantedFilter['name'], $destinationMimeData ) ) 1114 { 1115 continue; 1116 } 1117 $filters[] = $wantedFilter; 1118 } 1119 $result = true; 1120 $tempFiles = array(); 1121 if ( $currentMimeData['name'] != $destinationMimeData['name'] or 1122 count( $filters ) > 0 ) 1123 { 1124 while ( $currentMimeData['name'] != $destinationMimeData['name'] or 1125 count( $filters ) > 0 ) 1126 { 1127 $nextMimeData = false; 1128 $nextHandler = false; 1129 foreach ( array_keys( $handlers ) as $handlerKey ) 1130 { 1131 $handler =& $handlers[$handlerKey]; 1132 if ( !$handler ) 1133 continue; 1134 $outputMimeData = $handler->outputMIMEType( $this, $currentMimeData, $destinationMimeData, $this->SupportedFormats, $aliasName ); 1135 if ( $outputMimeData['name'] == $destinationMimeData['name'] ) 1136 { 1137 $nextMimeData = $outputMimeData; 1138 $nextHandler =& $handler; 1139 break; 1140 } 1141 if ( $outputMimeData and 1142 !$nextMimeData ) 1143 { 1144 $nextMimeData = $outputMimeData; 1145 $nextHandler =& $handler; 1146 } 1147 } 1148 if ( !$nextMimeData ) 1149 { 1150 eZDebug::writeError( "None of the handlers can convert MIME-Type " . $currentMimeData['name'], 1151 'eZImageManager::convert' ); 1152 // VS-DBFILE 1153 $sourceFile->deleteLocal(); 1154 return false; 1155 } 1156 $handlerFilters = array(); 1157 $leftoverFilters = array(); 1158 foreach ( $filters as $filter ) 1159 { 1160 if ( $nextHandler->isFilterSupported( $filter ) ) 1161 $handlerFilters[] = $filter; 1162 else 1163 $leftoverFilters[] = $filter; 1164 } 1165 $useTempImage = false; 1166 if ( $nextMimeData['name'] == $destinationMimeData['name'] and 1167 count( $leftoverFilters ) == 0 ) 1168 { 1169 $nextMimeData['dirpath'] = $destinationMimeData['dirpath']; 1170 } 1171 else 1172 { 1173 $useTempImage = true; 1174 $nextMimeData['dirpath'] = $this->temporaryImageDirPath(); 1175 } 1176 eZMimeType::changeDirectoryPath( $nextMimeData, $nextMimeData['dirpath'] ); 1177 1178 if ( $nextMimeData['dirpath'] and 1179 !file_exists( $nextMimeData['dirpath'] ) ) 1180 eZDir::mkdir( $nextMimeData['dirpath'], eZDir::directoryPermission(), true ); 1181 if ( $currentMimeData['name'] == $nextMimeData['name'] and 1182 count( $handlerFilters ) == 0 ) 1183 { 1184 if ( $currentMimeData['url'] != $nextMimeData['url'] ) 1185 { 1186 include_once ( 'lib/ezfile/classes/ezfilehandler.php' ); 1187 if ( eZFileHandler::copy( $currentMimeData['url'], $nextMimeData['url'] ) ) 1188 { 1189 if ( $useTempImage ) 1190 $tempFiles[] = $nextMimeData['url']; 1191 } 1192 else 1193 { 1194 $result = false; 1195 break; 1196 } 1197 } 1198 $currentMimeData = $nextMimeData; 1199 } 1200 else 1201 { 1202 if ( $nextHandler->convert( $this, $currentMimeData, $nextMimeData, $handlerFilters ) ) 1203 { 1204 if ( $useTempImage ) 1205 $tempFiles[] = $nextMimeData['url']; 1206 } 1207 else 1208 { 1209 $result = false; 1210 break; 1211 } 1212 $currentMimeData = $nextMimeData; 1213 } 1214 $filters = $leftoverFilters; 1215 } 1216 } 1217 else 1218 { 1219 $useCopy = false; 1220 if ( $aliasName and 1221 $aliasName != 'original' ) 1222 { 1223 $destinationMimeData['filename'] = $destinationMimeData['basename'] . '_' . $aliasName . '.' . $destinationMimeData['suffix']; 1224 if ( $destinationMimeData['dirpath'] ) 1225 $destinationMimeData['url'] = $destinationMimeData['dirpath'] . '/' . $destinationMimeData['filename']; 1226 else 1227 $destinationMimeData['url'] = $destinationMimeData['filename']; 1228 } 1229 if ( $sourceMimeData['url'] != $destinationMimeData['url'] ) 1230 { 1231 include_once ( 'lib/ezfile/classes/ezfilehandler.php' ); 1232 if ( $useCopy ) 1233 { 1234 eZFileHandler::copy( $sourceMimeData['url'], $destinationMimeData['url'] ); 1235 } 1236 else 1237 { 1238 eZFileHandler::linkCopy( $sourceMimeData['url'], $destinationMimeData['url'], false ); 1239 } 1240 $currentMimeData = $destinationMimeData; 1241 } 1242 } 1243 foreach ( $tempFiles as $tempFile ) 1244 { 1245 if ( !@unlink( $tempFile ) ) 1246 { 1247 eZDebug::writeError( "Failed to unlink temporary image file $tempFile", 1248 'eZImageManager::convert' ); 1249 } 1250 } 1251 $destinationMimeData = $currentMimeData; 1252 1253 // VS-DBFILE 1254 1255 if ( $aliasName && $aliasName != 'original' ) 1256 { 1257 if ( $result ) 1258 { 1259 $destinationFilePath = $destinationMimeData['url']; 1260 require_once ( 'kernel/classes/ezclusterfilehandler.php' ); 1261 $fileHandler = eZClusterFileHandler::instance(); 1262 $fileHandler->fileStore( $destinationFilePath, 'image', true, $destinationMimeData['name'] ); 1263 } 1264 1265 $sourceFile->deleteLocal(); 1266 } 1267 1268 return $result; 1269 } 1270 1271 /*! 1272 \return the path for temporary images. 1273 \note The default value uses the temporary directory setting from site.ini. 1274 */ 1275 function temporaryImageDirPath() 1276 { 1277 return $this->TemporaryImageDirPath; 1278 } 1279 1280 /*! 1281 Returns the only instance of the image manager. 1282 */ 1283 function &instance() 1284 { 1285 $instance =& $GLOBALS["eZImageManager"]; 1286 if ( get_class( $instance ) != "ezimagemanager" ) 1287 { 1288 $instance = new eZImageManager(); 1289 } 1290 return $instance; 1291 } 1292 1293 /// \privatesection 1294 var $ImageHandlers; 1295 var $OutputMIME; 1296 var $OutputMIMEMap; 1297 var $Rules; 1298 var $DefaultRule; 1299 var $RuleMap; 1300 var $MIMETypes; 1301 var $Types = array(); 1302 } 1303 1304 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |