Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezimagemanager.php
Go to the documentation of this file.
1 <?php
84 class eZImageManager
85 {
89 public function __construct()
90 {
91 $this->SupportedFormats = array();
92 $this->SupportedMIMEMap = array();
93 $this->ImageHandlers = array();
94 $this->AliasList = array();
95 $this->Factories = array();
96 $this->ImageFilters = array();
97 $this->MIMETypeSettings = array();
98 $this->MIMETypeSettingsMap = array();
99 $this->QualityValues = array();
100 $this->QualityValueMap = array();
101 $this->QualityValueMapOverride = array();
102
103 $ini = eZINI::instance( 'image.ini' );
104 $this->TemporaryImageDirPath = eZSys::cacheDirectory() . '/' . $ini->variable( 'FileSettings', 'TemporaryDir' );
105 }
106
114 function setSupportedFormats( $mimeList )
115 {
116 $this->SupportedFormats = $mimeList;
117 $this->SupportedMIMEMap = array();
118 foreach ( $mimeList as $mimeName )
119 {
120 $this->SupportedMIMEMap[$mimeName] = true;
121 }
122 }
123
131 function appendSupportedFormat( $mimeName )
132 {
133 $this->SupportedFormats[] = $mimeName;
134 $this->SupportedMIMEMap[$mimeName] = true;
135 }
136
143 function appendImageHandler( $handler )
144 {
145 if ( !$handler )
146 return false;
147 if ( !$handler->isAvailable() )
148 return false;
149 $this->ImageHandlers[] = $handler;
150 $this->ImageFilters = array_merge( $this->ImageFilters, $handler->supportedImageFilters() );
151 $this->ImageFilters = array_unique( $this->ImageFilters );
152 return true;
153 }
154
158 function isFilterSupported( $filterName )
159 {
160 return in_array( $filterName, $this->ImageFilters );
161 }
162
173 function aliasList()
174 {
175 $aliasList = $this->AliasList;
176 if ( !isset( $aliasList['original'] ) )
177 {
178 $alias = array( 'name' => 'original',
179 'reference' => false,
180 'mime_type' => false,
181 'quality' => false,
182 'filters' => array() );
183 $alias['alias_key'] = $this->createImageAliasKey( $alias );
184 $aliasList['original'] = $alias;
185 }
186 return $aliasList;
187 }
188
192 function hasAlias( $aliasName )
193 {
194 $aliasList = $this->aliasList();
195 return array_key_exists( $aliasName, $aliasList );
196 }
197
201 function alias( $aliasName )
202 {
203 $aliasList = $this->aliasList();
204 if ( !array_key_exists( $aliasName, $aliasList ) )
205 return false;
206 return $aliasList[$aliasName];
207 }
208
212 function appendImageAlias( $alias )
213 {
214 $key = $this->createImageAliasKey( $alias );
215 $alias['alias_key'] = $key;
216 $this->AliasList[$alias['name']] = $alias;
217 return $key;
218 }
219
224 function createImageAliasKey( $alias )
225 {
226 $keyData = array( $alias['name'],
227 $alias['reference'],
228 $alias['mime_type'] );
229 if ( $alias['reference'] )
230 {
231 $referenceAlias = $this->alias( $alias['reference'] );
232 if ( $referenceAlias )
233 $keyData[] = $referenceAlias['alias_key'];
234 }
235 foreach ( $alias['filters'] as $filter )
236 {
237 $filterData = $filter['name'];
238 if ( is_array( $filter['data'] ) )
239 $filterData .= '=' . implode( ',', $filter['data'] );
240 $keyData[] = $filterData;
241 }
242
243 $key = eZSys::ezcrc32( implode( "\n", $keyData ) );
244
245 return $key;
246 }
247
252 function isImageAliasValid( $alias )
253 {
254 $aliasName = $alias['name'];
255 if ( isset( $this->AliasList[$aliasName] ) )
256 {
257 $aliasKey = $alias['alias_key'];
258 $aliasInfo = $this->AliasList[$aliasName];
259 $checkKey = $aliasInfo['alias_key'];
260 $isValid = ( $aliasKey == $checkKey );
261 if ( $isValid )
262 {
263 $aliasTimestamp = $alias['timestamp'];
264 $isValid = $this->isImageTimestampValid( $aliasTimestamp );
265 }
266 return $isValid;
267 }
268 return false;
269 }
270
277 function isImageTimestampValid( $timestamp )
278 {
279 $expiryHandler = eZExpiryHandler::instance();
280 if ( $expiryHandler->hasTimestamp( 'image-manager-alias' ) )
281 {
282 $aliasTimestamp = $expiryHandler->timestamp( 'image-manager-alias' );
283 return ( $timestamp > $aliasTimestamp );
284 }
285 return true;
286 }
287
293 function readImageAliasesFromINI( $iniFile = false )
294 {
295 if ( !$iniFile )
296 $iniFile = 'image.ini';
297 $ini = eZINI::instance( $iniFile );
298 if ( !$ini )
299 return false;
300 $aliasNames = $ini->variable( 'AliasSettings', 'AliasList' );
301 foreach ( $aliasNames as $aliasName )
302 {
303 $alias = $this->createAliasFromINI( $aliasName );
304 if ( $alias )
305 {
306 $this->appendImageAlias( $alias );
307 }
308 else
309 eZDebug::writeWarning( "Failed reading Image Alias $aliasName from $iniFile", __METHOD__ );
310 }
311 $aliasName = 'original';
312 if ( !in_array( $aliasName, $aliasNames ) )
313 {
314 $ini = eZINI::instance( 'image.ini' );
315 if ( $ini->hasGroup( $aliasName ) )
316 {
317 $alias = $this->createAliasFromINI( $aliasName );
318 if ( $alias )
319 {
320 $alias['reference'] = false;
321 $this->appendImageAlias( $alias );
322 }
323 else
324 {
325 eZDebug::writeWarning( "Failed reading Image Alias $aliasName from $iniFile", __METHOD__ );
326 }
327 }
328 }
329 }
330
336 function readSupportedFormatsFromINI( $iniFile = false )
337 {
338 if ( !$iniFile )
339 $iniFile = 'image.ini';
340 $ini = eZINI::instance( $iniFile );
341 if ( !$ini )
342 return false;
343 $allowedOutputFormats = $ini->variable( 'OutputSettings', 'AllowedOutputFormat' );
344 foreach ( $allowedOutputFormats as $allowedOutputFormat )
345 {
346 $this->appendSupportedFormat( $allowedOutputFormat );
347 }
348 }
349
353 function hasMIMETypeSetting( $mimeData )
354 {
355 return isset( $this->MIMETypeSettingsMap[$mimeData['name']] );
356 }
357
361 function mimeTypeSetting( $mimeData )
362 {
363 if ( !isset( $this->MIMETypeSettingsMap[$mimeData['name']] ) )
364 return false;
365 $list = $this->MIMETypeSettingsMap[$mimeData['name']];
366 foreach ( $list as $item )
367 {
368 if ( is_array( $item['match'] ) )
369 {
370 if ( array_key_exists( 'info', $mimeData ) && is_array( $mimeData['info'] ) )
371 {
372 $isMatch = true;
373 $info = $mimeData['info'];
374 foreach ( $item['match'] as $matchKey => $matchValue )
375 {
376 if ( !isset( $info[$matchKey] ) or
377 $info[$matchKey] != $matchValue )
378 {
379 $isMatch = false;
380 break;
381 }
382 }
383 if ( $isMatch )
384 return $item;
385 }
386 }
387 else
388 return $item;
389 }
390 return false;
391 }
392
397 static function wildcardToRegexp( $wildcard, $separatorCharacter = false )
398 {
399 return eZImageHandler::wildcardToRegexp( $wildcard, $separatorCharacter );
400 }
401
405 function mimeTypeOverride( $mimeData )
406 {
407 if ( $this->hasMIMETypeSetting( $mimeData ) )
408 {
409 $settings = $this->mimeTypeSetting( $mimeData );
410 if ( $settings )
411 {
412 return $settings['override_mime_type'];
413 }
414 }
415 return false;
416 }
417
422 function mimeTypeFilters( $mimeData )
423 {
424 if ( $this->hasMIMETypeSetting( $mimeData ) )
425 {
426 $settings = $this->mimeTypeSetting( $mimeData );
427 if ( $settings )
428 {
429 return $settings['extra_filters'];
430 }
431 }
432 return false;
433 }
434
438 function isFilterAllowed( $filterName, $mimeData )
439 {
440 if ( $this->hasMIMETypeSetting( $mimeData ) )
441 {
442 $settings = $this->mimeTypeSetting( $mimeData );
443 if ( $settings )
444 {
445 if ( is_array( $settings['disallowed_filters'] ) )
446 {
447 foreach ( $settings['disallowed_filters'] as $filter )
448 {
449 $regexp = eZImageManager::wildcardToRegexp( $filter );
450 if ( preg_match( '#' . $regexp . '#', $filterName ) )
451 {
452 return false;
453 }
454 }
455 }
456 if ( is_array( $settings['allowed_filters'] ) )
457 {
458 foreach ( $settings['allowed_filters'] as $filter )
459 {
460 $regexp = eZImageManager::wildcardToRegexp( $filter );
461 if ( preg_match( '#' . $regexp . '#', $filterName ) )
462 {
463 return true;
464 }
465 }
466 return false;
467 }
468 return true;
469 }
470 }
471 return true;
472 }
473
477 function appendQualityValue( $mimeType, $qualityValue )
478 {
479 $element = array( 'name' => $mimeType,
480 'value' => $qualityValue );
481 $this->QualityValues[] = $element;
482 $this->QualityValueMap[$mimeType] = $element;
483 }
484
488 function qualityValue( $mimeType )
489 {
490 if ( isset( $this->QualityValueMapOverride[$mimeType] ) )
491 return $this->QualityValueMapOverride[$mimeType]['value'];
492 else if ( isset( $this->QualityValueMap[$mimeType] ) )
493 return $this->QualityValueMap[$mimeType]['value'];
494 return false;
495 }
496
500 function setQualityValueOverride( $mimeType, $qualityValue )
501 {
502 $element = array( 'name' => $mimeType,
503 'value' => $qualityValue );
504 $this->QualityValueMapOverride[$mimeType] = $element;
505 }
506
510 function resetQualityValueOverride()
511 {
512 $this->QualityValueMapOverride = array();
513 }
514
518 function appendMIMETypeSetting( $settings )
519 {
520 $this->MIMETypeSettings[] = $settings;
521 if ( !isset( $this->MIMETypeSettingsMap[$settings['mime_type']] ) )
522 $this->MIMETypeSettingsMap[$settings['mime_type']] = array();
523 $this->MIMETypeSettingsMap[$settings['mime_type']][] = $settings;
524 }
525
531 function readMIMETypeSettingsFromINI( $iniFile = false )
532 {
533 if ( !$iniFile )
534 $iniFile = 'image.ini';
535 $ini = eZINI::instance( $iniFile );
536 if ( !$ini )
537 return false;
538 $overrideList = $ini->variable( 'MIMETypeSettings', 'OverrideList' );
539 foreach ( $overrideList as $mimeType )
540 {
541 $settings = eZImageManager::readMIMETypeSettingFromINI( $mimeType );
542 if ( $settings )
543 $this->appendMIMETypeSetting( $settings );
544 }
545 }
546
550 function readMIMETypeQualitySettingFromINI( $iniFile = false )
551 {
552 if ( !$iniFile )
553 $iniFile = 'image.ini';
554 $ini = eZINI::instance( $iniFile );
555 if ( !$ini )
556 return false;
557 if ( !$ini->hasVariable( 'MIMETypeSettings', 'Quality' ) )
558 return false;
559 $values = $ini->variable( 'MIMETypeSettings', 'Quality' );
560 foreach ( $values as $value )
561 {
562 $elements = explode( ';', $value );
563 $mimeType = $elements[0];
564 $qualityValue = $elements[1];
565 $this->appendQualityValue( $mimeType, $qualityValue );
566 }
567 }
568
572 function readConversionRuleSettingsFromINI( $iniFile = false )
573 {
574 if ( !$iniFile )
575 $iniFile = 'image.ini';
576 $ini = eZINI::instance( $iniFile );
577 if ( !$ini )
578 return false;
579 if ( $ini->hasVariable( 'MIMETypeSettings', 'ConversionRules' ) )
580 {
581 $conversionRules = array();
582 $rules = $ini->variable( 'MIMETypeSettings', 'ConversionRules' );
583 foreach ( $rules as $ruleString )
584 {
585 $ruleItems = explode( ';', $ruleString );
586 if ( count( $ruleItems ) >= 2 )
587 {
588 $conversionRule = array( 'from' => $ruleItems[0],
589 'to' => $ruleItems[1] );
590 $this->appendConversionRule( $conversionRule );
591 }
592 }
593 }
594 }
595
599 function readINISettings()
600 {
601 $this->readImageHandlersFromINI();
602 $this->readSupportedFormatsFromINI();
603 $this->readImageAliasesFromINI();
604 $this->readMIMETypeSettingsFromINI();
605 $this->readMIMETypeQualitySettingFromINI();
606 $this->readConversionRuleSettingsFromINI();
607 }
608
612 function appendConversionRule( $conversionRule )
613 {
614 $this->ConversionRules[] = $conversionRule;
615 }
616
620 function conversionRules()
621 {
622 return $this->ConversionRules;
623 }
624
632 function readMIMETypeSettingFromINI( $mimeGroup, $iniFile = false )
633 {
634 if ( !$iniFile )
635 $iniFile = 'image.ini';
636 $ini = eZINI::instance( $iniFile );
637 if ( !$ini )
638 return false;
639 if ( !$ini->hasGroup( $mimeGroup ) )
640 return false;
641 if ( !$ini->hasVariable( $mimeGroup, 'MIMEType' ) )
642 return false;
643 $settings = array( 'name' => $mimeGroup,
644 'match' => false,
645 'mime_type' => false,
646 'override_mime_type' => false,
647 'allowed_filters' => false,
648 'disallowed_filters' => false,
649 'extra_filters' => false );
650 $settings['mime_type'] = $ini->variable( $mimeGroup, 'MIMEType' );
651 $ini->assign( $mimeGroup, 'Match', $settings['match'] );
652 $ini->assign( $mimeGroup, 'OverrideMIMEType', $settings['override_mime_type'] );
653 $ini->assign( $mimeGroup, 'AllowedFilters', $settings['allowed_filters'] );
654 $ini->assign( $mimeGroup, 'DisallowedFilters', $settings['disallowed_filters'] );
655 if ( $ini->hasVariable( $mimeGroup, 'ExtraFilters' ) )
656 {
657 $filters = array();
658 $filterRawList = $ini->variable( $mimeGroup, 'ExtraFilters' );
659 foreach ( $filterRawList as $filterRawItem )
660 {
661 $filters[] = $this->createFilterDataFromINI( $filterRawItem );
662 }
663 if ( count( $filters ) > 0 )
664 $settings['extra_filters'] = $filters;
665 }
666 return $settings;
667 }
668
674 function readImageHandlersFromINI( $iniFile = false )
675 {
676 if ( !$iniFile )
677 $iniFile = 'image.ini';
678 $ini = eZINI::instance( $iniFile );
679 if ( !$ini )
680 return false;
681
682 $converterList = $ini->variable( 'ImageConverterSettings', 'ImageConverters' );
683 foreach ( $converterList as $converterName )
684 {
685 if ( $ini->hasGroup( $converterName ) )
686 {
687 if ( $ini->hasVariable( $converterName, 'Handler' ) )
688 {
689 $factoryName = $ini->variable( $converterName, 'Handler' );
690 $factory = $this->factoryFor( $factoryName, $iniFile, $converterName );
691 if ( $factory )
692 {
693 $convertHandler = $factory->produceFromINI( $converterName, $iniFile );
694 $this->appendImageHandler( $convertHandler );
695 }
696 }
697 else
698 {
699 eZDebug::writeWarning( "INI group $converterName does not have a Handler setting, cannot instantiate handler without it", __METHOD__ );
700 }
701 }
702 else
703 {
704 eZDebug::writeWarning( "No INI group $converterName for Image Handler $converterName, cannot instantiate", __METHOD__ );
705 }
706 }
707 }
708
713 function factoryFor( $factoryName, $iniFile = false, $converterName = false )
714 {
715 if ( !$iniFile )
716 $iniFile = 'image.ini';
717 if ( isset( $this->Factories[$factoryName] ) )
718 {
719 return $this->Factories[$factoryName];
720 }
721 else
722 {
723 $optionArray = array( 'iniFile' => $iniFile,
724 'iniSection' => $converterName,
725 'iniVariable' => 'Handler' );
726
727 $options = new ezpExtensionOptions( $optionArray );
728
729 $factory = eZExtension::getHandlerClass( $options );
730
731 return $this->Factories[$factoryName] = $factory;
732 }
733
734 return false;
735 }
736
741 function createFilterDataFromINI( $filterText )
742 {
743 $equalPosition = strpos( $filterText, '=' );
744 $filterData = false;
745 if ( $equalPosition !== false )
746 {
747 $filterName = substr( $filterText, 0, $equalPosition );
748 $filterDataText = substr( $filterText, $equalPosition + 1 );
749 $filterData = explode( ';', $filterDataText );
750 }
751 else
752 $filterName = $filterText;
753 return array( 'name' => $filterName,
754 'data' => $filterData );
755 }
756
761 function createAliasFromINI( $iniGroup )
762 {
763 $ini = eZINI::instance( 'image.ini' );
764 if ( !$ini->hasGroup( $iniGroup ) )
765 {
766 eZDebug::writeError( "No such group $iniGroup in ini file image.ini", __METHOD__ );
767 return false;
768 }
769 $alias = array( 'name' => $iniGroup,
770 'reference' => false,
771 'mime_type' => false,
772 'quality' => false,
773 'filters' => array() );
774 if ( $ini->hasVariable( $iniGroup, 'Name' ) )
775 $alias['name'] = $ini->variable( $iniGroup, 'Name' );
776 if ( $ini->hasVariable( $iniGroup, 'MIMEType' ) )
777 $alias['mime_type'] = $ini->variable( $iniGroup, 'MIMEType' );
778 if ( $ini->hasVariable( $iniGroup, 'Quality' ) )
779 $alias['quality'] = $ini->variable( $iniGroup, 'Quality' );
780 if ( $ini->hasVariable( $iniGroup, 'Filters' ) )
781 {
782 $filters = array();
783 $filterRawList = $ini->variable( $iniGroup, 'Filters' );
784 foreach ( $filterRawList as $filterRawItem )
785 {
786 $filters[] = $this->createFilterDataFromINI( $filterRawItem );
787 }
788 $alias['filters'] = $filters;
789 }
790 if ( $ini->hasVariable( $iniGroup, 'Reference' ) )
791 {
792 $alias['reference'] = trim( $ini->variable( $iniGroup, 'Reference' ) );
793 $alias['reference'] = ( $alias['reference'] !== '' ) ? $alias['reference'] : false;
794 }
795 return $alias;
796 }
797
810 function createImageAlias( $aliasName, &$existingAliasList, $parameters = array() )
811 {
812 // check for $aliasName validity
813 $aliasList = $this->aliasList();
814 if ( !isset( $aliasList[$aliasName] ) )
815 {
816 eZDebug::writeWarning( "Alias name $aliasName does not exist, cannot create it" );
817 return false;
818 }
819
820 // check if the reference alias is defined, and if no, use original as ref
821 $currentAliasInfo = $aliasList[$aliasName];
822 $referenceAlias = $currentAliasInfo['reference'];
823 if ( $referenceAlias and !$this->hasAlias( $referenceAlias ) )
824 {
825 eZDebug::writeError( "The referenced alias '$referenceAlias' for image alias '$aliasName' does not exist, cannot use it for reference.\n" .
826 "Will use 'original' alias instead.",
827 __METHOD__ );
828 $referenceAlias = false;
829 }
830 if ( !$referenceAlias )
831 $referenceAlias = 'original';
832
833 // generate the reference alias if it hasn't been generated yet
834 $hasReference = false;
835 if ( array_key_exists( $referenceAlias, $existingAliasList ) )
836 {
837 $fileHandler = eZClusterFileHandler::instance();
838 if ( $fileHandler->fileExists( $existingAliasList[$referenceAlias]['url'] ) )
839 {
840 $hasReference = true;
841 }
842 else
843 {
844 eZDebug::writeError( "The reference alias $referenceAlias file {$existingAliasList[$referenceAlias]['url']} does not exist", __METHOD__ );
845 eZDebug::writeError( "Error ocurred using URI: " . $_SERVER['REQUEST_URI'] , __METHOD__ );
846 }
847 }
848 if ( !$hasReference )
849 {
850 if ( $referenceAlias == 'original' )
851 {
852 eZDebug::writeError( "Original alias does not exist, cannot create other aliases without it" );
853 return false;
854 }
855 if ( !$this->createImageAlias( $referenceAlias, $existingAliasList, $parameters ) )
856 {
857 eZDebug::writeError( "Failed creating the referenced alias $referenceAlias, cannot create alias $aliasName", __METHOD__ );
858 return false;
859 }
860 }
861
862 // from now on, our reference image (either reference or original)
863 // exists
864 $aliasInfo = $existingAliasList[$referenceAlias];
865 $aliasFilePath = $aliasInfo['url'];
866 $aliasKey = $currentAliasInfo['alias_key'];
867
868 $sourceMimeData = eZMimeType::findByFileContents( $aliasFilePath );
869
875 $destinationMimeData = $sourceMimeData;
876 if ( isset( $parameters['basename'] ) )
877 {
878 $sourceMimeData['basename'] = $parameters['basename'];
879 eZMimeType::changeBasename( $destinationMimeData, $parameters['basename'] );
880 }
881
889 while ( true )
890 {
891 $convertHandler = eZClusterFileHandler::instance( $sourceMimeData['url'] );
892 $startGeneration = $convertHandler->startCacheGeneration();
893 if ( $startGeneration === true )
894 {
895 $destinationMimeData['is_valid'] = false;
896 if ( $this->convert( $sourceMimeData, $destinationMimeData, $aliasName, $parameters ) )
897 {
908 $currentAliasData = array( 'url' => $destinationMimeData['url'],
909 'dirpath' => $destinationMimeData['dirpath'],
910 'filename' => $destinationMimeData['filename'],
911 'suffix' => $destinationMimeData['suffix'],
912 'basename' => $destinationMimeData['basename'],
913 'alternative_text' => $aliasInfo['alternative_text'],
914 'name' => $aliasName,
915 'sub_type' => false,
916 'timestamp' => time(),
917 'alias_key' => $aliasKey,
918 'mime_type' => $destinationMimeData['name'],
919 'override_mime_type' => false,
920 'info' => false,
921 'width' => false,
922 'height' => false,
923 'is_valid' => true,
924 'is_new' => true );
925
926 if ( isset( $destinationMimeData['override_mime_type'] ) )
927 $currentAliasData['override_mime_type'] = $destinationMimeData['override_mime_type'];
928 if ( isset( $destinationMimeData['info'] ) )
929 $currentAliasData['info'] = $destinationMimeData['info'];
930 $currentAliasData['full_path'] = $currentAliasData['url'];
931
932 if ( function_exists( 'getimagesize' ) )
933 {
938 $fileHandler = eZClusterFileHandler::instance( $destinationMimeData['url'] );
939 if ( $tempPath = $fileHandler->fetchUnique() )
940 {
941 $info = getimagesize( $tempPath );
942 if ( $info )
943 {
944 list( $currentAliasData['width'], $currentAliasData['height'] ) = $info;
945 }
946 else
947 {
948 eZDebug::writeError("The size of the generated image {$destinationMimeData['url']} could not be read by getimagesize()", __METHOD__ );
949 }
950 $fileHandler->fileDeleteLocal( $tempPath );
951 }
952 else
953 {
954 eZDebug::writeError( "The destination image {$destinationMimeData['url']} does not exist, cannot figure out image size", __METHOD__ );
955 }
956 }
957 else
958 {
959 eZDebug::writeError( "Unknown function 'getimagesize', cannot get image size", __METHOD__ );
960 }
961 $existingAliasList[$aliasName] = $currentAliasData;
962
963 $convertHandler->endCacheGeneration( false );
964
965 // Notify about image alias generation. Parameters are alias
966 // url and alias name.
967 ezpEvent::getInstance()->notify( 'image/alias', array( $currentAliasData['url'],
968 $currentAliasData['name'] ) );
969
970 return true;
971 }
972 // conversion failed, we abort generation
973 else
974 {
975 $sourceFile = $sourceMimeData['url'];
976 $destinationDir = $destinationMimeData['dirpath'];
977 eZDebug::writeError( "Failed converting $sourceFile to alias '$aliasName' in directory '$destinationDir'", __METHOD__ );
978 $convertHandler->abortCacheGeneration();
979 return false;
980 }
981 }
982 // we were not granted file generation (someone else is doing it)
983 // we wait for max. $remainingGenerationTime and check if the
984 // file has been generated in between
985 // Actually, we have no clue if the generated file was the one we were
986 // looking for, and it doesn't seem possible to RELOAD the alias list.
987 // We don't even know what attribute we're using... CRAP
988 else
989 {
990 eZDebug::writeDebug( "An alias is already being generated for this image, let's wait", __METHOD__ );
991 while ( true )
992 {
993 $startGeneration = $convertHandler->startCacheGeneration();
994 // generation lock granted: we can start again by breaking to
995 // the beggining of the while loop
996 if ( $startGeneration === true )
997 {
998 eZDebug::writeDebug( "Got granted generation permission, restarting !", __METHOD__ );
999 $convertHandler->abortCacheGeneration();
1000 continue 2;
1001 }
1002 else
1003 {
1004 sleep( 1 );
1005 }
1006 }
1007 }
1008 }
1009 return false;
1010 }
1011
1018 static function analyzeImage( &$mimeData, $parameters = array() )
1019 {
1020 $file = $mimeData['url'];
1021
1022 if ( !file_exists( $file ) )
1023 return false;
1024 $analyzer = eZImageAnalyzer::createForMIME( $mimeData );
1025 $status = true;
1026 if ( is_object( $analyzer ) )
1027 {
1028 $imageInformation = $analyzer->process( $mimeData, $parameters );
1029 if ( $imageInformation )
1030 {
1031 $mimeData['info'] = $imageInformation;
1032 }
1033 else
1034 $status = false;
1035 }
1036 return $status;
1037 }
1038
1053 function convert( $sourceMimeData, &$destinationMimeData, $aliasName = false, $parameters = array() )
1054 {
1055 // if the local file doesn't exist, we need to fetch it locally
1056 if ( is_array( $sourceMimeData ) && isset( $sourceMimeData['url'] ) && !file_exists( $sourceMimeData['url'] ) )
1057 {
1058 $sourceFileHandler = eZClusterFileHandler::instance( $sourceMimeData['url'] );
1059 $sourceFileHandler->fetch();
1060 }
1061
1062 if ( is_string( $sourceMimeData ) )
1063 $sourceMimeData = eZMimeType::findByFileContents( $sourceMimeData );
1064
1065 $this->analyzeImage( $sourceMimeData );
1066 $currentMimeData = $sourceMimeData;
1067 $handlers = $this->ImageHandlers;
1068 $supportedMIMEMap = $this->SupportedMIMEMap;
1069 if ( is_string( $destinationMimeData ) )
1070 {
1071 $destinationPath = $destinationMimeData;
1072 $destinationMimeData = eZMimeType::findByFileContents( $destinationPath );
1073 }
1074
1075 $filters = array();
1076 $alias = false;
1077 $this->resetQualityValueOverride();
1078 if ( $aliasName )
1079 {
1080 $aliasList = $this->aliasList();
1081 if ( isset( $aliasList[$aliasName] ) )
1082 {
1083 $alias = $aliasList[$aliasName];
1084 $filters = $alias['filters'];
1085 if ( $alias['mime_type'] )
1086 {
1087 eZMimeType::changeMIMEType( $destinationMimeData, $alias['mime_type'] );
1088 }
1089 $qualityOverride = $alias['quality'];
1090 if ( $qualityOverride )
1091 {
1092 foreach ( $qualityOverride as $qualityOverrideValue )
1093 {
1094 $elements = explode( ';', $qualityOverrideValue );
1095 $mimeType = $elements[0];
1096 $qualityValue = $elements[1];
1097 $this->setQualityValueOverride( $mimeType, $qualityValue );
1098 }
1099 }
1100 }
1101 }
1102 $mimeTypeOverride = $this->mimeTypeOverride( $sourceMimeData );
1103 if ( $mimeTypeOverride )
1104 $alias['override_mime_type'] = $mimeTypeOverride;
1105
1106 if ( isset( $parameters['filters'] ) )
1107 {
1108 $filters = array_merge( $filters, $parameters['filters'] );
1109 }
1110
1111 $wantedFilters = $filters;
1112 $mimeTypeFilters = $this->mimeTypeFilters( $sourceMimeData );
1113 if ( is_array( $mimeTypeFilters ) )
1114 $wantedFilters = array_merge( $wantedFilters, $mimeTypeFilters );
1115 $filters = array();
1116 foreach ( array_keys( $wantedFilters ) as $wantedFilterKey )
1117 {
1118 $wantedFilter = $wantedFilters[$wantedFilterKey];
1119 if ( !$this->isFilterSupported( $wantedFilter['name'] ) )
1120 {
1121 eZDebug::writeWarning( "The filter '" . $wantedFilter['name'] . "' is not supported by any of the image handlers, will ignore this filter", __METHOD__ );
1122 continue;
1123 }
1124 $filters[] = $wantedFilter;
1125 }
1126 if ( !$destinationMimeData['is_valid'] )
1127 {
1128 $destinationDirPath = $destinationMimeData['dirpath'];
1129 $destinationBasename = $destinationMimeData['basename'];
1130 if ( isset( $supportedMIMEMap[$sourceMimeData['name']] ) )
1131 {
1132 $destinationMimeData = $sourceMimeData;
1133 if ( $alias['mime_type'] )
1134 {
1135 eZMimeType::changeMIMEType( $destinationMimeData, $alias['mime_type'] );
1136 }
1137 eZMimeType::changeFileData( $destinationMimeData, $destinationDirPath, $destinationBasename );
1138 }
1139 else
1140 {
1141 $hasDestination = false;
1142 foreach ( $handlers as $handler )
1143 {
1144 $gotMimeData = true;
1145 while( $gotMimeData )
1146 {
1147 $gotMimeData = false;
1148 $outputMimeData = $handler->outputMIMEType( $this, $sourceMimeData, false, $this->SupportedFormats, $aliasName );
1149 if ( $outputMimeData and
1150 isset( $supportedMIMEMap[$outputMimeData['name']] ) )
1151 {
1152 $destinationMimeData = $outputMimeData;
1153 eZMimeType::changeFileData( $destinationMimeData, $destinationDirPath, $destinationBasename );
1154 $hasDestination = true;
1155 $gotMimeData = true;
1156 break;
1157 }
1158 }
1159 }
1160 if ( !$hasDestination )
1161 {
1162 if ( isset( $sourceFileHandler ) )
1163 $sourceFileHandler->deleteLocal();
1164 return false;
1165 }
1166 }
1167 }
1168
1169 $wantedFilters = $filters;
1170 $filters = array();
1171 foreach ( array_keys( $wantedFilters ) as $wantedFilterKey )
1172 {
1173 $wantedFilter = $wantedFilters[$wantedFilterKey];
1174 if ( !$this->isFilterAllowed( $wantedFilter['name'], $destinationMimeData ) )
1175 {
1176 continue;
1177 }
1178 $filters[] = $wantedFilter;
1179 }
1180 $result = true;
1181 $tempFiles = array();
1182 if ( $currentMimeData['name'] != $destinationMimeData['name'] or
1183 count( $filters ) > 0 )
1184 {
1185 while ( $currentMimeData['name'] != $destinationMimeData['name'] or
1186 count( $filters ) > 0 )
1187 {
1188 $nextMimeData = false;
1189 $nextHandler = false;
1190 foreach ( $handlers as $handler )
1191 {
1192 if ( !$handler )
1193 continue;
1194
1195 $handlerFilters = array();
1196 $leftoverFilters = array();
1197 foreach ( $filters as $filter )
1198 {
1199 if ( $handler->isFilterSupported( $filter ) )
1200 $handlerFilters[] = $filter;
1201 else
1202 $leftoverFilters[] = $filter;
1203 }
1204
1205 $outputMimeData = $handler->outputMIMEType( $this, $currentMimeData, $destinationMimeData, $this->SupportedFormats, $aliasName );
1206 if ( $outputMimeData['name'] == $destinationMimeData['name'] and count( $handlerFilters ) > 0 )
1207 {
1208 $nextMimeData = $outputMimeData;
1209 $nextHandler = $handler;
1210 break;
1211 }
1212 if ( $outputMimeData and
1213 !$nextMimeData )
1214 {
1215 $nextMimeData = $outputMimeData;
1216 $nextHandler = $handler;
1217 }
1218 }
1219 if ( !$nextMimeData )
1220 {
1221 eZDebug::writeError( "None of the handlers can convert MIME-Type " . $currentMimeData['name'], __METHOD__ );
1222 if ( isset( $sourceFile ) )
1223 $sourceFile->deleteLocal();
1224 return false;
1225 }
1226
1227 $useTempImage = false;
1228 if ( $nextMimeData['name'] == $destinationMimeData['name'] and
1229 count( $leftoverFilters ) == 0 )
1230 {
1231 $nextMimeData['dirpath'] = $destinationMimeData['dirpath'];
1232 }
1233 else
1234 {
1235 $useTempImage = true;
1236 $nextMimeData['dirpath'] = $this->temporaryImageDirPath();
1237 }
1238 eZMimeType::changeDirectoryPath( $nextMimeData, $nextMimeData['dirpath'] );
1239
1240 if ( $nextMimeData['dirpath'] and
1241 !file_exists( $nextMimeData['dirpath'] ) )
1242 eZDir::mkdir( $nextMimeData['dirpath'], false, true );
1243 if ( $currentMimeData['name'] == $nextMimeData['name'] and
1244 count( $handlerFilters ) == 0 )
1245 {
1246 if ( $currentMimeData['url'] != $nextMimeData['url'] )
1247 {
1248 if ( eZFileHandler::copy( $currentMimeData['url'], $nextMimeData['url'] ) )
1249 {
1250 if ( $useTempImage )
1251 $tempFiles[] = $nextMimeData['url'];
1252 }
1253 else
1254 {
1255 $result = false;
1256 break;
1257 }
1258 }
1259 $currentMimeData = $nextMimeData;
1260 }
1261 else
1262 {
1263 if ( $nextHandler->convert( $this, $currentMimeData, $nextMimeData, $handlerFilters ) )
1264 {
1265 if ( $useTempImage )
1266 $tempFiles[] = $nextMimeData['url'];
1267 }
1268 else
1269 {
1270 $result = false;
1271 break;
1272 }
1273 // store the converted file to cluster if the conversion is between mime name
1274 $fileHandler = eZClusterFileHandler::instance();
1275 $fileHandler->fileStore( $nextMimeData['url'], 'image', false, $nextMimeData['name'] );
1276
1277 $currentMimeData = $nextMimeData;
1278 }
1279 $filters = $leftoverFilters;
1280 }
1281 }
1282 else
1283 {
1284 $useCopy = false;
1285 if ( $aliasName and
1286 $aliasName != 'original' )
1287 {
1288 $destinationMimeData['filename'] = $destinationMimeData['basename'] . '_' . $aliasName . '.' . $destinationMimeData['suffix'];
1289 if ( $destinationMimeData['dirpath'] )
1290 $destinationMimeData['url'] = $destinationMimeData['dirpath'] . '/' . $destinationMimeData['filename'];
1291 else
1292 $destinationMimeData['url'] = $destinationMimeData['filename'];
1293 }
1294 if ( $sourceMimeData['url'] != $destinationMimeData['url'] )
1295 {
1296 if ( $useCopy )
1297 {
1298 eZFileHandler::copy( $sourceMimeData['url'], $destinationMimeData['url'] );
1299 }
1300 else
1301 {
1302 eZFileHandler::linkCopy( $sourceMimeData['url'], $destinationMimeData['url'], false );
1303 }
1304 $currentMimeData = $destinationMimeData;
1305 }
1306 }
1307 foreach ( $tempFiles as $tempFile )
1308 {
1309 if ( !@unlink( $tempFile ) )
1310 {
1311 eZDebug::writeError( "Failed to unlink temporary image file $tempFile", __METHOD__ );
1312 }
1313 }
1314 $destinationMimeData = $currentMimeData;
1315
1316 if ( $aliasName && $aliasName != 'original' )
1317 {
1318 if ( $result )
1319 {
1320 $destinationFilePath = $destinationMimeData['url'];
1321 $fileHandler = eZClusterFileHandler::instance();
1322 $fileHandler->fileStore( $destinationFilePath, 'image', true, $destinationMimeData['name'] );
1323 }
1324
1325 if ( isset( $sourceFileHandler ) )
1326 $sourceFileHandler->deleteLocal();
1327 }
1328
1329 return $result;
1330 }
1331
1341 function imageAliasInfo( $mimeData, $aliasName, $isAliasNew = false )
1342 {
1343 if ( is_string( $mimeData ) )
1344 $mimeData = eZMimeType::findByFileContents( $mimeData );
1345
1346 $this->analyzeImage( $mimeData );
1347 if ( $aliasName )
1348 {
1349 $aliasList = $this->aliasList();
1350 if ( isset( $aliasList[$aliasName] ) )
1351 {
1352 $alias = $aliasList[$aliasName];
1353 if ( $alias['mime_type'] )
1354 {
1355 eZMimeType::changeMIMEType( $mimeData, $alias['mime_type'] );
1356 }
1357 }
1358 }
1359 if ( $aliasName != 'original' )
1360 {
1361 $mimeData['filename'] = $mimeData['basename'] . '_' . $aliasName . '.' . $mimeData['suffix'];
1362 $mimeData['url'] = $mimeData['dirpath'] . '/' . $mimeData['filename'];
1363 }
1364 // eZDebug::writeDebug( $mimeData, __METHOD__ );
1365 return $mimeData;
1366 }
1367
1372 function temporaryImageDirPath()
1373 {
1374 return $this->TemporaryImageDirPath;
1375 }
1376
1383 static function instance()
1384 {
1385 if ( self::$instance === null )
1386 {
1387 self::$instance = new self();
1388 }
1389 return self::$instance;
1390 }
1391
1399 static function factory()
1400 {
1401 if ( self::$factory === false )
1402 {
1403 self::$factory = true;
1404 self::instance()->readINISettings();
1405 eZImageAnalyzer::readAnalyzerSettingsFromINI();
1406 }
1407 return self::instance();
1408 }
1409
1416 static function resetInstance()
1417 {
1418 self::$instance = null;
1419 self::$factory = false;
1420
1421 }
1422
1424 public $ImageHandlers;
1425 public $OutputMIME;
1426 public $OutputMIMEMap;
1427 public $Rules;
1428 public $DefaultRule;
1429 public $RuleMap;
1430 public $MIMETypes;
1431 public $Types = array();
1432 public $SupportedFormats;
1433 public $SupportedMIMEMap;
1434 public $AliasList;
1435 public $Factories;
1436 public $ImageFilters;
1437 public $MIMETypeSettings;
1438 public $MIMETypeSettingsMap;
1439 public $QualityValues;
1440 public $QualityValueMap;
1441 public $QualityValueMapOverride;
1442 public $TemporaryImageDirPath;
1443 public $ConversionRules;
1444
1445
1452 protected static $instance;
1453
1460 protected static $factory = false;
1461 }
1462 ?>
eZClusterFileHandler\instance
static instance( $filename=false)
Definition: ezclusterfilehandler.php:21
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZDebug\writeDebug
static writeDebug( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:588
eZDebug\writeWarning
static writeWarning( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:502
eZDir\mkdir
static mkdir( $dir, $perm=false, $recursive=false)
Definition: ezdir.php:73
eZExtension\getHandlerClass
static getHandlerClass(ezpExtensionOptions $options)
Definition: ezextension.php:531
eZFileHandler\linkCopy
static linkCopy( $sourceFilename, $destinationFilename, $symbolicLink=true)
Definition: ezfilehandler.php:147
eZFileHandler\copy
static copy( $sourceFilename, $destinationFilename)
Definition: ezfilehandler.php:356
eZImageAnalyzer\createForMIME
static createForMIME( $mimeData)
Definition: ezimageanalyzer.php:53
eZImageAnalyzer\readAnalyzerSettingsFromINI
static readAnalyzerSettingsFromINI()
Definition: ezimageanalyzer.php:133
eZImageHandler\wildcardToRegexp
static wildcardToRegexp( $wildcard, $separatorCharacter=false)
Definition: ezimagehandler.php:277
eZImageManager
Manages image operations using delegates to do the work.
Definition: ezimagemanager.php:85
eZImageManager\isImageTimestampValid
isImageTimestampValid( $timestamp)
Definition: ezimagemanager.php:277
eZImageManager\$RuleMap
$RuleMap
Definition: ezimagemanager.php:1429
eZImageManager\isImageAliasValid
isImageAliasValid( $alias)
Definition: ezimagemanager.php:252
eZImageManager\$Types
$Types
Definition: ezimagemanager.php:1431
eZImageManager\$instance
static $instance
Definition: ezimagemanager.php:1452
eZImageManager\$QualityValueMap
$QualityValueMap
Definition: ezimagemanager.php:1440
eZImageManager\readImageAliasesFromINI
readImageAliasesFromINI( $iniFile=false)
Definition: ezimagemanager.php:293
eZImageManager\hasAlias
hasAlias( $aliasName)
Definition: ezimagemanager.php:192
eZImageManager\readSupportedFormatsFromINI
readSupportedFormatsFromINI( $iniFile=false)
Definition: ezimagemanager.php:336
eZImageManager\$ImageHandlers
$ImageHandlers
Definition: ezimagemanager.php:1424
eZImageManager\imageAliasInfo
imageAliasInfo( $mimeData, $aliasName, $isAliasNew=false)
Definition: ezimagemanager.php:1341
eZImageManager\appendSupportedFormat
appendSupportedFormat( $mimeName)
Definition: ezimagemanager.php:131
eZImageManager\readINISettings
readINISettings()
Definition: ezimagemanager.php:599
eZImageManager\__construct
__construct()
Definition: ezimagemanager.php:89
eZImageManager\mimeTypeSetting
mimeTypeSetting( $mimeData)
Definition: ezimagemanager.php:361
eZImageManager\$TemporaryImageDirPath
$TemporaryImageDirPath
Definition: ezimagemanager.php:1442
eZImageManager\$MIMETypes
$MIMETypes
Definition: ezimagemanager.php:1430
eZImageManager\appendMIMETypeSetting
appendMIMETypeSetting( $settings)
Definition: ezimagemanager.php:518
eZImageManager\convert
convert( $sourceMimeData, &$destinationMimeData, $aliasName=false, $parameters=array())
Definition: ezimagemanager.php:1053
eZImageManager\appendQualityValue
appendQualityValue( $mimeType, $qualityValue)
Definition: ezimagemanager.php:477
eZImageManager\analyzeImage
static analyzeImage(&$mimeData, $parameters=array())
Definition: ezimagemanager.php:1018
eZImageManager\mimeTypeOverride
mimeTypeOverride( $mimeData)
Definition: ezimagemanager.php:405
eZImageManager\readMIMETypeQualitySettingFromINI
readMIMETypeQualitySettingFromINI( $iniFile=false)
Definition: ezimagemanager.php:550
eZImageManager\resetInstance
static resetInstance()
Definition: ezimagemanager.php:1416
eZImageManager\createFilterDataFromINI
createFilterDataFromINI( $filterText)
Definition: ezimagemanager.php:741
eZImageManager\$SupportedFormats
$SupportedFormats
Definition: ezimagemanager.php:1432
eZImageManager\readMIMETypeSettingFromINI
readMIMETypeSettingFromINI( $mimeGroup, $iniFile=false)
Definition: ezimagemanager.php:632
eZImageManager\$ImageFilters
$ImageFilters
Definition: ezimagemanager.php:1436
eZImageManager\$OutputMIME
$OutputMIME
Definition: ezimagemanager.php:1425
eZImageManager\$OutputMIMEMap
$OutputMIMEMap
Definition: ezimagemanager.php:1426
eZImageManager\$MIMETypeSettingsMap
$MIMETypeSettingsMap
Definition: ezimagemanager.php:1438
eZImageManager\appendImageHandler
appendImageHandler( $handler)
Definition: ezimagemanager.php:143
eZImageManager\appendConversionRule
appendConversionRule( $conversionRule)
Definition: ezimagemanager.php:612
eZImageManager\$AliasList
$AliasList
Definition: ezimagemanager.php:1434
eZImageManager\alias
alias( $aliasName)
Definition: ezimagemanager.php:201
eZImageManager\$SupportedMIMEMap
$SupportedMIMEMap
Definition: ezimagemanager.php:1433
eZImageManager\$factory
static $factory
Definition: ezimagemanager.php:1460
eZImageManager\createAliasFromINI
createAliasFromINI( $iniGroup)
Definition: ezimagemanager.php:761
eZImageManager\$QualityValueMapOverride
$QualityValueMapOverride
Definition: ezimagemanager.php:1441
eZImageManager\$QualityValues
$QualityValues
Definition: ezimagemanager.php:1439
eZImageManager\appendImageAlias
appendImageAlias( $alias)
Definition: ezimagemanager.php:212
eZImageManager\wildcardToRegexp
static wildcardToRegexp( $wildcard, $separatorCharacter=false)
Definition: ezimagemanager.php:397
eZImageManager\factoryFor
factoryFor( $factoryName, $iniFile=false, $converterName=false)
Definition: ezimagemanager.php:713
eZImageManager\mimeTypeFilters
mimeTypeFilters( $mimeData)
Definition: ezimagemanager.php:422
eZImageManager\setQualityValueOverride
setQualityValueOverride( $mimeType, $qualityValue)
Definition: ezimagemanager.php:500
eZImageManager\$Rules
$Rules
Definition: ezimagemanager.php:1427
eZImageManager\instance
static instance()
Definition: ezimagemanager.php:1383
eZImageManager\$DefaultRule
$DefaultRule
Definition: ezimagemanager.php:1428
eZImageManager\$Factories
$Factories
Definition: ezimagemanager.php:1435
eZImageManager\setSupportedFormats
setSupportedFormats( $mimeList)
Definition: ezimagemanager.php:114
eZImageManager\resetQualityValueOverride
resetQualityValueOverride()
Definition: ezimagemanager.php:510
eZImageManager\$MIMETypeSettings
$MIMETypeSettings
Definition: ezimagemanager.php:1437
eZImageManager\isFilterAllowed
isFilterAllowed( $filterName, $mimeData)
Definition: ezimagemanager.php:438
eZImageManager\conversionRules
conversionRules()
Definition: ezimagemanager.php:620
eZImageManager\$ConversionRules
$ConversionRules
Definition: ezimagemanager.php:1443
eZImageManager\temporaryImageDirPath
temporaryImageDirPath()
Definition: ezimagemanager.php:1372
eZImageManager\readMIMETypeSettingsFromINI
readMIMETypeSettingsFromINI( $iniFile=false)
Definition: ezimagemanager.php:531
eZImageManager\qualityValue
qualityValue( $mimeType)
Definition: ezimagemanager.php:488
eZImageManager\createImageAliasKey
createImageAliasKey( $alias)
Definition: ezimagemanager.php:224
eZImageManager\aliasList
aliasList()
Definition: ezimagemanager.php:173
eZImageManager\readConversionRuleSettingsFromINI
readConversionRuleSettingsFromINI( $iniFile=false)
Definition: ezimagemanager.php:572
eZImageManager\hasMIMETypeSetting
hasMIMETypeSetting( $mimeData)
Definition: ezimagemanager.php:353
eZImageManager\readImageHandlersFromINI
readImageHandlersFromINI( $iniFile=false)
Definition: ezimagemanager.php:674
eZImageManager\isFilterSupported
isFilterSupported( $filterName)
Definition: ezimagemanager.php:158
eZImageManager\createImageAlias
createImageAlias( $aliasName, &$existingAliasList, $parameters=array())
Definition: ezimagemanager.php:810
eZImageManager\factory
static factory()
Definition: ezimagemanager.php:1399
eZMimeType\changeMIMEType
static changeMIMEType(&$mimeInfo, $mimetype)
Definition: ezmimetype.php:117
eZMimeType\changeDirectoryPath
static changeDirectoryPath(&$mimeInfo, $dirpath)
Definition: ezmimetype.php:155
eZMimeType\changeFileData
static changeFileData(&$mimeInfo, $dirpath=false, $basename=false, $suffix=false, $filename=false)
Definition: ezmimetype.php:168
eZMimeType\changeBasename
static changeBasename(&$mimeInfo, $basename)
Definition: ezmimetype.php:141
eZMimeType\findByFileContents
static findByFileContents( $url, $returnDefault=true)
Definition: ezmimetype.php:308
eZSys\ezcrc32
static ezcrc32( $string)
Definition: ezsys.php:1297
eZSys\cacheDirectory
static cacheDirectory()
Definition: ezsys.php:527
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.