00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 class eZImageInterface
00043 {
00044 function eZImageInterface( $imageObjectRef = null, $imageObject = null, $width = false, $height = false )
00045 {
00046 $this->ImageObjectRef = $imageObjectRef;
00047 $this->ImageObject = $imageObject;
00048 $this->Width = $width;
00049 $this->Height = $height;
00050 $this->AlternativeText = '';
00051 $this->IsTrueColor = null;
00052 $this->Palette = array();
00053 $this->PaletteIndex = array();
00054 $this->StoredPath = false;
00055 $this->Font = null;
00056 $this->IsProcessed = false;
00057 }
00058
00059
00060
00061
00062
00063 function isTruecolor()
00064 {
00065 return $this->IsTrueColor;
00066 }
00067
00068
00069
00070
00071
00072
00073 function attributeMemberMap()
00074 {
00075 return array( 'filepath' => 'StoredPath',
00076 'filename' => 'StoredFile',
00077 'width' => 'Width',
00078 'height' => 'Height',
00079 'alternative_text' => 'AlternativeText' );
00080 }
00081
00082
00083
00084
00085
00086
00087 function attributeFunctionMap()
00088 {
00089 return array( 'imagepath' => 'imagePath',
00090 'has_size' => 'hasSize' );
00091 }
00092
00093
00094
00095
00096 function attributes()
00097 {
00098 return array_merge( array_keys( eZImageInterface::attributeMemberMap() ),
00099 array_keys( eZImageInterface::attributeFunctionMap() ) );
00100 }
00101
00102
00103
00104
00105 function hasAttribute( $name )
00106 {
00107 $attributeMemberMap = eZImageInterface::attributeMemberMap();
00108 if ( isset( $attributeMemberMap[$name] ) )
00109 return true;
00110 $attributeFunctionMap = eZImageInterface::attributeFunctionMap();
00111 if ( isset( $attributeFunctionMap[$name] ) )
00112 return true;
00113 return false;
00114 }
00115
00116
00117
00118
00119 function &attribute( $name )
00120 {
00121 $attributeMemberMap = eZImageInterface::attributeMemberMap();
00122 if ( isset( $attributeMemberMap[$name] ) )
00123 {
00124 $member = $attributeMemberMap[$name];
00125 if ( isset( $this->$member ) )
00126 return $this->$member;
00127 eZDebug::writeWarning( 'The member variable $member was not found for attribute $name', 'eZImageInterface::attribute' );
00128 $retValue = null;
00129 return $retValue;
00130 }
00131 $attributeFunctionMap = eZImageInterface::attributeFunctionMap();
00132 if ( isset( $attributeFunctionMap[$name] ) )
00133 {
00134 $function = $attributeFunctionMap[$name];
00135 if ( method_exists( $this, $function ) )
00136 return $this->$function();
00137 eZDebug::writeWarning( 'The member function $function was not found for attribute $name', 'eZImageInterface::attribute' );
00138 $retValue = null;
00139 return $retValue;
00140 }
00141 eZDebug::writeError( "Attribute '$name' does not exist", 'eZImageInterface::attribute' );
00142 $retValue = null;
00143 return $retValue;
00144 }
00145
00146
00147
00148
00149
00150 function isProcessed()
00151 {
00152 return $this->IsProcessed;
00153 }
00154
00155
00156
00157
00158 function &hasSize()
00159 {
00160 $hasSize = ( $this->Width !== false and $this->Height !== false );
00161 return $hasSize;
00162 }
00163
00164
00165
00166
00167 function &imagePath()
00168 {
00169 $imagePath = $this->StoredPath . '/' . $this->StoredFile;
00170 return $imagePath;
00171 }
00172
00173
00174
00175
00176
00177 function setAlternativeText( $text )
00178 {
00179 $this->AlternativeText = $text;
00180 }
00181
00182
00183
00184
00185
00186 function alternativeText()
00187 {
00188 return $this->AlternativeText;
00189 }
00190
00191
00192
00193
00194
00195
00196
00197 function registerImage( $image )
00198 {
00199 if ( !is_resource( $image ) or get_resource_type( $image ) != 'gd' )
00200 return false;
00201 $imageObjectRef = md5( microtime() );
00202 $createdImageArray =& $GLOBALS['eZImageCreatedArray'];
00203 if ( !is_array( $createdImageArray ) )
00204 {
00205 $createdImageArray = array();
00206 register_shutdown_function( 'eZGlobalImageCleanupFunction' );
00207 }
00208 $createdImageArray[$imageObjectRef] = $image;
00209 return $imageObjectRef;
00210 }
00211
00212
00213
00214
00215 function unregisterImage( $imageRef )
00216 {
00217 $createdImageArray =& $GLOBALS['eZImageCreatedArray'];
00218 if ( !is_array( $createdImageArray ) )
00219 return;
00220 if ( !isset( $createdImageArray[$imageRef] ) )
00221 return;
00222 unset( $createdImageArray[$imageRef] );
00223 }
00224
00225
00226
00227
00228 function cleanupRegisteredImages()
00229 {
00230 $createdImageArray =& $GLOBALS['eZImageCreatedArray'];
00231 if ( !is_array( $createdImageArray ) )
00232 return;
00233 foreach ( array_keys( $createdImageArray ) as $createImageKey )
00234 {
00235 $createdImage = $createdImageArray[$createImageKey];
00236 if ( is_resource( $createdImage ) and get_resource_type( $createdImage ) == 'gd' )
00237 ImageDestroy( $createdImage );
00238 }
00239 }
00240
00241
00242
00243
00244
00245
00246 function loadPNG( $storedPath, $storedFile )
00247 {
00248 if ( !function_exists( 'ImageCreateFromPNG' ) )
00249 return false;
00250 $this->ImageObject = ImageCreateFromPNG( $storedPath . '/' . $storedFile );
00251 if ( $this->ImageObject )
00252 {
00253 $this->ImageObjectRef = eZImageInterface::registerImage( $this->ImageObject );
00254 return true;
00255 }
00256 return false;
00257 }
00258
00259
00260
00261
00262
00263
00264 function loadJPEG( $storedPath, $storedFile )
00265 {
00266 $this->ImageObject = ImageCreateFromJPEG( $storedPath . '/' . $storedFile );
00267 if ( $this->ImageObject )
00268 {
00269 $this->ImageObjectRef = eZImageInterface::registerImage( $this->ImageObject );
00270 return true;
00271 }
00272 return false;
00273 }
00274
00275
00276
00277
00278
00279
00280 function loadGIF( $storedPath, $storedFile )
00281 {
00282 if ( !function_exists( 'ImageCreateFromGIF' ) )
00283 return false;
00284 $this->ImageObject = ImageCreateFromGIF( $storedPath . '/' . $storedFile );
00285 if ( $this->ImageObject )
00286 {
00287 $this->ImageObjectRef = eZImageInterface::registerImage( $this->ImageObject );
00288 return true;
00289 }
00290 return false;
00291 }
00292
00293
00294
00295
00296
00297
00298 function load()
00299 {
00300 if ( $this->ImageObject !== null and
00301 $this->ImageObjectRef !== null )
00302 return;
00303 switch( $this->StoredType )
00304 {
00305 case 'png':
00306 {
00307 return $this->loadPNG( $this->StoredPath, $this->StoredFile );
00308 } break;
00309
00310 case 'jpg':
00311 {
00312 return $this->loadJPEG( $this->StoredPath, $this->StoredFile );
00313 } break;
00314
00315 case 'gif':
00316 {
00317 return $this->loadGIF( $this->StoredPath, $this->StoredFile );
00318 } break;
00319
00320 default:
00321 {
00322 if ( @$this->loadPNG( $this->StoredPath, $this->StoredFile ) )
00323 return true;
00324 else if ( @$this->loadJPEG( $this->StoredPath, $this->StoredFile ) )
00325 return true;
00326 else if ( @$this->loadGIF( $this->StoredPath, $this->StoredFile ) )
00327 return true;
00328 eZDebug::writeError( 'Image format not supported: ' . $this->StoredType, 'eZImageInterface::load' );
00329 };
00330 }
00331 return false;
00332 }
00333
00334
00335
00336
00337 function destroy()
00338 {
00339 if ( $this->ImageObjectRef === null )
00340 return;
00341 if ( is_resource( $this->ImageObject ) and get_resource_type( $this->ImageObject ) == 'gd' )
00342 ImageDestroy( $this->ImageObject );
00343 eZImageInterface::unregisterImage( $this->ImageObjectRef );
00344 unset( $this->ImageObject );
00345 $this->ImageObject = null;
00346 $this->ImageObjectRef = null;
00347 }
00348
00349
00350
00351
00352
00353
00354
00355 function imageObject( $createMissing = true )
00356 {
00357 if ( $this->ImageObject === null or
00358 $this->ImageObjectRef === null )
00359 {
00360 if ( $createMissing )
00361 {
00362 if ( $this->StoredFile != '' )
00363 {
00364 $this->process();
00365 }
00366 }
00367 }
00368 return $this->ImageObject;
00369 }
00370
00371
00372
00373
00374
00375
00376
00377 function imageObjectInternal( $createMissing = true )
00378 {
00379 if ( $this->ImageObject === null or
00380 $this->ImageObjectRef === null )
00381 {
00382 if ( $createMissing )
00383 $this->create( $this->Width, $this->Height );
00384 }
00385 return $this->ImageObject;
00386 }
00387
00388
00389
00390
00391
00392 function process()
00393 {
00394 if ( $this->processImage() )
00395 $this->IsProcessed = true;
00396 }
00397
00398
00399
00400
00401
00402
00403
00404 function processImage()
00405 {
00406 if ( $this->StoredFile == '' )
00407 return true;
00408 $fileArray = array( $this->StoredPath, $this->StoredFile );
00409 include_once( 'lib/ezfile/classes/ezdir.php' );
00410 $filePath = eZDir::path( $fileArray );
00411 $imageinfo = getimagesize( $filePath );
00412 if ( $imageinfo )
00413 {
00414 $width = $imageinfo[0];
00415 $height = $imageinfo[1];
00416 if ( $this->load() )
00417 {
00418 $this->Width = $width;
00419 $this->Height = $height;
00420 return true;
00421 }
00422 else
00423 eZDebug::writeWarning( "Image failed to load '$filePath'", 'eZImageInterface::imageObject' );
00424 }
00425 else
00426 eZDebug::writeWarning( "No image info could be extracted from '$filePath'", 'eZImageInterface::imageObject' );
00427 return false;
00428 }
00429
00430
00431
00432
00433
00434
00435 function store( $fileName, $filePath, $type )
00436 {
00437 if ( !$this->IsProcessed )
00438 $this->process();
00439 $imageObject = $this->imageObject();
00440 switch( $type )
00441 {
00442 case 'png':
00443 {
00444 include_once( 'lib/ezfile/classes/ezdir.php' );
00445 if ( !file_exists( $filePath ) )
00446 {
00447 $ini =& eZINI::instance();
00448 $perm = $ini->variable( 'FileSettings', 'StorageDirPermissions' );
00449 eZDir::mkdir( $filePath, octdec( $perm ), true );
00450 }
00451 $fileFullPath = eZDir::path( array( $filePath, $fileName ) );
00452 ImagePNG( $imageObject, $fileFullPath );
00453 $this->StoredPath = $filePath;
00454 $this->StoredFile = $fileName;
00455 $this->StoredType = $type;
00456 return true;
00457 } break;
00458
00459 case 'jpg':
00460 {
00461 include_once( 'lib/ezfile/classes/ezdir.php' );
00462 if ( !file_exists( $filePath ) )
00463 {
00464 $ini =& eZINI::instance();
00465 $perm = $ini->variable( 'FileSettings', 'StorageDirPermissions' );
00466 eZDir::mkdir( $filePath, octdec( $perm ), true );
00467 }
00468 ImageJPEG( $imageObject, eZDir::path( array( $filePath, $fileName ) ) );
00469 $this->StoredPath = $filePath;
00470 $this->StoredFile = $fileName;
00471 $this->StoredType = $type;
00472 return true;
00473 } break;
00474
00475 default:
00476 {
00477 eZDebug::writeError( 'Image format not supported: ' . $type, 'eZImageInterface::store' );
00478 };
00479 }
00480 return false;
00481 }
00482
00483
00484
00485
00486
00487 function hasGD2()
00488 {
00489 $imageINI =& eZINI::instance( 'image.ini' );
00490 return $imageINI->variable( 'GDSettings', 'HasGD2' ) == 'true';
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501 }
00502
00503
00504
00505
00506
00507
00508 function createImage( $width, $height, &$useTruecolor )
00509 {
00510 if ( $useTruecolor === null )
00511 {
00512
00513 $useTruecolor = eZImageInterface::hasGD2();
00514 }
00515 if ( $useTruecolor and
00516 !function_exists( 'ImageCreateTrueColor' ) )
00517 {
00518 eZDebug::writeWarning( 'Function ImageCreateTrueColor does not exist, cannot create true color images',
00519 'eZImageInterface::createImage' );
00520 $useTruecolor = false;
00521 }
00522 if ( $useTruecolor )
00523 $imageObject = ImageCreateTrueColor( $width, $height );
00524 else
00525 $imageObject = ImageCreate( $width, $height );
00526 return $imageObject;
00527 }
00528
00529
00530
00531
00532
00533
00534
00535 function create( $width, $height, $useTruecolor = null )
00536 {
00537 if ( $this->ImageObject !== null and
00538 $this->ImageObjectRef !== null )
00539 {
00540 $this->destroy();
00541 }
00542 unset( $this->ImageObject );
00543 $this->ImageObject = eZImageInterface::createImage( $width, $height, $useTruecolor );
00544 $this->Width = $width;
00545 $this->Height = $height;
00546
00547 $this->IsTrueColor = $useTruecolor;
00548 $this->ImageObjectRef = eZImageInterface::registerImage( $this->ImageObject );
00549 }
00550
00551
00552
00553
00554 function clone( &$image )
00555 {
00556 $this->cloneImage( $image->imageObject(), $image->width(), $image->height(),
00557 $image->isTruecolor() );
00558 }
00559
00560
00561
00562
00563
00564 function cloneImage( $imageObject, $width, $height, $useTruecolor = null )
00565 {
00566 if ( $this->ImageObject !== null and
00567 $this->ImageObjectRef !== null )
00568 {
00569 $this->destroy();
00570 }
00571 $this->ImageObject = eZImageInterface::createImage( $width, $height, $useTruecolor );
00572 $this->IsTrueColor = $useTruecolor;
00573 $this->ImageObjectRef = eZImageInterface::registerImage( $this->ImageObject );
00574 ImageCopy( $this->ImageObject, $imageObject, 0, 0, 0, 0, $width, $height );
00575 }
00576
00577
00578
00579
00580 function width()
00581 {
00582 return $this->Width;
00583 }
00584
00585
00586
00587
00588 function height()
00589 {
00590 return $this->Height;
00591 }
00592
00593
00594
00595
00596 function setWidth( $w )
00597 {
00598 $this->Width = $w;
00599 }
00600
00601
00602
00603
00604 function setHeight( $h )
00605 {
00606 $this->Height = $h;
00607 }
00608
00609
00610
00611
00612
00613 function setStoredFile( $file, $path, $type )
00614 {
00615 $this->StoredFile = $file;
00616 $this->StoredPath = $path;
00617 $this->StoredType = $type;
00618 }
00619
00620
00621
00622
00623 function setFont( $font )
00624 {
00625 $this->Font = $font;
00626 }
00627
00628
00629
00630
00631 function font()
00632 {
00633 return $this->Font;
00634 }
00635
00636
00637
00638
00639
00640
00641 function copyImage( $destinationImageObject, $imageObject,
00642 $destinationX, $destinationY,
00643 $sourceWidth, $sourceHeight, $sourceX = 0, $sourceY = 0 )
00644 {
00645 ImageCopy( $destinationImageObject, $imageObject,
00646 $destinationX, $destinationY,
00647 $sourceX, $sourceY, $sourceWidth, $sourceHeight );
00648 }
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658 function mergeImage( $destinationImageObject, $imageObject,
00659 $destinationX, $destinationY,
00660 $sourceWidth, $sourceHeight, $sourceX = 0, $sourceY = 0,
00661 $transparency = 0 )
00662 {
00663 $percent = 100 - $transparency;
00664 ImageCopyMerge( $destinationImageObject, $imageObject,
00665 $destinationX, $destinationY,
00666 $sourceX, $sourceY, $sourceWidth, $sourceHeight,
00667 $percent );
00668 }
00669
00670
00671
00672
00673
00674
00675
00676 function blendImage( $destinationImageObject, $imageObject,
00677 $destinationX, $destinationY,
00678 $sourceWidth, $sourceHeight, $sourceX = 0, $sourceY = 0 )
00679 {
00680 ImageAlphaBlending( $destinationImageObject, true );
00681 ImageCopy( $destinationImageObject, $imageObject,
00682 $destinationX, $destinationY,
00683 $sourceX, $sourceY, $sourceWidth, $sourceHeight );
00684 }
00685
00686 function merge( $imageObject, $x, $y, $width, $height )
00687 {
00688 if ( $this->ImageObject === null or
00689 $this->ImageObjectRef === null )
00690 return false;
00691
00692 imagecolortransparent( $imageObject, 0 );
00693
00694 ImageCopyMerge( $this->ImageObject, $imageObject, $x, $y, 0, 0, $width, $height, 50 );
00695 }
00696
00697
00698
00699
00700
00701 function clear( $color = false )
00702 {
00703 if ( $color === false )
00704 {
00705 if ( count( $this->PaletteIndex ) > 0)
00706 $color = $this->PaletteIndex[0];
00707 else
00708 $color = 'bgcol';
00709 }
00710 if ( is_string( $color ) )
00711 $color = $this->color( $color );
00712 ImageFilledRectangle( $this->ImageObject, 0, 0, $this->Width, $this->Height, $color );
00713 }
00714
00715
00716
00717
00718
00719 function allocateColor( $name, $red, $green, $blue )
00720 {
00721 if ( isset( $this->Palette[$name] ) )
00722 {
00723 eZDebug::writeError( 'Color already defined: ' . $name, 'eZImageInterface::allocateColor' );
00724 return null;
00725 }
00726 $red = max( 0, min( 255, $red ) );
00727 $green = max( 0, min( 255, $green ) );
00728 $blue = max( 0, min( 255, $blue ) );
00729 $color = ImageColorAllocate( $this->ImageObject, $red, $green, $blue );
00730 $this->Palette[$name] = $color;
00731 $this->PaletteIndex[] = $color;
00732 return $color;
00733 }
00734
00735
00736
00737
00738 function color( $name )
00739 {
00740 if ( !isset( $this->Palette[$name] ) )
00741 {
00742 eZDebug::writeError( 'Color not defined: ' . $name, 'eZImageInterface::color' );
00743 return null;
00744 }
00745 return $this->Palette[$name];
00746 }
00747
00748
00749
00750
00751 function textColor()
00752 {
00753 return $this->TextColor;
00754 }
00755
00756
00757
00758
00759 function setTextColor( $textColor )
00760 {
00761 $this->TextColor = $textColor;
00762 }
00763
00764
00765
00766
00767
00768
00769
00770 function drawText( &$font, $textColor, $text, $x, $y, $angle,
00771 $imageObject = null )
00772 {
00773 if ( !$font )
00774 {
00775 eZDebug::writeWarning( 'Cannot render text, no font is set',
00776 'eZImageInterface::drawText' );
00777 return false;
00778 }
00779 if ( !$textColor )
00780 {
00781 eZDebug::writeWarning( 'Cannot render text, no text color is set',
00782 'eZImageInterface::drawText' );
00783 return false;
00784 }
00785 if ( is_string( $textColor ) )
00786 $textColor = $this->color( $textColor );
00787 if ( $textColor === null )
00788 {
00789 eZDebug::writeWarning( 'Cannot render text, invalid text color',
00790 'eZImageInterface::drawText' );
00791 return false;
00792 }
00793
00794 $x += $font->xAdjustment();
00795 $y += $font->yAdjustment();
00796
00797 if ( $imageObject === null )
00798 $imageObject = $this->ImageObject;
00799
00800 ImageTTFText( $imageObject, $font->pointSize(), $angle, $x, $y,
00801 $textColor, $font->realFile(), $text );
00802 }
00803
00804
00805 var $Width;
00806 var $Height;
00807 var $Font;
00808 var $ImageObject;
00809 var $ImageObjectRef;
00810 var $StoredFile;
00811 var $StoredPath;
00812 var $StoredType;
00813 var $PaletteIndex;
00814 var $Palette;
00815 var $AlternativeText;
00816 var $IsTrueColor;
00817 var $IsProcessed;
00818 }
00819
00820
00821
00822
00823
00824 function eZGlobalImageCleanupFunction()
00825 {
00826 eZImageInterface::cleanupRegisteredImages();
00827 }
00828
00829 ?>