eZ Publish  [4.0]
eztemplateimageoperator.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZTemplateImageOperator class
00004 //
00005 // Created on: <05-Mar-2002 12:52:10 amos>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*!
00032   \class eZTemplateImageOperator eztemplateimageoperator.php
00033   \ingroup eZTemplateOperators
00034   \brief Text to image conversion using operator "texttoimage"
00035 
00036   This operator allows a piece of text be converted to an image file representing
00037   the text. The output image is written in PNG format.
00038   Use setFontDir() and setCacheDir() to change where the font is located and where
00039   the cache file should be put.
00040   If fontDir() is an empty string the font will be looked for in the system.
00041 */
00042 
00043 //include_once( "lib/eztemplate/classes/eztemplate.php" );
00044 //include_once( "lib/ezimage/classes/ezimageobject.php" );
00045 //include_once( "lib/ezimage/classes/ezimagelayer.php" );
00046 //include_once( "lib/ezimage/classes/ezimagetextlayer.php" );
00047 //include_once( 'lib/ezimage/classes/ezimagefont.php' );
00048 //include_once( "lib/ezutils/classes/ezini.php" );
00049 
00050 class eZTemplateImageOperator
00051 {
00052     /*!
00053      Initializes the image operator with the operator name $name.
00054     */
00055     function eZTemplateImageOperator( $texttoimageName = "texttoimage",
00056                                       $imageName = "image",
00057                                       $imagefileName = "imagefile" )
00058     {
00059         $this->Operators = array( $texttoimageName,
00060                                   $imageName,
00061                                   $imagefileName );
00062 
00063         //include_once( "lib/ezutils/classes/ezsys.php" );
00064         $ini = eZINI::instance( 'texttoimage.ini' );
00065         $fontDirs = $ini->variable( "PathSettings", "FontDir" );
00066         $this->FontDir = array();
00067         foreach ( $fontDirs as $fontDir )
00068         {
00069             $this->FontDir[] = $fontDir;
00070         }
00071         $this->CacheDir = $ini->variable( "PathSettings", "CacheDir" );
00072         $this->HTMLDir = eZSys::wwwDir() . $ini->variable( "PathSettings", "HtmlDir" );
00073 
00074         $this->DefaultClass = 'default';
00075         $this->Family = "arial";
00076         $this->Colors = array( "bgcolor" => array( 255, 255, 255 ),
00077                                "textcolor" => array( 0, 0, 0 ) );
00078         $this->PointSize = 12;
00079         $this->Angle = 0;
00080         $this->XAdjust = 0;
00081         $this->YAdjust = 0;
00082         $this->WAdjust = 0;
00083         $this->HAdjust = 0;
00084         $this->UseCache = true;
00085         if ( $ini->variable( "ImageSettings", "UseCache" ) == "disabled" )
00086             $this->UseCache = false;
00087 
00088         $functions = array( "ImageTTFBBox",
00089                             "ImageCreate",
00090                             "ImageColorAllocate",
00091                             "ImageColorAllocate",
00092                             "ImageTTFText",
00093                             "ImagePNG",
00094                             "ImageJPEG",
00095                             "ImageDestroy" );
00096         $this->MissingGDFunctions = array();
00097         foreach ( $functions as $function )
00098         {
00099             if ( !function_exists( $function ) )
00100                 $this->MissingGDFunctions[] = $function;
00101         }
00102         $this->ImageGDSupported = count( $this->MissingGDFunctions ) == 0;
00103     }
00104 
00105     function operatorTemplateHints()
00106     {
00107         return array( 'texttoimage' => array( 'input' => true,
00108                                               'output' => true,
00109                                               'output-type' => array( 'objectproxy', 'keep' ),
00110                                               'parameters' => true ),
00111                       'image' => array( 'input' => false,
00112                                         'output' => true,
00113                                         'output-type' => array( 'objectproxy', 'keep' ),
00114                                         'parameters' => true ),
00115                       'imagefile' => array( 'input' => false,
00116                                             'output' => true,
00117                                             'output-type' => 'objectproxy',
00118                                             'parameters' => true ) );
00119     }
00120 
00121     /*!
00122      Performs image conversion using Image GD and returns the html
00123      text for the image.
00124     */
00125     function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$inputValue, $namedParameters,
00126                      $placement )
00127     {
00128         if ( !$this->ImageGDSupported )
00129         {
00130             eZDebug::writeError( "$operatorName cannot be used since the following ImageGD functions are missing: " . implode( ', ', $this->MissingGDFunctions ) );
00131             return;
00132         }
00133 
00134         if ( $operatorName == 'texttoimage' )
00135         {
00136             $class = $namedParameters['class'];
00137 
00138             $family = $this->Family;
00139             $size = $this->PointSize;
00140             $angle = $this->Angle;
00141             $xadj = $this->XAdjust;
00142             $yadj = $this->YAdjust;
00143             $wadj = $this->WAdjust;
00144             $hadj = $this->HAdjust;
00145             $usecache = $this->UseCache;
00146             $bgcol = $this->color( "bgcolor" );
00147             $textcol = $this->color( "textcolor" );
00148 
00149             $ini = eZINI::instance( 'texttoimage.ini' );
00150             $family = $ini->variable( 'DefaultSettings', 'Family' );
00151             $size = $ini->variable( 'DefaultSettings', 'PointSize' );
00152             $angle = $ini->variable( 'DefaultSettings', 'Angle' );
00153             $xadj = $ini->variable( 'DefaultSettings', 'XAdjustment' );
00154             $yadj = $ini->variable( 'DefaultSettings', 'YAdjustment' );
00155             $wadj = $ini->variable( 'DefaultSettings', 'WidthAdjustment' );
00156             $hadj = $ini->variable( 'DefaultSettings', 'HeightAdjustment' );
00157             $bgcol = $this->decodeColor( $ini->variable( 'DefaultSettings', 'BackgroundColor' ) );
00158             $textcol = $this->decodeColor( $ini->variable( 'DefaultSettings', 'TextColor' ) );
00159 
00160             $absoluteWidth = false;
00161             $absoluteHeight = false;
00162 
00163             if ( $ini->hasVariable( $class, 'Family' ) )
00164                 $family = $ini->variable( $class, 'Family' );
00165             if ( $ini->hasVariable( $class, 'PointSize' ) )
00166                 $size = $ini->variable( $class, 'PointSize' );
00167             if ( $ini->hasVariable( $class, 'Angle' ) )
00168                 $angle = $ini->variable( $class, 'Angle' );
00169             if ( $ini->hasVariable( $class, 'XAdjustment' ) )
00170                 $xadj = $ini->variable( $class, 'XAdjustment' );
00171             if ( $ini->hasVariable( $class, 'YAdjustment' ) )
00172                 $yadj = $ini->variable( $class, 'YAdjustment' );
00173             if ( $ini->hasVariable( $class, 'WidthAdjustment' ) )
00174                 $wadj = $ini->variable( $class, 'WidthAdjustment' );
00175             if ( $ini->hasVariable( $class, 'HeightAdjustment' ) )
00176                 $hadj = $ini->variable( $class, 'HeightAdjustment' );
00177             if ( $ini->hasVariable( $class, 'BackgroundColor' ) )
00178                 $bgcol = $this->decodeColor( $ini->variable( $class, 'BackgroundColor' ) );
00179             if ( $ini->hasVariable( $class, 'TextColor' ) )
00180                 $textcol = $this->decodeColor( $ini->variable( $class, 'TextColor' ) );
00181             if ( $ini->hasVariable( $class, 'AbsoluteWidth' ) )
00182                 $absoluteWidth = $ini->variable( $class, 'AbsoluteWidth' );
00183             if ( $ini->hasVariable( $class, 'AbsoluteHeight' ) )
00184                 $absoluteHeight = $ini->variable( $class, 'AbsoluteHeight' );
00185 
00186             if ( $namedParameters['family'] !== null )
00187                 $family = $namedParameters["family"];
00188             if ( $namedParameters["pointsize"] !== null )
00189                 $size = $namedParameters["pointsize"];
00190             if ( $namedParameters["angle"] !== null )
00191                 $angle = $namedParameters["angle"];
00192             if ( $namedParameters["x"] !== null )
00193                 $xadj = $namedParameters["x"];
00194             if ( $namedParameters["y"] !== null )
00195                 $yadj = $namedParameters["y"];
00196             if ( $namedParameters["w"] !== null )
00197                 $wadj = $namedParameters["w"];
00198             if ( $namedParameters["h"] !== null )
00199                 $hadj = $namedParameters["h"];
00200             if ( $namedParameters["usecache"] !== null )
00201                 $usecache = $namedParameters["usecache"];
00202             if ( $namedParameters["bgcolor"] !== null )
00203                 $bgcol = $this->decodeColor( $namedParameters["bgcolor"] );
00204             if ( $namedParameters["textcolor"] !== null )
00205                 $textcol = $this->decodeColor( $namedParameters["textcolor"] );
00206             $storeImage = $namedParameters["storeimage"];
00207 
00208             $fontDir = false;
00209             foreach ( $this->FontDir as $fontPath )
00210             {
00211                 if ( eZImageFont::exists( $family, $fontPath ) )
00212                 {
00213                     $fontDir = $fontPath;
00214                     break;
00215                 }
00216             }
00217             if ( !$fontDir )
00218                 return;
00219             $font = new eZImageFont( $family, $size, $fontDir, $xadj, $yadj );
00220 
00221             if ( $bgcol === null )
00222                 $bgcol = $this->color( "bgcolor" );
00223             if ( !is_array( $bgcol ) or
00224                  count( $bgcol ) < 3 )
00225                 $bgcol = array( 255, 255, 255 );
00226             if ( $textcol === null )
00227                 $textcol = $this->color( "textcolor" );
00228             if ( !is_array( $textcol ) or
00229                  count( $textcol ) < 3 )
00230                 $textcol = array( 0, 0, 0 );
00231 
00232             $alternativeText = htmlspecialchars( $inputValue );
00233             if ( is_string( $usecache ) )
00234                 $md5Text = $usecache;
00235             else
00236                 $md5Text = md5( $inputValue . $family . $size . $angle . $xadj . $yadj . $wadj . $hadj . $absoluteWidth . $absoluteHeight . implode( ",", $bgcol ) . implode( ",", $textcol ) );
00237             if ( is_string( $usecache ) or !$usecache or
00238                  !$this->hasImage( $this->CacheDir, 'imagetext', $md5Text, $alternativeText, $this->StoreAs ) )
00239             {
00240                 $layer = eZImageTextLayer::createForText( $inputValue, $font,
00241                                                            $wadj, $hadj, $angle,
00242                                                            $absoluteWidth, $absoluteHeight );
00243                 if ( !$layer )
00244                 {
00245                     $tpl->error( $operatorName, "Could not open font \"$family\", no image created", $placement );
00246                     return;
00247                 }
00248                 $layer->allocateColor( 'bgcol', $bgcol[0], $bgcol[1], $bgcol[2] );
00249                 $layer->allocateColor( 'textcol', $textcol[0], $textcol[1], $textcol[2] );
00250                 $layer->setTextColor( 'textcol' );
00251 
00252                 if ( $storeImage )
00253                     $this->storeImage( $layer, $this->CacheDir, 'imagetext', $md5Text, $alternativeText, $this->StoreAs );
00254                 $layer->destroy();
00255             }
00256             else
00257             {
00258                 $layer = $this->loadImage( $this->CacheDir, 'imagetext', $md5Text, $alternativeText, $this->StoreAs );
00259             }
00260             $layer->setAlternativeText( $alternativeText );
00261             $inputValue = $layer;
00262         }
00263 
00264         else if ( $operatorName == 'image' )
00265         {
00266             $useCache = $this->UseCache;
00267             $image = new eZImageObject();
00268             $md5Input = "image\n";
00269             $alternativeText = '';
00270             $this->readImageParameters( $tpl, $image, $operatorParameters, $rootNamespace, $currentNamespace, $md5Input, $alternativeText,
00271                                         $placement );
00272             $image->setAlternativeText( $alternativeText );
00273             $md5Text = md5( $md5Input );
00274             if ( !$useCache or
00275                  !$this->hasImage( $this->CacheDir, 'imageobject', $md5Text, $alternativeText, $this->StoreAs ) )
00276             {
00277                 $this->storeImage( $image, $this->CacheDir, 'imageobject', $md5Text, $alternativeText, $this->StoreAs );
00278                 $image->destroy();
00279                 $inputValue = $image;
00280             }
00281             else
00282             {
00283                 $this->setLoadImage( $image, $this->CacheDir, 'imageobject', $md5Text, $alternativeText, $this->StoreAs );
00284                 $image->load();
00285                 $inputValue = $image;
00286             }
00287         }
00288         else if ( $operatorName == 'imagefile' )
00289         {
00290             $file =& $namedParameters['filename'];
00291             $options =& $namedParameters['options'];
00292             $dir = '';
00293             if ( preg_match( "#^(.+)/([^/]+)$#", $file, $matches ) )
00294             {
00295                 $dir = $matches[1];
00296                 $file = $matches[2];
00297             }
00298 
00299             $layer = eZImageLayer::createForFile( $file, $dir );
00300             $alternativeText = $file;
00301             if ( preg_match( "#(.+)\.([^.]+)$#", $file, $matches ) )
00302                 $alternativeText = $matches[1];
00303             $layer->setAlternativeText( $alternativeText );
00304             $inputValue = $layer;
00305         }
00306     }
00307 
00308     /*!
00309      Returns the operators in this class.
00310     */
00311     function operatorList()
00312     {
00313         return $this->Operators;
00314     }
00315 
00316     /*!
00317      \return true to tell the template engine that the parameter list exists per operator type.
00318     */
00319     function namedParameterPerOperator()
00320     {
00321         return true;
00322     }
00323 
00324     /*!
00325      See eZTemplateOperator::namedParameterList
00326     */
00327     function namedParameterList()
00328     {
00329         return array( 'texttoimage' => array( "class" => array( 'type' => 'string',
00330                                                                 'required' => false,
00331                                                                 'default' => $this->DefaultClass ),
00332                                               "family" => array( "type" => "string",
00333                                                                  "required" => false,
00334                                                                  "default" => null ),
00335                                               "pointsize" => array( "type" => "integer",
00336                                                                     "required" => false,
00337                                                                     "default" => null ),
00338                                               "angle" => array( "type" => "integer",
00339                                                                 "required" => false,
00340                                                                 "default" => null ),
00341                                               "bgcolor" => array( "type" => "mixed",
00342                                                                   "required" => false,
00343                                                                   "default" => null ),
00344                                               "textcolor" => array( "type" => "mixed",
00345                                                                     "required" => false,
00346                                                                     "default" => null ),
00347                                               "x" => array( "type" => "integer",
00348                                                             "required" => false,
00349                                                             "default" => null ),
00350                                               "y" => array( "type" => "integer",
00351                                                             "required" => false,
00352                                                             "default" => null ),
00353                                               "w" => array( "type" => "integer",
00354                                                             "required" => false,
00355                                                             "default" => null ),
00356                                               "h" => array( "type" => "integer",
00357                                                             "required" => false,
00358                                                             "default" => null ),
00359                                               "usecache" => array( "type" => "boolean",
00360                                                                    "required" => false,
00361                                                                    "default" => null ),
00362                                               "storeimage" => array( "type" => "boolean",
00363                                                                      "required" => false,
00364                                                                      "default" => true )
00365                                               ),
00366                       'imagefile' => array( 'filename' => array( 'type' => 'string',
00367                                                                  'required' => true ),
00368                                             'options' => array( 'type' => 'array',
00369                                                                 'default' => array(),
00370                                                                 'required' => false ) ) );
00371     }
00372 
00373     /*!
00374      \return the directory where fonts are located. If it is empty
00375      the font is looked for in the system font dirs.
00376      \sa setFontDir
00377     */
00378     function fontDir()
00379     {
00380         return $this->FontDir;
00381     }
00382 
00383     /*!
00384      \return the directory where images are created.
00385      \sa setCacheDir
00386     */
00387     function cacheDir()
00388     {
00389         return $this->CacheDir;
00390     }
00391 
00392     /*!
00393      \return the directory where image is accessible from HTML code.
00394      \sa setHTMLdir
00395     */
00396     function htmlDir()
00397     {
00398         return $this->HTMLDir;
00399     }
00400 
00401     /*!
00402      \return the family name of the default font.
00403      \sa setFamily
00404     */
00405     function family()
00406     {
00407         return $this->Family;
00408     }
00409 
00410     /*!
00411      \return the pointsize of the default font.
00412      \sa setPointsize
00413     */
00414     function pointSize()
00415     {
00416         return $this->PointSize;
00417     }
00418 
00419     /*!
00420      \return the angle at which the font is rendered.
00421     */
00422     function angle()
00423     {
00424         return $this->Angle;
00425     }
00426 
00427     /*!
00428      \return the number of pixels the font is adjusted in the X direction.
00429      \sa setXAdjustment, yAdjustment
00430     */
00431     function xAdjustment()
00432     {
00433         return $this->XAdjust;
00434     }
00435 
00436     /*!
00437      \return the number of pixels the font is adjusted in the Y direction.
00438      \sa setYAdjustment, xAdjustment
00439     */
00440     function yAdjustment()
00441     {
00442         return $this->YAdjust;
00443     }
00444 
00445     /*!
00446      \return the number of pixels the width of the image is adjusted.
00447      \sa setWidthAdjustment, heightAdjustment
00448     */
00449     function widthAdjustment()
00450     {
00451         return $this->WAdjust;
00452     }
00453 
00454     /*!
00455      \return the number of pixels the height of the image is adjusted.
00456      \sa setHeightAdjustment, widthAdjustment
00457     */
00458     function heightAdjustment()
00459     {
00460         return $this->HAdjust;
00461     }
00462 
00463     /*!
00464      \return true if image cache should be reused if the image text etc.. hasn't changed.
00465     */
00466     function useCache()
00467     {
00468         return $this->UseCache;
00469     }
00470 
00471     /*!
00472      \return the array of colors in use
00473     */
00474     function colors()
00475     {
00476         return $this->Colors;
00477     }
00478 
00479     /*!
00480      Takes a mixed mode color representation and decodes it to a an array of three elements
00481      which represents the R, G and B color elements.
00482     */
00483     function decodeColor( /*! The mixed color mode */ $col )
00484     {
00485         $decode = null;
00486         if ( is_array( $col ) )
00487             $decode = $col;
00488         else if ( is_string( $col ) )
00489         {
00490             if ( preg_match( "/^#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})/", $col, $regs ) )
00491                 $decode = array( hexdec( $regs[1] ), hexdec( $regs[2] ), hexdec( $regs[3] ) );
00492         }
00493         return $decode;
00494     }
00495 
00496     /*!
00497      Returns the decodecd color for colorname $colname
00498     */
00499     function color( $colname )
00500     {
00501         if ( isset( $this->Colors[$colname] ) )
00502         {
00503             $col = $this->Colors[$colname];
00504             return $this->decodeColor( $col );
00505         }
00506         return null;
00507     }
00508 
00509     /*!
00510      Sets the font directory, see fontDir() for more information.
00511      \sa fontDir
00512     */
00513     function setFontDir( $dir )
00514     {
00515         $this->FontDir = $dir;
00516     }
00517 
00518     /*!
00519      Sets the directory where images are created.
00520      \sa cacheDir
00521     */
00522     function setCacheDir( $dir )
00523     {
00524         $this->CacheDir = $dir;
00525     }
00526 
00527     /*!
00528      Sets the directory which the HTML code uses to acces the image.
00529      \sa htmlDir
00530     */
00531     function setHTMLDir( $dir )
00532     {
00533         $this->HTMLDir = $dir;
00534     }
00535 
00536     /*!
00537      Sets the font family for the default font.
00538     */
00539     function setFamily( $fam )
00540     {
00541         $this->Family = $fam;
00542     }
00543 
00544     /*!
00545      Sets the pointsize for the default font.
00546     */
00547     function setPointSize( $size )
00548     {
00549         $this->PointSize = $size;
00550     }
00551 
00552     /*!
00553      Sets the angle for the default font.
00554     */
00555     function setAngle( $ang )
00556     {
00557         $this->Angle = $ang;
00558     }
00559 
00560     /*!
00561      Adjustment in the x axis.
00562      \sa xAdjustment, yAdjustment, setYAdjustment
00563     */
00564     function setXAdjustment( $x )
00565     {
00566         $this->XAdjust = $x;
00567     }
00568 
00569     /*!
00570      Adjustment in the y axis.
00571      \sa xAdjustment, yAdjustment, setXAdjustment
00572     */
00573     function setYAdjustment( $y )
00574     {
00575         $this->YAdjust = $y;
00576     }
00577 
00578     /*!
00579      Adjustment for width.
00580      \sa widthAdjustment, heightAdjustment, setHeightAdjustment
00581     */
00582     function setWidthAdjustment( $w )
00583     {
00584         $this->WAdjust = $w;
00585     }
00586 
00587     /*!
00588      Adjustment for height.
00589      \sa widthAdjustment, heightAdjustment, setWidthAdjustment
00590     */
00591     function setHeightAdjustment( $h )
00592     {
00593         $this->HAdjust = $h;
00594     }
00595 
00596     /*!
00597      Sets whether to reuse cache files or not.
00598      \sa useCache
00599     */
00600     function setUseCache( $use )
00601     {
00602         $this->UseCache = $use;
00603     }
00604 
00605     /*!
00606      Sets all the colors.
00607      \sa setColor, color
00608     */
00609     function setColors( $cols )
00610     {
00611         $this->Colors = $cols;
00612     }
00613 
00614     /*!
00615      Sets the colorname $colname to color value $colval.
00616      The colval is a mixed color mode so different values can be input.
00617      \sa setColors, color
00618     */
00619     function setColor( $colname, $colval )
00620     {
00621         $this->Colors[$colname] = $colval;
00622     }
00623 
00624     function hasImage( $dirs, $base, $md5Text, $alternativeText, $imageType )
00625     {
00626         $name = preg_replace( array( "#[^a-zA-Z0-9_-]+#",
00627                                      "#__+#",
00628                                      "#_$#" ),
00629                               array( '_',
00630                                      '_',
00631                                      '' ),
00632                               $alternativeText );
00633         $file = "$name.$imageType";
00634         $splitMD5Path = eZDir::getPathFromFilename( $md5Text );
00635         $filePath = eZDir::path( array( $dirs, $base, $splitMD5Path, $md5Text, $file ) );
00636 
00637         $fileHandler = eZClusterFileHandler::instance( $filePath );
00638         return $fileHandler->exists();
00639     }
00640 
00641     function storeImage( $image, $dirs, $base, $md5Text, $alternativeText, $imageType )
00642     {
00643         $name = preg_replace( array( "#[^a-zA-Z0-9_-]+#",
00644                                      "#__+#",
00645                                      "#_$#" ),
00646                               array( '_',
00647                                      '_',
00648                                      '' ),
00649                               $alternativeText );
00650         $file = "$name.$imageType";
00651         $splitMD5Path = eZDir::getPathFromFilename( $md5Text );
00652         $dirPath = eZDir::path( array( $dirs, $base, $splitMD5Path, $md5Text ) );
00653         $image->store( $file, $dirPath, $imageType );
00654 
00655         $fileHandler = eZClusterFileHandler::instance();
00656         $filePath = eZDir::path( array( $dirPath, $file ) );
00657         $mimeData = eZMimeType::findByURL( $filePath, true );
00658         $fileHandler->fileStore( $filePath, 'texttoimage', false, $mimeData['name'] );
00659     }
00660 
00661     function setLoadImage( $image, $dirs, $base, $md5Text, $alternativeText, $imageType )
00662     {
00663         $name = preg_replace( array( "#[^a-zA-Z0-9_-]+#",
00664                                      "#__+#",
00665                                      "#_$#" ),
00666                               array( '_',
00667                                      '_',
00668                                      '' ),
00669                               $alternativeText );
00670         $file = "$name.$imageType";
00671         $splitMD5Path = eZDir::getPathFromFilename( $md5Text );
00672         $dirPath = eZDir::path( array( $dirs, $base, $splitMD5Path, $md5Text ) );
00673         $filePath = eZDir::path( array( $dirPath, $file ) );
00674         $fileHandler = eZClusterFileHandler::instance( $filePath );
00675         if ( !$fileHandler->exists() )
00676         {
00677             return null;
00678         }
00679 
00680         // we use a local cache of the file, because the eZImage library only works on files on the file system
00681         $fileHandler->fetch( true );
00682         $image->setStoredFile( $file, $dirPath, $imageType );
00683     }
00684 
00685     function loadImage( $dirs, $base, $md5Text, $alternativeText, $imageType )
00686     {
00687         $name = preg_replace( array( "#[^a-zA-Z0-9_-]+#",
00688                                      "#__+#",
00689                                      "#_$#" ),
00690                               array( '_',
00691                                      '_',
00692                                      '' ),
00693                               $alternativeText );
00694         $file = "$name.$imageType";
00695         $splitMD5Path = eZDir::getPathFromFilename( $md5Text );
00696         $dirPath = eZDir::path( array( $dirs, $base, $splitMD5Path, $md5Text ) );
00697         $filePath = eZDir::path( array( $dirPath, $file ) );
00698 
00699         $fileHandler = eZClusterFileHandler::instance( $filePath );
00700         if ( !$fileHandler->exists() )
00701         {
00702             return null;
00703         }
00704 
00705         // we use a local cache of the file, because the eZImage library only works on files on the file system
00706         $fileHandler->fetch( true );
00707         return eZImageLayer::createForFile( $file, $dirPath, $this->StoreAs );
00708     }
00709 
00710     function readImageParameters( $tpl, $image, $operatorParameters, $rootNamespace, $currentNamespace, &$md5Input, &$alternativeText,
00711                                   $placement )
00712     {
00713         $imageAlternativeText = false;
00714         foreach ( array_keys( $operatorParameters ) as $operatorParameterKey )
00715         {
00716             $operatorParameter = $tpl->elementValue( $operatorParameters[$operatorParameterKey], $rootNamespace, $currentNamespace, $placement );
00717             unset( $imageLayer );
00718             $imageLayer = null;
00719             $imageParameters = array();
00720             if ( is_string( $operatorParameter ) )
00721             {
00722                 $imageAlternativeText = $operatorParameter;
00723             }
00724             else if ( is_array( $operatorParameter ) )
00725             {
00726                 $imageLayer = $operatorParameter[0];
00727                 $imageParameterSource = $operatorParameter[1];
00728                 if ( isset( $imageParameterSource['transparency'] ) )
00729                     $imageParameters['transparency'] = $imageParameterSource['transparency'];
00730                 if ( isset( $imageParameterSource['halign'] ) or
00731                      isset( $imageParameterSource['valign'] ) or
00732                      isset( $imageParameterSource['x'] ) or
00733                      isset( $imageParameterSource['y'] ) )
00734                 {
00735                     $xAlignment = eZImageObject::ALIGN_AXIS_NONE;
00736                     $yAlignment = eZImageObject::ALIGN_AXIS_NONE;
00737                     $xPlacement = eZImageObject::PLACE_CONSTANT;
00738                     $yPlacement = eZImageObject::PLACE_CONSTANT;
00739                     $xPos = 0;
00740                     $yPos = 0;
00741                     if ( isset( $imageParameterSource['halign'] ) )
00742                     {
00743                         $alignmentText = strtolower( $imageParameterSource['halign'] );
00744                         switch ( $alignmentText )
00745                         {
00746                             case 'left':
00747                             {
00748                                 $xAlignment = eZImageObject::ALIGN_AXIS_START;
00749                             } break;
00750                             case 'right':
00751                             {
00752                                 $xAlignment = eZImageObject::ALIGN_AXIS_STOP;
00753                             } break;
00754                             case 'center':
00755                             {
00756                                 $xAlignment = eZImageObject::ALIGN_AXIS_CENTER;
00757                             } break;
00758                         }
00759                     }
00760                     if ( isset( $imageParameterSource['valign'] ) )
00761                     {
00762                         $alignmentText = strtolower( $imageParameterSource['valign'] );
00763                         switch ( $alignmentText )
00764                         {
00765                             case 'top':
00766                             {
00767                                 $yAlignment = eZImageObject::ALIGN_AXIS_START;
00768                             } break;
00769                             case 'bottom':
00770                             {
00771                                 $yAlignment = eZImageObject::ALIGN_AXIS_STOP;
00772                             } break;
00773                             case 'center':
00774                             {
00775                                 $yAlignment = eZImageObject::ALIGN_AXIS_CENTER;
00776                             } break;
00777                         }
00778                     }
00779                     if ( isset( $imageParameterSource['x'] ) )
00780                     {
00781                         $xPos = $imageParameterSource['x'];
00782                         $xPlacement = eZImageObject::PLACE_CONSTANT;
00783                     }
00784                     if ( isset( $imageParameterSource['xrel'] ) )
00785                     {
00786                         $xPos = $imageParameterSource['xrel'];
00787                         $xPlacement = eZImageObject::PLACE_RELATIVE;
00788                     }
00789                     if ( isset( $imageParameterSource['y'] ) )
00790                     {
00791                         $yPos = $imageParameterSource['y'];
00792                         $yPlacement = eZImageObject::PLACE_CONSTANT;
00793                     }
00794                     if ( isset( $imageParameterSource['yrel'] ) )
00795                     {
00796                         $yPos = $imageParameterSource['yrel'];
00797                         $yPlacement = eZImageObject::PLACE_RELATIVE;
00798                     }
00799                     $x = array( 'alignment' => $xAlignment,
00800                                 'placement' => $xPlacement,
00801                                 'value' => $xPos );
00802                     $y = array( 'alignment' => $yAlignment,
00803                                 'placement' => $yPlacement,
00804                                 'value' => $yPos );
00805                     $imageParameters['x'] = $x;
00806                     $imageParameters['y'] = $y;
00807                 }
00808             }
00809             else
00810                 $imageLayer = $operatorParameter;
00811             if ( $imageLayer !== null and
00812                  $image->appendLayer( $imageLayer, $imageParameters ) )
00813             {
00814                 $layerText = trim( $imageLayer->alternativeText() );
00815                 if ( $layerText != '' )
00816                 {
00817                     if ( $alternativeText != '' )
00818                         $alternativeText .= '-';
00819                     $alternativeText .= $layerText;
00820                 }
00821                 $md5Input .= $imageLayer->attribute( 'imagepath' );
00822                 $xAlignment = eZImageObject::ALIGN_AXIS_NONE;
00823                 $yAlignment = eZImageObject::ALIGN_AXIS_NONE;
00824                 $xPlacement = eZImageObject::PLACE_CONSTANT;
00825                 $yPlacement = eZImageObject::PLACE_CONSTANT;
00826                 $xPos = 0;
00827                 $yPos = 0;
00828                 if ( isset( $imageParameters['x']['alignment'] ) )
00829                     $xAlignment = $imageParameters['x']['alignment'];
00830                 if ( isset( $imageParameters['y']['alignment'] ) )
00831                     $yAlignment = $imageParameters['y']['alignment'];
00832                 if ( isset( $imageParameters['x']['placement'] ) )
00833                     $xPlacement = $imageParameters['x']['placement'];
00834                 if ( isset( $imageParameters['y']['placement'] ) )
00835                     $yPlacement = $imageParameters['y']['placement'];
00836                 if ( isset( $imageParameters['x']['value'] ) )
00837                     $xPos = $imageParameters['x']['value'];
00838                 if ( isset( $imageParameters['y']['value'] ) )
00839                     $yPos = $imageParameters['y']['value'];
00840                 $md5Input .= "$xPos-$xAlignment-$xPlacement-$yPos-$yAlignment-$yPlacement\n";
00841             }
00842         }
00843         if ( $imageAlternativeText !== false )
00844             $alternativeText = $imageAlternativeText;
00845     }
00846 
00847     /// \privatesection
00848     /// The operator array
00849     public $Operators;
00850     /// The default class to use for text to image conversion
00851     public $DefaultClass;
00852     /// the directory were fonts are found, default is ""
00853     public $FontDir;
00854     /// the directory were cache files are created, default is ""
00855     public $CacheDir;
00856     /// the directory were html code finds the images, default is ""
00857     public $HTMLDir;
00858     /// the default font family, default is "arial"
00859     public $Family;
00860     /// the default font point size, default is 12
00861     public $PointSize;
00862     /// the default font angle, default is 0
00863     public $Angle;
00864     /// the default font x adjustment, default is 0
00865     public $XAdjust;
00866     /// the default font y adjustment, default is 0
00867     public $YAdjust;
00868     /// whether to reuse cache files or not
00869     public $UseCache;
00870     /// the color array, default is bgcolor=white and textcolor=black
00871     public $Colors;
00872     /// Whether image GD is supported
00873     public $ImageGDSupported;
00874     /// Storage Format, default is "png"
00875     public $StoreAs = 'png';
00876 }
00877 
00878 ?>