|
eZ Publish
[4.0]
|
00001 <?php 00002 // 00003 // Definition of eZImageObject class 00004 // 00005 // Created on: <03-Oct-2002 15:05:09 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 /*! \file ezimageobject.php 00032 */ 00033 00034 /*! 00035 \class eZImageObject ezimageobject.php 00036 \ingroup eZImageObject 00037 \brief Image object which handles image layers 00038 00039 */ 00040 00041 //include_once( 'lib/ezimage/classes/ezimageinterface.php' ); 00042 00043 class eZImageObject extends eZImageInterface 00044 { 00045 /// Alignment values @{ 00046 const ALIGN_AXIS_NONE = 0x00; 00047 const ALIGN_AXIS_START = 0x01; 00048 const ALIGN_AXIS_STOP = 0x02; 00049 const ALIGN_AXIS_CENTER = 0x03; // ALIGN_AXIS_START | ALIGN_AXIS_STOP 00050 const ALIGN_AXIS_MASK = 0x03; // ALIGN_AXIS_START | ALIGN_AXIS_STOP 00051 ///@} 00052 00053 /// Placement types @{ 00054 /// Places the layer absolutely from on the axis. 00055 const PLACE_CONSTANT = 1; 00056 /// Places the layer relative to the axis size. 00057 const PLACE_RELATIVE = 2; 00058 ///@} 00059 00060 function eZImageObject( $imageObjectRef = null, $imageObject = null, $width = false, $height = false ) 00061 { 00062 $this->eZImageInterface( $imageObjectRef, $imageObject, $width, $height ); 00063 $this->TemplateURI = 'design:image/imageobject.tpl'; 00064 $this->ImageLayers = array(); 00065 $this->ImageLayerCounter = 0; 00066 } 00067 00068 /*! 00069 A definition which tells the template engine which template to use 00070 for displaying the image. 00071 */ 00072 function templateData() 00073 { 00074 return array( 'type' => 'template', 00075 'template_variable_name' => 'image', 00076 'uri' => $this->TemplateURI ); 00077 } 00078 00079 /*! 00080 Sets the URI of the template to use for displaying it using the template engine to \a $uri. 00081 */ 00082 function setTemplateURI( $uri ) 00083 { 00084 $this->TemplateURI = $uri; 00085 } 00086 00087 /*! 00088 Figures out the absolute axis placement and returns it. 00089 The variable \a $type determines how \a $value is used, it can either 00090 be a constant value (self::PLACE_CONSTANT) or a relative value 00091 (self::PLACE_RELATIVE) where input value is placed relative to the length 00092 of the axis (\a $axisStop - \a $axisStart). 00093 \a $alignment determines where the axis should start, self::ALIGN_AXIS_NONE 00094 and self::ALIGN_AXIS_START will return the position from \a $axisStart towards \a $axisStop, 00095 self::ALIGN_AXIS_STOP returns the position from the \a $axisStop towards \a $axisStart 00096 while self::ALIGN_AXIS_CENTER returns the middle of \a $axisStart and \a $axisStop. 00097 */ 00098 function calculateAxisPlacement( $value, $type, $alignment, $axisStart, $axisStop, $currentLength ) 00099 { 00100 $pos = 0; 00101 if ( $type == self::PLACE_CONSTANT ) 00102 $pos = $value; 00103 else if ( $type == self::PLACE_RELATIVE ) 00104 { 00105 $length = $axisStop - $axisStart; 00106 $pos = $value * $length; 00107 } 00108 $alignment = $alignment & self::ALIGN_AXIS_MASK; 00109 if ( $alignment == self::ALIGN_AXIS_NONE or 00110 $alignment == self::ALIGN_AXIS_START ) 00111 return $axisStart + $pos; 00112 if ( $alignment == self::ALIGN_AXIS_CENTER ) 00113 { 00114 $length = $axisStop - $axisStart; 00115 $halfLength = (int)(($length - $currentLength) / 2); 00116 return $axisStart + $halfLength + $value; 00117 } 00118 else // Align to stop 00119 { 00120 $pos = $axisStop - $pos - $currentLength; 00121 return $pos; 00122 } 00123 } 00124 00125 /*! 00126 Initializes the axis from the parameter value \a $name filling 00127 in any missing values with defaults. 00128 */ 00129 function initializeAxis( $parameters, $name ) 00130 { 00131 $axis = array(); 00132 if ( isset( $parameters[$name] ) ) 00133 $axis = $parameters[$name]; 00134 if ( !isset( $axis['alignment'] ) ) 00135 $axis['alignment'] = self::ALIGN_AXIS_NONE; 00136 if ( !isset( $axis['placement'] ) ) 00137 $axis['placement'] = self::PLACE_CONSTANT; 00138 if ( !isset( $axis['value'] ) ) 00139 $axis['value'] = 0; 00140 return $axis; 00141 } 00142 00143 /*! 00144 Calculates the position for \c x and \c y parameters in \a $parameters and returns 00145 an absolute position in an array. 00146 The returned array will contain the \c x and \c y key. 00147 */ 00148 function calculatePosition( $parameters, $width, $height ) 00149 { 00150 $xAxis = eZImageObject::initializeAxis( $parameters, 'x' ); 00151 $yAxis = eZImageObject::initializeAxis( $parameters, 'y' ); 00152 $x = eZImageObject::calculateAxisPlacement( $xAxis['value'], $xAxis['placement'], $xAxis['alignment'], 00153 0, $this->width(), $width ); 00154 $y = eZImageObject::calculateAxisPlacement( $yAxis['value'], $yAxis['placement'], $yAxis['alignment'], 00155 0, $this->height(), $height ); 00156 return array( 'x' => $x, 00157 'y' => $y ); 00158 } 00159 00160 /*! 00161 \return the transparency value found in the parameters or 0 if no value is found. 00162 */ 00163 function getTransparency( $parameters ) 00164 { 00165 if ( !isset( $parameters['transparency'] ) ) 00166 { 00167 return 0.0; 00168 } 00169 $transparency = max( 0.0, min( 1.0, $parameters['transparency'] ) ); 00170 return $transparency; 00171 } 00172 00173 /*! 00174 \return the transparency percentage found in the parameters or 0 if no percentage is found. 00175 */ 00176 function getTransparencyPercent( $parameters ) 00177 { 00178 $transparency = eZImageObject::getTransparency( $parameters ); 00179 return (int)($transparency * 100.0); 00180 } 00181 00182 /*! 00183 Adds the image layer object \a $imageLayer to the end of the layer list with optional parameters \a $parameters 00184 \return the ID of the layer, the ID is unique per image object. 00185 \sa prependLayer 00186 */ 00187 function appendLayer( &$imageLayer, $parameters = array() ) 00188 { 00189 if ( !$imageLayer instanceof eZImageLayer ) 00190 { 00191 eZDebug::writeWarning( 'Only eZImageLayer objects may be added as layer items', 00192 'eZImageObject::appendLayer' ); 00193 return false; 00194 } 00195 ++$this->ImageLayerCounter; 00196 $layerID = $this->ImageLayerCounter; 00197 $this->ImageLayers[] = $layerID; 00198 $this->ImageLayerIndex[$layerID] = array( 'image' => &$imageLayer, 00199 'parameters' => $parameters ); 00200 return $layerID; 00201 } 00202 00203 /*! 00204 Adds the image layer object \a $imageLayer to the beginning of the layer list with optional parameters \a $parameters 00205 \return the ID of the layer, the ID is unique per image object. 00206 \sa appendLayer 00207 */ 00208 function prependLayer( &$imageLayer, $parameters = array() ) 00209 { 00210 if ( !$imageLayer instanceof eZImageLayer ) 00211 { 00212 eZDebug::writeWarning( 'Only eZImageLayer objects may be added as layer items', 00213 'eZImageObject::prependLayer' ); 00214 return false; 00215 } 00216 ++$this->ImageLayerCounter; 00217 $layerID = $this->ImageLayerCounter; 00218 $this->ImageLayers = array_merge( array( $layerID ), 00219 $this->ImageLayers ); 00220 $this->ImageLayerIndex[$layerID] = array( 'image' => &$imageLayer, 00221 'parameters' => $parameters ); 00222 return $layerID; 00223 } 00224 00225 /*! 00226 \return true if the layer with ID \a $layerID exists in this image object. 00227 */ 00228 function hasLayer( $layerID ) 00229 { 00230 return ( in_array( $layerID, $this->ImageLayers ) and 00231 array_key_exists( $layerID, $this->ImageLayerIndex ) ); 00232 } 00233 00234 /*! 00235 Cleans up the current image object if it is set. 00236 */ 00237 function destroy() 00238 { 00239 if ( is_array( $this->ImageLayers ) ) 00240 { 00241 foreach( $this->ImageLayers as $item ) 00242 { 00243 if ( $this->ImageLayerIndex[$item]['image'] instanceof eZImageLayer ) 00244 { 00245 $this->ImageLayerIndex[$item]['image']->destroy(); 00246 } 00247 } 00248 } 00249 parent::destroy(); 00250 } 00251 00252 /*! 00253 Removes the layer with ID \a $layerID. 00254 \return true if succesful 00255 \sa hasLayer, appendLayer 00256 */ 00257 function removeLayer( $layerID ) 00258 { 00259 if ( !in_array( $layerID, $this->ImageLayers ) or 00260 !array_key_exists( $layerID, $this->ImageLayerIndex ) ) 00261 return false; 00262 $layerData = $this->ImageLayerIndex[$layerID]; 00263 unset( $this->ImageLayerIndex[$layerID] ); 00264 $imageLayers = array(); 00265 foreach ( $this->ImageLayers as $imageLayerID ) 00266 { 00267 if ( $imageLayerID != $layerID ) 00268 $imageLayers[] = $imageLayerID; 00269 } 00270 $this->ImageLayers = $imageLayers; 00271 return true; 00272 } 00273 00274 /*! 00275 \virtual 00276 Flattens the image so that it can be displayed. 00277 */ 00278 function processImage() 00279 { 00280 $this->flatten(); 00281 return true; 00282 } 00283 00284 /*! 00285 Goes trough all layers and merges them together into a single image. 00286 This image can then be displayed on the webpage. 00287 */ 00288 function flatten() 00289 { 00290 $i = 0; 00291 $hasFirst = false; 00292 $firstImageLayer = null; 00293 $firstImageLayerData = null; 00294 while ( $i < count( $this->ImageLayers ) and 00295 !$hasFirst ) 00296 { 00297 $layerID = $this->ImageLayers[$i]; 00298 $layerData =& $this->ImageLayerIndex[$layerID]; 00299 $layer = $layerData['image']; 00300 if ( $layer instanceof eZImageLayer ) 00301 { 00302 $firstImageLayerData = $layerData; 00303 $firstImageLayer = $layer; 00304 $hasFirst = true; 00305 } 00306 else 00307 eZDebug::writeWarning( 'Wrong image type ' . gettype( $layer ), 'eZImageObject::flatten' ); 00308 ++$i; 00309 } 00310 if ( $hasFirst ) 00311 { 00312 $lastImageLayerData = null; 00313 $firstImageLayer->imageObject(); 00314 if ( !$this->width() ) 00315 { 00316 $this->setWidth( $firstImageLayer->width() ); 00317 } 00318 if ( !$this->height() ) 00319 { 00320 $this->setHeight( $firstImageLayer->height() ); 00321 } 00322 $firstImageLayer->mergeLayer( $this, 00323 $firstImageLayerData, 00324 $lastImageLayerData ); 00325 $lastImageLayerData = null; 00326 while ( $i < count( $this->ImageLayers ) ) 00327 { 00328 $layerID = $this->ImageLayers[$i]; 00329 $layerData =& $this->ImageLayerIndex[$layerID]; 00330 $layer = $layerData['image']; 00331 unset( $imageObject ); 00332 if ( $layer instanceof eZImageLayer ) 00333 { 00334 $layer->mergeLayer( $this, 00335 $layerData, 00336 $lastImageLayerData ); 00337 $lastImageLayerData = $layerData; 00338 } 00339 else 00340 { 00341 eZDebug::writeWarning( 'Wrong image type ' . gettype( $layer ), 'eZImageObject::flatten' ); 00342 } 00343 ++$i; 00344 } 00345 return true; 00346 } 00347 return false; 00348 } 00349 00350 /// \privatesection 00351 public $ImageLayers; 00352 public $TemplateURI; 00353 public $ImageLayerIndex; 00354 } 00355 00356 ?>