[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/ezimage/classes/ -> ezimageobject.php (source)

   1  <?php
   2  //
   3  // Definition of eZImageObject class
   4  //
   5  // Created on: <03-Oct-2002 15:05:09 amos>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*! \file ezimageobject.php
  30  */
  31  
  32  /*!
  33    \class eZImageObject ezimageobject.php
  34    \ingroup eZImageObject
  35    \brief Image object which handles image layers
  36  
  37  */
  38  
  39  include_once ( 'lib/ezimage/classes/ezimageinterface.php' );
  40  
  41  /// Alignment values @{
  42  define( 'EZ_IMAGE_ALIGN_AXIS_NONE', 0x00 );
  43  define( 'EZ_IMAGE_ALIGN_AXIS_START', 0x01 );
  44  define( 'EZ_IMAGE_ALIGN_AXIS_STOP', 0x02 );
  45  define( 'EZ_IMAGE_ALIGN_AXIS_CENTER', EZ_IMAGE_ALIGN_AXIS_START | EZ_IMAGE_ALIGN_AXIS_STOP );
  46  define( 'EZ_IMAGE_ALIGN_AXIS_MASK', EZ_IMAGE_ALIGN_AXIS_START | EZ_IMAGE_ALIGN_AXIS_STOP );
  47  ///@}
  48  
  49  /// Placement types @{
  50  /// Places the layer absolutely from on the axis.
  51  define( 'EZ_IMAGE_PLACE_CONSTANT', 1 );
  52  /// Places the layer relative to the axis size.
  53  define( 'EZ_IMAGE_PLACE_RELATIVE', 2 );
  54  ///@}
  55  
  56  class eZImageObject extends eZImageInterface
  57  {
  58      function eZImageObject( $imageObjectRef = null, $imageObject = null, $width = false, $height = false )
  59      {
  60          $this->eZImageInterface( $imageObjectRef, $imageObject, $width, $height );
  61          $this->TemplateURI = 'design:image/imageobject.tpl';
  62          $this->ImageLayers = array();
  63          $this->ImageLayerCounter = 0;
  64      }
  65  
  66      /*!
  67       A definition which tells the template engine which template to use
  68       for displaying the image.
  69      */
  70      function templateData()
  71      {
  72          return array( 'type' => 'template',
  73                        'template_variable_name' => 'image',
  74                        'uri' => $this->TemplateURI );
  75      }
  76  
  77      /*!
  78       Sets the URI of the template to use for displaying it using the template engine to \a $uri.
  79      */
  80      function setTemplateURI( $uri )
  81      {
  82          $this->TemplateURI = $uri;
  83      }
  84  
  85      /*!
  86       Figures out the absolute axis placement and returns it.
  87       The variable \a $type determines how \a $value is used, it can either
  88       be a constant value (EZ_IMAGE_PLACE_CONSTANT) or a relative value
  89       (EZ_IMAGE_PLACE_RELATIVE) where input value is placed relative to the length
  90       of the axis (\a $axisStop - \a $axisStart).
  91       \a $alignment determines where the axis should start, EZ_IMAGE_ALIGN_AXIS_NONE
  92       and EZ_IMAGE_ALIGN_AXIS_START will return the position from \a $axisStart towards \a $axisStop,
  93       EZ_IMAGE_ALIGN_AXIS_STOP returns the position from the \a $axisStop towards \a $axisStart
  94       while EZ_IMAGE_ALIGN_AXIS_CENTER returns the middle of \a $axisStart and \a $axisStop.
  95      */
  96      function calculateAxisPlacement( $value, $type, $alignment, $axisStart, $axisStop, $currentLength )
  97      {
  98          $pos = 0;
  99          if ( $type == EZ_IMAGE_PLACE_CONSTANT )
 100              $pos = $value;
 101          else if ( $type == EZ_IMAGE_PLACE_RELATIVE )
 102          {
 103              $length = $axisStop - $axisStart;
 104              $pos = $value * $length;
 105          }
 106          $alignment = $alignment & EZ_IMAGE_ALIGN_AXIS_MASK;
 107          if ( $alignment == EZ_IMAGE_ALIGN_AXIS_NONE or
 108               $alignment == EZ_IMAGE_ALIGN_AXIS_START )
 109              return $axisStart + $pos;
 110          if ( $alignment == EZ_IMAGE_ALIGN_AXIS_CENTER )
 111          {
 112              $length = $axisStop - $axisStart;
 113              $halfLength = (int)(($length - $currentLength) / 2);
 114              return $axisStart + $halfLength + $value;
 115          }
 116          else // Align to stop
 117          {
 118              $pos = $axisStop - $pos - $currentLength;
 119              return $pos;
 120          }
 121      }
 122  
 123      /*!
 124       Initializes the axis from the parameter value \a $name filling
 125       in any missing values with defaults.
 126      */
 127      function initializeAxis( $parameters, $name )
 128      {
 129          $axis = array();
 130          if ( isset( $parameters[$name] ) )
 131              $axis = $parameters[$name];
 132          if ( !isset( $axis['alignment'] ) )
 133              $axis['alignment'] = EZ_IMAGE_ALIGN_AXIS_NONE;
 134          if ( !isset( $axis['placement'] ) )
 135              $axis['placement'] = EZ_IMAGE_PLACE_CONSTANT;
 136          if ( !isset( $axis['value'] ) )
 137              $axis['value'] = 0;
 138          return $axis;
 139      }
 140  
 141      /*!
 142       Calculates the position for \c x and \c y parameters in \a $parameters and returns
 143       an absolute position in an array.
 144       The returned array will contain the \c x and \c y key.
 145      */
 146      function calculatePosition( $parameters, $width, $height )
 147      {
 148          $xAxis = eZImageObject::initializeAxis( $parameters, 'x' );
 149          $yAxis = eZImageObject::initializeAxis( $parameters, 'y' );
 150          $x = eZImageObject::calculateAxisPlacement( $xAxis['value'], $xAxis['placement'], $xAxis['alignment'],
 151                                                      0, $this->width(), $width );
 152          $y = eZImageObject::calculateAxisPlacement( $yAxis['value'], $yAxis['placement'], $yAxis['alignment'],
 153                                                      0, $this->height(), $height );
 154          return array( 'x' => $x,
 155                        'y' => $y );
 156      }
 157  
 158      /*!
 159       \return the transparency value found in the parameters or 0 if no value is found.
 160      */
 161      function getTransparency( $parameters )
 162      {
 163          if ( !isset( $parameters['transparency'] ) )
 164          {
 165              return 0.0;
 166          }
 167          $transparency = max( 0.0, min( 1.0, $parameters['transparency'] ) );
 168          return $transparency;
 169      }
 170  
 171      /*!
 172       \return the transparency percentage found in the parameters or 0 if no percentage is found.
 173      */
 174      function getTransparencyPercent( $parameters )
 175      {
 176          $transparency = eZImageObject::getTransparency( $parameters );
 177          return (int)($transparency * 100.0);
 178      }
 179  
 180      /*!
 181       Adds the image layer object \a $imageLayer to the end of the layer list with optional parameters \a $parameters
 182       \return the ID of the layer, the ID is unique per image object.
 183       \sa prependLayer
 184      */
 185      function appendLayer( &$imageLayer, $parameters = array() )
 186      {
 187          if ( get_class( $imageLayer ) != 'ezimagelayer' and
 188               !is_subclass_of( $imageLayer, 'ezimagelayer' ) )
 189          {
 190              eZDebug::writeWarning( 'Only eZImageLayer objects may be added as layer items',
 191                                     'eZImageObject::appendLayer' );
 192              return false;
 193          }
 194          ++$this->ImageLayerCounter;
 195          $layerID = $this->ImageLayerCounter;
 196          $this->ImageLayers[] = $layerID;
 197          $this->ImageLayerIndex[$layerID] = array( 'image' => &$imageLayer,
 198                                                    'parameters' => $parameters );
 199          return $layerID;
 200      }
 201  
 202      /*!
 203       Adds the image layer object \a $imageLayer to the beginning of the layer list with optional parameters \a $parameters
 204       \return the ID of the layer, the ID is unique per image object.
 205       \sa appendLayer
 206      */
 207      function prependLayer( &$imageLayer, $parameters = array() )
 208      {
 209          if ( get_class( $imageLayer ) != 'ezimagelayer' and
 210               !is_subclass_of( $imageLayer, 'ezimagelayer' ) )
 211          {
 212              eZDebug::writeWarning( 'Only eZImageLayer objects may be added as layer items',
 213                                     'eZImageObject::prependLayer' );
 214              return false;
 215          }
 216          ++$this->ImageLayerCounter;
 217          $layerID = $this->ImageLayerCounter;
 218          $this->ImageLayers = array_merge( array( $layerID ),
 219                                            $this->ImageLayers );
 220          $this->ImageLayerIndex[$layerID] = array( 'image' => &$imageLayer,
 221                                                    'parameters' => $parameters );
 222          return $layerID;
 223      }
 224  
 225      /*!
 226       \return true if the layer with ID \a $layerID exists in this image object.
 227      */
 228      function hasLayer( $layerID )
 229      {
 230          return ( in_array( $layerID, $this->ImageLayers ) and
 231                   array_key_exists( $layerID, $this->ImageLayerIndex ) );
 232      }
 233  
 234      /*!
 235       Cleans up the current image object if it is set.
 236      */
 237      function destroy()
 238      {
 239          if( is_array( $this->ImageLayers ) )
 240          {
 241              foreach( $this->ImageLayers as $item )
 242              {
 243                  if ( get_class( $this->ImageLayerIndex[$item]['image'] ) == 'ezimagelayer' or
 244                       is_subclass_of( $this->ImageLayerIndex[$item]['image'], 'ezimagelayer' ) )
 245                      $this->ImageLayerIndex[$item]['image']->destroy();
 246              }
 247          }
 248          parent::destroy();
 249      }
 250  
 251      /*!
 252       Removes the layer with ID \a $layerID.
 253       \return true if succesful
 254       \sa hasLayer, appendLayer
 255      */
 256      function removeLayer( $layerID )
 257      {
 258          if ( !in_array( $layerID, $this->ImageLayers ) or
 259               !array_key_exists( $layerID, $this->ImageLayerIndex ) )
 260              return false;
 261          $layerData = $this->ImageLayerIndex[$layerID];
 262          unset( $this->ImageLayerIndex[$layerID] );
 263          $imageLayers = array();
 264          foreach ( $this->ImageLayers as $imageLayerID )
 265          {
 266              if ( $imageLayerID != $layerID )
 267                  $imageLayers[] = $imageLayerID;
 268          }
 269          $this->ImageLayers = $imageLayers;
 270          return true;
 271      }
 272  
 273      /*!
 274       \virtual
 275       Flattens the image so that it can be displayed.
 276      */
 277      function processImage()
 278      {
 279          $this->flatten();
 280          return true;
 281      }
 282  
 283      /*!
 284       Goes trough all layers and merges them together into a single image.
 285       This image can then be displayed on the webpage.
 286      */
 287      function flatten()
 288      {
 289          $i = 0;
 290          $hasFirst = false;
 291          $firstImageLayer = null;
 292          $firstImageLayerData = null;
 293          while ( $i < count( $this->ImageLayers ) and
 294                  !$hasFirst )
 295          {
 296              $layerID = $this->ImageLayers[$i];
 297              $layerData =& $this->ImageLayerIndex[$layerID];
 298              $layer =& $layerData['image'];
 299              if ( get_class( $layer ) == 'ezimagelayer' or
 300                   is_subclass_of( $layer, 'ezimagelayer' ) )
 301              {
 302                  $firstImageLayerData =& $layerData;
 303                  $firstImageLayer =& $layer;
 304                  $hasFirst = true;
 305              }
 306              else
 307                  eZDebug::writeWarning( 'Wrong image type ' . gettype( $layer ), 'eZImageObject::flatten' );
 308              ++$i;
 309          }
 310          if ( $hasFirst )
 311          {
 312              $firstImageLayer->imageObject();
 313              if ( !$this->width() )
 314              {
 315                  $this->setWidth( $firstImageLayer->width() );
 316              }
 317              if ( !$this->height() )
 318              {
 319                  $this->setHeight( $firstImageLayer->height() );
 320              }
 321              $firstImageLayer->mergeLayer( $this,
 322                                            $firstImageLayerData,
 323                                            $lastImageLayerData );
 324              $lastImageLayerData = null;
 325              while ( $i < count( $this->ImageLayers ) )
 326              {
 327                  $layerID = $this->ImageLayers[$i];
 328                  $layerData =& $this->ImageLayerIndex[$layerID];
 329                  $layer =& $layerData['image'];
 330                  unset( $imageObject );
 331                  if ( get_class( $layer ) == 'ezimagelayer' or
 332                       is_subclass_of( $layer, 'ezimagelayer' ) )
 333                  {
 334                      $layer->mergeLayer( $this,
 335                                          $layerData,
 336                                          $lastImageLayerData );
 337                      $lastImageLayerData =& $layerData;
 338                  }
 339                  else
 340                  {
 341                      eZDebug::writeWarning( 'Wrong image type ' . gettype( $layer ), 'eZImageObject::flatten' );
 342                  }
 343                  ++$i;
 344              }
 345              return true;
 346          }
 347          return false;
 348      }
 349  
 350      /// \privatesection
 351      var $ImageLayers;
 352      var $TemplateURI;
 353      var $ImageLayerIndex;
 354  }
 355  
 356  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7