Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezimagehandler.php
Go to the documentation of this file.
1 <?php
18 class eZImageHandler
19 {
23 public $HandlerName;
24 public $SupportedInputMIMETypes;
25 public $SupportedOutputMIMETypes;
26 public $ConversionRules;
27 public $OutputRewriteType;
28 public $Filters;
29 public $FilterMap;
30 public $SupportImageFilters;
31 public $MIMETagMap;
32 public $IsEnabled;
33 const KEEP_SUFFIX = 1;
34 const REPLACE_SUFFIX = 2;
35 const PREPEND_TAG_REPLACE_SUFFIX = 3;
36
49 public function __construct( $handlerName, $isEnabled = true, $outputRewriteType = self::REPLACE_SUFFIX,
50 $supportedInputMIMETypes = false, $supportedOutputMIMETypes = false,
51 $conversionRules = false, $filters = false, $mimeTagMap = false )
52 {
53 $this->HandlerName = $handlerName;
54 $this->SupportedInputMIMETypes = $supportedInputMIMETypes;
55 $this->SupportedOutputMIMETypes = $supportedOutputMIMETypes;
56 $this->ConversionRules = $conversionRules;
57 $this->OutputRewriteType = $outputRewriteType;
58 $this->Filters = $filters;
59 $this->FilterMap = array();
60 $this->SupportImageFilters = array();
61 $this->MIMETagMap = array();
62 if ( $mimeTagMap )
63 $this->MIMETagMap = $mimeTagMap;
64 if ( $this->Filters)
65 {
66 foreach ( array_keys( $this->Filters ) as $filterKey )
67 {
68 $filter =& $this->Filters[$filterKey];
69 $this->FilterMap[$filter['name']] =& $filter;
70 $this->SupportImageFilters[] = $filter['name'];
71 }
72 }
73 $this->SupportImageFilters = array_unique( $this->SupportImageFilters );
74 $this->IsEnabled = $isEnabled;
75 }
76
85 function isAvailable()
86 {
87 return $this->IsEnabled;
88 }
89
96 function tagForMIMEType( $mimeData )
97 {
98 $name = $mimeData['name'];
99 if ( isset( $this->MIMETagMap[$name] ) )
100 return $this->MIMETagMap[$name];
101 $position = strpos( $name, '/' );
102 if ( $position !== false )
103 return substr( $name, $position + 1 );
104 else
105 return false;
106 }
107
111 function supportedImageFilters()
112 {
113 return $this->SupportImageFilters;
114 }
115
119 function conversionRules()
120 {
121 return $this->ConversionRules;
122 }
123
128 static function createFilterDefinitionFromINI( $filterText )
129 {
130 $equalPosition = strpos( $filterText, '=' );
131 $filterData = false;
132 if ( $equalPosition !== false )
133 {
134 $filterName = substr( $filterText, 0, $equalPosition );
135 $filterData = substr( $filterText, $equalPosition + 1 );
136 }
137 else
138 $filterName = $filterText;
139 return array( 'name' => $filterName,
140 'text' => $filterData );
141 }
142
147 static function convertFilterToText( $filterDefinition, $filterData )
148 {
149 $replaceList = array();
150 if ( $filterData['data'] )
151 {
152 $counter = 1;
153 foreach ( $filterData['data'] as $data )
154 {
155 $replaceList['%' . $counter] = $data;
156 ++$counter;
157 }
158 }
159 $argumentText = $filterDefinition['text'];
160 $text = eZSys::createShellArgument( $argumentText, $replaceList );
161 return $text;
162 }
163
167 function textForFilter( $filterData )
168 {
169 $text = false;
170 if ( isset( $this->FilterMap[$filterData['name']] ) )
171 {
172 $filterDefinition =& $this->FilterMap[$filterData['name']];
173 $text = $this->convertFilterToText( $filterDefinition, $filterData );
174 }
175 return $text;
176 }
177
186 function rewriteURL( $originalMimeData, &$destinationMimeData, $rewriteType, $aliasName = false )
187 {
188 $extraText = false;
189 if ( $aliasName and
190 $aliasName != 'original' )
191 $extraText = '_' . $aliasName;
192 switch ( $rewriteType )
193 {
194 case self::KEEP_SUFFIX:
195 {
196 $destinationMimeData['basename'] = $originalMimeData['basename'];
197 $destinationMimeData['filename'] = $originalMimeData['basename'] . $extraText . '.' . $originalMimeData['suffix'];
198 $destinationMimeData['dirpath'] = $originalMimeData['dirpath'];
199 if ( $originalMimeData['dirpath'] )
200 $destinationMimeData['url'] = $originalMimeData['dirpath'] . '/' . $destinationMimeData['filename'];
201 else
202 $destinationMimeData['url'] = $destinationMimeData['filename'];
203 } break;
204 case self::REPLACE_SUFFIX:
205 {
206 $destinationMimeData['basename'] = $originalMimeData['basename'];
207 $destinationMimeData['filename'] = $originalMimeData['basename'] . $extraText . '.' . $destinationMimeData['suffixes'][0];
208 $destinationMimeData['dirpath'] = $originalMimeData['dirpath'];
209 if ( $originalMimeData['dirpath'] )
210 $destinationMimeData['url'] = $originalMimeData['dirpath'] . '/' . $destinationMimeData['filename'];
211 else
212 $destinationMimeData['url'] = $destinationMimeData['filename'];
213 } break;
214 case self::PREPEND_TAG_REPLACE_SUFFIX:
215 {
216 $tagName = $this->tagForMIMEType( $destinationMimeData );
217 $destinationMimeData['basename'] = $originalMimeData['basename'];
218 $destinationMimeData['filename'] = $originalMimeData['basename'] . $extraText . '.' . $destinationMimeData['suffixes'][0];
219 $destinationMimeData['dirpath'] = $originalMimeData['dirpath'];
220 if ( $originalMimeData['dirpath'] )
221 $destinationMimeData['url'] = $originalMimeData['dirpath'] . '/' . $destinationMimeData['filename'];
222 else
223 $destinationMimeData['url'] = $destinationMimeData['filename'];
224 $destinationMimeData['url'] = $tagName . ':' . $destinationMimeData['url'];
225 } break;
226 }
227 }
228
235 function supportedInputMIMETypes()
236 {
237 return $this->SupportedInputMIMETypes;
238 }
239
246 function supportedOutputMIMETypes()
247 {
248 return $this->SupportedOutputMIMETypes;
249 }
250
257 static function changeFilePermissions( $filepath )
258 {
259 if ( !file_exists( $filepath ) )
260 return false;
261 $ini = eZINI::instance( 'image.ini' );
262 $perm = $ini->variable( "FileSettings", "ImagePermissions" );
263 $success = false;
264 $oldmask = umask( 0 );
265 if ( !chmod( $filepath, octdec( $perm ) ) )
266 eZDebug::writeError( "Chmod $perm $filepath failed", __METHOD__ );
267 else
268 $success = true;
269 umask( $oldmask );
270 return $success;
271 }
272
277 static function wildcardToRegexp( $wildcard, $separatorCharacter = false )
278 {
279 if ( !$separatorCharacter )
280 $separatorCharacter = '#';
281 $wildcardArray = preg_split( "#[*]#", $wildcard, -1, PREG_SPLIT_DELIM_CAPTURE );
282 $wildcardList = array();
283 $i = 0;
284 foreach ( $wildcardArray as $wildcardElement )
285 {
286 if ( ( $i % 2 ) == 1 )
287 $wildcardList[] = '(.+)';
288 else
289 $wildcardList[] = preg_quote( $wildcardElement, $separatorCharacter );
290 ++$i;
291 }
292 $wildcardString = implode( '', $wildcardList );
293 return $wildcardString;
294 }
295
299 function isOutputMIMETypeSupported( $mimeData )
300 {
301 $mimeTypes = $this->supportedOutputMIMETypes();
302 $mimeName = $mimeData;
303 if ( is_array( $mimeData ) )
304 $mimeName = $mimeData['name'];
305 foreach ( $mimeTypes as $mimeType )
306 {
307 if ( strpos( $mimeType, '*' ) !== false )
308 {
309 $matchString = eZImageHandler::wildcardToRegexp( $mimeType );
310 if ( preg_match( "#^" . $matchString . "$#", $mimeName ) )
311 {
312 return true;
313 }
314 }
315 else if ( $mimeType == $mimeName )
316 {
317 return true;
318 }
319 }
320 return false;
321 }
322
326 function isInputMIMETypeSupported( $mimeData )
327 {
328 $mimeTypes = $this->supportedInputMIMETypes();
329 $mimeName = $mimeData;
330 if ( is_array( $mimeData ) )
331 $mimeName = $mimeData['name'];
332 foreach ( $mimeTypes as $mimeType )
333 {
334 if ( strpos( $mimeType, '*' ) !== false )
335 {
336 $matchString = eZImageHandler::wildcardToRegexp( $mimeType );
337 if ( preg_match( "#^" . $matchString . "$#", $mimeName ) )
338 {
339 return true;
340 }
341 }
342 else if ( $mimeType == $mimeName )
343 {
344 return true;
345 }
346 }
347 return false;
348 }
349
360 function outputMIMEType( &$manager, $currentMimeData, $wantedMimeData, $supportedFormatsOriginal, $aliasName = false )
361 {
362 if ( is_array( $this->conversionRules() ) )
363 {
364 $conversionRules = array_merge( $manager->conversionRules(), $this->conversionRules() );
365 }
366 else
367 {
368 $conversionRules = $manager->conversionRules();
369 }
370 $mimeData = false;
371 $mimeType = false;
372 if ( !$this->isInputMIMETypeSupported( $currentMimeData ) )
373 return false;
374
375 if ( $wantedMimeData )
376 {
377 $conversionRules = array_merge( array( array( 'from' => $currentMimeData['name'],
378 'to' => $wantedMimeData['name'] ) ),
379 $conversionRules );
380 }
381
382 $supportedFormats = array();
383 foreach ( $supportedFormatsOriginal as $supportedFormat )
384 {
385 if ( $this->isOutputMIMETypeSupported( $supportedFormat ) )
386 {
387 $supportedFormats[] = $supportedFormat;
388 $conversionRules[] = array( 'from' => $supportedFormat,
389 'to' => $supportedFormat );
390 }
391 }
392
393 if ( $wantedMimeData and
394 in_array( $wantedMimeData['name'], $supportedFormats ) )
395 {
396 $mimeType = $wantedMimeData['name'];
397 }
398 else if ( is_array( $conversionRules ) )
399 {
400 foreach ( $conversionRules as $rule )
401 {
402 if ( !$this->isOutputMIMETypeSupported( $rule['to'] ) or
403 !in_array( $rule['to'], $supportedFormats ) )
404 continue;
405
406 $matchRule = false;
407 if ( strpos( $rule['from'], '*' ) !== false )
408 {
409 $matchString = eZImageHandler::wildcardToRegexp( $rule['from'] );
410 if ( preg_match( "#^" . $matchString . "$#", $currentMimeData['name'] ) )
411 {
412 $matchRule = $rule;
413 }
414 }
415 else if ( $rule['from'] == $currentMimeData['name'] )
416 {
417 $matchRule = $rule;
418 }
419 if ( $matchRule )
420 {
421 if ( $mimeType )
422 {
423 if ( $wantedMimeData and
424 $matchRule['to'] == $wantedMimeData['name'] )
425 $mimeType = $matchRule['to'];
426 }
427 else
428 $mimeType = $matchRule['to'];
429 }
430 }
431 }
432 if ( $mimeType )
433 {
434 $mimeData = eZMimeType::findByName( $mimeType );
435 $this->rewriteURL( $currentMimeData, $mimeData, $this->outputRewriteType(), $aliasName );
436 }
437 return $mimeData;
438 }
439
443 function outputRewriteType()
444 {
445 return $this->OutputRewriteType;
446 }
447
451 function isFilterSupported( $filter )
452 {
453 return isset( $this->FilterMap[$filter['name']] );
454 }
455
461 function convert( $manager, $sourceMimeData, &$destinationMimeData, $filters = false )
462 {
463 }
464
465 }
466
467 ?>
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZImageHandler
The class eZImageHandler does.
Definition: ezimagehandler.php:19
eZImageHandler\$SupportedOutputMIMETypes
$SupportedOutputMIMETypes
Definition: ezimagehandler.php:25
eZImageHandler\supportedImageFilters
supportedImageFilters()
Definition: ezimagehandler.php:111
eZImageHandler\conversionRules
conversionRules()
Definition: ezimagehandler.php:119
eZImageHandler\$OutputRewriteType
$OutputRewriteType
Definition: ezimagehandler.php:27
eZImageHandler\$FilterMap
$FilterMap
Definition: ezimagehandler.php:29
eZImageHandler\tagForMIMEType
tagForMIMEType( $mimeData)
Definition: ezimagehandler.php:96
eZImageHandler\__construct
__construct( $handlerName, $isEnabled=true, $outputRewriteType=self::REPLACE_SUFFIX, $supportedInputMIMETypes=false, $supportedOutputMIMETypes=false, $conversionRules=false, $filters=false, $mimeTagMap=false)
Definition: ezimagehandler.php:49
eZImageHandler\createFilterDefinitionFromINI
static createFilterDefinitionFromINI( $filterText)
Definition: ezimagehandler.php:128
eZImageHandler\$MIMETagMap
$MIMETagMap
Definition: ezimagehandler.php:31
eZImageHandler\outputRewriteType
outputRewriteType()
Definition: ezimagehandler.php:443
eZImageHandler\$IsEnabled
$IsEnabled
Definition: ezimagehandler.php:32
eZImageHandler\changeFilePermissions
static changeFilePermissions( $filepath)
Definition: ezimagehandler.php:257
eZImageHandler\isInputMIMETypeSupported
isInputMIMETypeSupported( $mimeData)
Definition: ezimagehandler.php:326
eZImageHandler\isOutputMIMETypeSupported
isOutputMIMETypeSupported( $mimeData)
Definition: ezimagehandler.php:299
eZImageHandler\isFilterSupported
isFilterSupported( $filter)
Definition: ezimagehandler.php:451
eZImageHandler\textForFilter
textForFilter( $filterData)
Definition: ezimagehandler.php:167
eZImageHandler\$SupportImageFilters
$SupportImageFilters
Definition: ezimagehandler.php:30
eZImageHandler\supportedInputMIMETypes
supportedInputMIMETypes()
Definition: ezimagehandler.php:235
eZImageHandler\$HandlerName
$HandlerName
Definition: ezimagehandler.php:23
eZImageHandler\KEEP_SUFFIX
const KEEP_SUFFIX
Definition: ezimagehandler.php:33
eZImageHandler\$Filters
$Filters
Definition: ezimagehandler.php:28
eZImageHandler\REPLACE_SUFFIX
const REPLACE_SUFFIX
Definition: ezimagehandler.php:34
eZImageHandler\$ConversionRules
$ConversionRules
Definition: ezimagehandler.php:26
eZImageHandler\supportedOutputMIMETypes
supportedOutputMIMETypes()
Definition: ezimagehandler.php:246
eZImageHandler\rewriteURL
rewriteURL( $originalMimeData, &$destinationMimeData, $rewriteType, $aliasName=false)
Definition: ezimagehandler.php:186
eZImageHandler\PREPEND_TAG_REPLACE_SUFFIX
const PREPEND_TAG_REPLACE_SUFFIX
Definition: ezimagehandler.php:35
eZImageHandler\isAvailable
isAvailable()
Definition: ezimagehandler.php:85
eZImageHandler\convert
convert( $manager, $sourceMimeData, &$destinationMimeData, $filters=false)
Definition: ezimagehandler.php:461
eZImageHandler\$SupportedInputMIMETypes
$SupportedInputMIMETypes
Definition: ezimagehandler.php:24
eZImageHandler\convertFilterToText
static convertFilterToText( $filterDefinition, $filterData)
Definition: ezimagehandler.php:147
eZImageHandler\wildcardToRegexp
static wildcardToRegexp( $wildcard, $separatorCharacter=false)
Definition: ezimagehandler.php:277
eZImageHandler\outputMIMEType
outputMIMEType(&$manager, $currentMimeData, $wantedMimeData, $supportedFormatsOriginal, $aliasName=false)
Definition: ezimagehandler.php:360
eZMimeType\findByName
static findByName( $mimeName, $returnDefault=true)
Definition: ezmimetype.php:197
eZSys\createShellArgument
static createShellArgument( $argumentText, array $replaceList)
Definition: ezsys.php:312
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.