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 include_once( 'lib/ezimage/classes/ezimageinterface.php' );
00042
00043
00044 define( 'EZ_IMAGE_ALIGN_AXIS_NONE', 0x00 );
00045 define( 'EZ_IMAGE_ALIGN_AXIS_START', 0x01 );
00046 define( 'EZ_IMAGE_ALIGN_AXIS_STOP', 0x02 );
00047 define( 'EZ_IMAGE_ALIGN_AXIS_CENTER', EZ_IMAGE_ALIGN_AXIS_START | EZ_IMAGE_ALIGN_AXIS_STOP );
00048 define( 'EZ_IMAGE_ALIGN_AXIS_MASK', EZ_IMAGE_ALIGN_AXIS_START | EZ_IMAGE_ALIGN_AXIS_STOP );
00049
00050
00051
00052
00053 define( 'EZ_IMAGE_PLACE_CONSTANT', 1 );
00054
00055 define( 'EZ_IMAGE_PLACE_RELATIVE', 2 );
00056
00057
00058 class eZImageObject extends eZImageInterface
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
00070
00071
00072 function templateData()
00073 {
00074 return array( 'type' => 'template',
00075 'template_variable_name' => 'image',
00076 'uri' => $this->TemplateURI );
00077 }
00078
00079
00080
00081
00082 function setTemplateURI( $uri )
00083 {
00084 $this->TemplateURI = $uri;
00085 }
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 function calculateAxisPlacement( $value, $type, $alignment, $axisStart, $axisStop, $currentLength )
00099 {
00100 $pos = 0;
00101 if ( $type == EZ_IMAGE_PLACE_CONSTANT )
00102 $pos = $value;
00103 else if ( $type == EZ_IMAGE_PLACE_RELATIVE )
00104 {
00105 $length = $axisStop - $axisStart;
00106 $pos = $value * $length;
00107 }
00108 $alignment = $alignment & EZ_IMAGE_ALIGN_AXIS_MASK;
00109 if ( $alignment == EZ_IMAGE_ALIGN_AXIS_NONE or
00110 $alignment == EZ_IMAGE_ALIGN_AXIS_START )
00111 return $axisStart + $pos;
00112 if ( $alignment == EZ_IMAGE_ALIGN_AXIS_CENTER )
00113 {
00114 $length = $axisStop - $axisStart;
00115 $halfLength = (int)(($length - $currentLength) / 2);
00116 return $axisStart + $halfLength + $value;
00117 }
00118 else
00119 {
00120 $pos = $axisStop - $pos - $currentLength;
00121 return $pos;
00122 }
00123 }
00124
00125
00126
00127
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'] = EZ_IMAGE_ALIGN_AXIS_NONE;
00136 if ( !isset( $axis['placement'] ) )
00137 $axis['placement'] = EZ_IMAGE_PLACE_CONSTANT;
00138 if ( !isset( $axis['value'] ) )
00139 $axis['value'] = 0;
00140 return $axis;
00141 }
00142
00143
00144
00145
00146
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
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
00175
00176 function getTransparencyPercent( $parameters )
00177 {
00178 $transparency = eZImageObject::getTransparency( $parameters );
00179 return (int)($transparency * 100.0);
00180 }
00181
00182
00183
00184
00185
00186
00187 function appendLayer( &$imageLayer, $parameters = array() )
00188 {
00189 if ( get_class( $imageLayer ) != 'ezimagelayer' and
00190 !is_subclass_of( $imageLayer, 'ezimagelayer' ) )
00191 {
00192 eZDebug::writeWarning( 'Only eZImageLayer objects may be added as layer items',
00193 'eZImageObject::appendLayer' );
00194 return false;
00195 }
00196 ++$this->ImageLayerCounter;
00197 $layerID = $this->ImageLayerCounter;
00198 $this->ImageLayers[] = $layerID;
00199 $this->ImageLayerIndex[$layerID] = array( 'image' => &$imageLayer,
00200 'parameters' => $parameters );
00201 return $layerID;
00202 }
00203
00204
00205
00206
00207
00208
00209 function prependLayer( &$imageLayer, $parameters = array() )
00210 {
00211 if ( get_class( $imageLayer ) != 'ezimagelayer' and
00212 !is_subclass_of( $imageLayer, 'ezimagelayer' ) )
00213 {
00214 eZDebug::writeWarning( 'Only eZImageLayer objects may be added as layer items',
00215 'eZImageObject::prependLayer' );
00216 return false;
00217 }
00218 ++$this->ImageLayerCounter;
00219 $layerID = $this->ImageLayerCounter;
00220 $this->ImageLayers = array_merge( array( $layerID ),
00221 $this->ImageLayers );
00222 $this->ImageLayerIndex[$layerID] = array( 'image' => &$imageLayer,
00223 'parameters' => $parameters );
00224 return $layerID;
00225 }
00226
00227
00228
00229
00230 function hasLayer( $layerID )
00231 {
00232 return ( in_array( $layerID, $this->ImageLayers ) and
00233 array_key_exists( $layerID, $this->ImageLayerIndex ) );
00234 }
00235
00236
00237
00238
00239 function destroy()
00240 {
00241 if( is_array( $this->ImageLayers ) )
00242 {
00243 foreach( $this->ImageLayers as $item )
00244 {
00245 if ( get_class( $this->ImageLayerIndex[$item]['image'] ) == 'ezimagelayer' or
00246 is_subclass_of( $this->ImageLayerIndex[$item]['image'], 'ezimagelayer' ) )
00247 $this->ImageLayerIndex[$item]['image']->destroy();
00248 }
00249 }
00250 parent::destroy();
00251 }
00252
00253
00254
00255
00256
00257
00258 function removeLayer( $layerID )
00259 {
00260 if ( !in_array( $layerID, $this->ImageLayers ) or
00261 !array_key_exists( $layerID, $this->ImageLayerIndex ) )
00262 return false;
00263 $layerData = $this->ImageLayerIndex[$layerID];
00264 unset( $this->ImageLayerIndex[$layerID] );
00265 $imageLayers = array();
00266 foreach ( $this->ImageLayers as $imageLayerID )
00267 {
00268 if ( $imageLayerID != $layerID )
00269 $imageLayers[] = $imageLayerID;
00270 }
00271 $this->ImageLayers = $imageLayers;
00272 return true;
00273 }
00274
00275
00276
00277
00278
00279 function processImage()
00280 {
00281 $this->flatten();
00282 return true;
00283 }
00284
00285
00286
00287
00288
00289 function flatten()
00290 {
00291 $i = 0;
00292 $hasFirst = false;
00293 $firstImageLayer = null;
00294 $firstImageLayerData = null;
00295 while ( $i < count( $this->ImageLayers ) and
00296 !$hasFirst )
00297 {
00298 $layerID = $this->ImageLayers[$i];
00299 $layerData =& $this->ImageLayerIndex[$layerID];
00300 $layer =& $layerData['image'];
00301 if ( get_class( $layer ) == 'ezimagelayer' or
00302 is_subclass_of( $layer, 'ezimagelayer' ) )
00303 {
00304 $firstImageLayerData =& $layerData;
00305 $firstImageLayer =& $layer;
00306 $hasFirst = true;
00307 }
00308 else
00309 eZDebug::writeWarning( 'Wrong image type ' . gettype( $layer ), 'eZImageObject::flatten' );
00310 ++$i;
00311 }
00312 if ( $hasFirst )
00313 {
00314 $firstImageLayer->imageObject();
00315 if ( !$this->width() )
00316 {
00317 $this->setWidth( $firstImageLayer->width() );
00318 }
00319 if ( !$this->height() )
00320 {
00321 $this->setHeight( $firstImageLayer->height() );
00322 }
00323 $firstImageLayer->mergeLayer( $this,
00324 $firstImageLayerData,
00325 $lastImageLayerData );
00326 $lastImageLayerData = null;
00327 while ( $i < count( $this->ImageLayers ) )
00328 {
00329 $layerID = $this->ImageLayers[$i];
00330 $layerData =& $this->ImageLayerIndex[$layerID];
00331 $layer =& $layerData['image'];
00332 unset( $imageObject );
00333 if ( get_class( $layer ) == 'ezimagelayer' or
00334 is_subclass_of( $layer, 'ezimagelayer' ) )
00335 {
00336 $layer->mergeLayer( $this,
00337 $layerData,
00338 $lastImageLayerData );
00339 $lastImageLayerData =& $layerData;
00340 }
00341 else
00342 {
00343 eZDebug::writeWarning( 'Wrong image type ' . gettype( $layer ), 'eZImageObject::flatten' );
00344 }
00345 ++$i;
00346 }
00347 return true;
00348 }
00349 return false;
00350 }
00351
00352
00353 var $ImageLayers;
00354 var $TemplateURI;
00355 var $ImageLayerIndex;
00356 }
00357
00358 ?>