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 include_once( "kernel/classes/ezpersistentobject.php" );
00043 include_once( "kernel/classes/ezcontentclassattribute.php" );
00044 include_once( "kernel/common/image.php" );
00045
00046 class eZImageVariation extends eZPersistentObject
00047 {
00048
00049
00050
00051 function eZImageVariation( $row )
00052 {
00053 $this->eZPersistentObject( $row );
00054 $this->IsOriginal = false;
00055 }
00056
00057 function definition()
00058 {
00059 return array( "fields" => array( "contentobject_attribute_id" => array( 'name' => "ContentObjectAttributeID",
00060 'datatype' => 'integer',
00061 'default' => 0,
00062 'required' => true,
00063 'foreign_class' => 'eZContentObjectAttribute',
00064 'foreign_attribute' => 'id',
00065 'multiplicity' => '1..*' ),
00066 "version" => array( 'name' => "Version",
00067 'datatype' => 'integer',
00068 'default' => 0,
00069 'required' => true ),
00070 "filename" => array( 'name' => "Filename",
00071 'datatype' => 'string',
00072 'default' => '',
00073 'required' => true ),
00074 "additional_path" => array( 'name' => "AdditionalPath",
00075 'datatype' => 'string',
00076 'default' => '',
00077 'required' => true ),
00078 "requested_width" => array( 'name' => "RequestedWidth",
00079 'datatype' => 'integer',
00080 'default' => 0,
00081 'required' => true ),
00082 "requested_height" => array( 'name' => "RequestedHeight",
00083 'datatype' => 'integer',
00084 'default' => 0,
00085 'required' => true ),
00086 "width" => array( 'name' => "Width",
00087 'datatype' => 'integer',
00088 'default' => 0,
00089 'required' => true ),
00090 "height" => array( 'name' => "Height",
00091 'datatype' => 'integer',
00092 'default' => 0,
00093 'required' => true ) ),
00094 "keys" => array( "contentobject_attribute_id, version, requestedwidth, requestedheight" ),
00095 "function_attributes" => array( 'full_path' => 'fullPath' ),
00096 "relations" => array( "contentobject_attribute_id" => array( "class" => "ezcontentobjectattribute",
00097 "field" => "id" ) ),
00098 "class_name" => "eZImageVariation",
00099 "name" => "ezimagevariation" );
00100 }
00101
00102 function &createOriginal( $contentObjectAttributeID, $version, $filename, $additionalPath )
00103 {
00104 $additionalPath = false;
00105 $row = array( 'contentobject_attribute_id' => $contentObjectAttributeID,
00106 'version' => $version,
00107 'filename' => $filename,
00108 'additional_path' => $additionalPath );
00109 $variation = new eZImageVariation( $row );
00110 $variation->IsOriginal = true;
00111 $fullPath = $variation->attribute( 'full_path' );
00112
00113
00114
00115 require_once( 'kernel/classes/ezclusterfilehandler.php' );
00116 $file = eZClusterFileHandler::instance( $fullPath );
00117
00118 if ( $file->exists() )
00119 {
00120 if ( function_exists( 'getimagesize' ) )
00121 {
00122 $fetchedFilePath = $file->fetchUnique();
00123 $info = getimagesize( $fetchedFilePath );
00124 $file->fileDeleteLocal( $fetchedFilePath );
00125
00126
00127
00128 if ( $info )
00129 {
00130 $width = $info[0];
00131 $height = $info[1];
00132 $variation->setAttribute( 'width', $width );
00133 $variation->setAttribute( 'height', $height );
00134 $variation->setAttribute( 'requested_width', $width );
00135 $variation->setAttribute( 'requested_height', $height );
00136 }
00137 }
00138 else
00139 eZDebug::writeError( "Unknown function 'getimagesize' cannot get image size", 'eZImageVariation::createOriginal' );
00140 }
00141 else
00142 eZDebug::writeError( "Unknown imagefile '$fullPath'", 'eZImageVariation::createOriginal' );
00143 return $variation;
00144 }
00145
00146 function fetchVariation( $contentobjectAttributeID, $version, $rwidth, $rheight )
00147 {
00148 $ret = eZPersistentObject::fetchObjectList( eZImageVariation::definition(),
00149 null, array( "contentobject_attribute_id" => $contentobjectAttributeID,
00150 "version" => $version,
00151 "requested_width" => $rwidth,
00152 "requested_height" => $rheight
00153 ),
00154 null, null,
00155 true );
00156 if ( count( $ret ) )
00157 {
00158 return $ret[0];
00159 }
00160 else
00161 {
00162 return null;
00163 }
00164 }
00165
00166 function removeVariation( $id, $version )
00167 {
00168 if( $version == null )
00169 {
00170 eZPersistentObject::removeObject( eZImageVariation::definition(),
00171 array( "contentobject_attribute_id" => $id ) );
00172 }
00173 else
00174 {
00175 eZPersistentObject::removeObject( eZImageVariation::definition(),
00176 array( "contentobject_attribute_id" => $id,
00177 "version" => $version ) );
00178 }
00179 }
00180
00181 function &requestVariation( $ezimageobj, $rwidth = 50, $rheight = 50 )
00182 {
00183 $contentobjectAttributeID = $ezimageobj->attribute( "contentobject_attribute_id" );
00184 $version = $ezimageobj->attribute( "version" );
00185 if( !(( $imagevariation = eZImageVariation::fetchVariation( $contentobjectAttributeID, $version, $rwidth, $rheight ) ) === null) )
00186 {
00187 $variationFileName = $imagevariation->attribute( "filename" );
00188 $fileName = $ezimageobj->attribute( "filename" );
00189 $fileName = preg_replace('/\.(.*)$/', "", $fileName ) ;
00190 if( preg_match( "/$fileName/", $variationFileName ) )
00191 return $imagevariation;
00192 else
00193 $imagevariation->removeVariation( $contentobjectAttributeID, $version );
00194 }
00195
00196 include_once( "lib/ezutils/classes/ezini.php" );
00197 include_once( "lib/ezfile/classes/ezdir.php" );
00198
00199 $ini =& eZINI::instance();
00200 $sys =& eZSys::instance();
00201 $StoragePath = $sys->storageDirectory();
00202 $cat = $ezimageobj->attribute( "mime_type_category" );
00203
00204 $referencePath = $StoragePath . '/' . $ini->variable( "ImageSettings", "ReferenceDir" ) . '/' . $cat;
00205 $variationPath = $StoragePath . '/' . $ini->variable( "ImageSettings", "VariationsDir" ) . '/' . $cat;
00206 $origFilename = $ezimageobj->attribute( "filename" );
00207 $imgSuffix = '_' . $rwidth . 'x' . $rheight . '_' . $contentobjectAttributeID ;
00208 $destFilename = preg_replace('/\.(.*)$/', "$imgSuffix.$1", $origFilename) ;
00209
00210 $additionalPath = eZDir::getPathFromFilename( $origFilename );
00211 eZDir::mkdir( $variationPath . '/' . $additionalPath , 0777, true);
00212
00213 $img =& imageInit();
00214
00215 $convertedName = $ezimageobj->attribute( "filename" );
00216
00217 $imgINI =& eZINI::instance( 'image.ini' );
00218 $ruleList = $imgINI->variableArray( 'Rules', 'Rules' );
00219 foreach ( $ruleList as $items )
00220 {
00221 $sourceMIME = $items[0];
00222 $destMIME = $items[1];
00223 $type = $items[2];
00224 if ( $type == 'convert' or
00225 $type == 'gd' )
00226 {
00227 $sourceMIME = str_replace("image/", "", $sourceMIME );
00228 $destMIME = str_replace("image/", "", $destMIME );
00229 $convertedName = str_replace( $sourceMIME, $destMIME, $convertedName );
00230 }
00231 }
00232
00233
00234
00235 $refImagename = $img->convert( $referencePath . '/' . $convertedName,
00236 $variationPath . '/' . $additionalPath . '/' . $destFilename,
00237 array( "width" => $rwidth, "height" => $rheight ),
00238 false
00239 );
00240
00241 $refImageFilename = explode( ':', $refImagename);
00242 $refImagename = $variationPath . '/' . $additionalPath . '/' . $refImageFilename[ 1 ];
00243
00244 $imgsize = getimagesize ( $refImagename );
00245 unset( $refImagename );
00246
00247 $imageVariation = new eZImageVariation( array("contentobject_attribute_id" => $ezimageobj->attribute( "contentobject_attribute_id" ),
00248 "version" => $ezimageobj->attribute( "version" ),
00249 "filename" => $refImageFilename[1],
00250 "additional_path" => $additionalPath,
00251 "requested_width" => $rwidth,
00252 "requested_height" => $rheight,
00253 "width" => $imgsize[0],
00254 "height" => $imgsize[1] ) );
00255
00256 $imageFullPath = $variationPath . '/' . $additionalPath . '/' . $refImageFilename[1];
00257
00258
00259
00260 require_once( 'kernel/classes/ezclusterfilehandler.php' );
00261 $file = eZClusterFileHandler::instance( $imageFullPath );
00262
00263 if ( !$file->exists() || $file->size() == 0 )
00264 {
00265 eZDebug::writeError( "Could not create variation for $imageFullPath" );
00266 $retValue = false;
00267 return $retValue;
00268 }
00269 else
00270 {
00271 $imageVariation->store();
00272 }
00273
00274 return $imageVariation;
00275 }
00276
00277 function &fullPath()
00278 {
00279 $sys =& eZSys::instance();
00280 $ini =& eZINI::instance();
00281 $contentobjectAttributeID = $this->attribute( "contentobject_attribute_id" );
00282 $version = $this->attribute( "version" );
00283 $img_obj = eZImage::fetch( $contentobjectAttributeID, $version );
00284
00285 $storageDir = $sys->storageDirectory();
00286 $category = $img_obj->attribute( "mime_type_category" );
00287 $additionalPath = $this->attribute( "additional_path" );
00288 $filename = $this->attribute( "filename" );
00289 if ( $this->IsOriginal )
00290 $variationPath = $ini->variable( "ImageSettings", "OriginalDir" );
00291 else
00292 $variationPath = $ini->variable( "ImageSettings", "VariationsDir" );
00293
00294 $retFullPath = eZDir::path( array( $storageDir, $variationPath, $category, $additionalPath, $filename ) );
00295 return $retFullPath;
00296 }
00297
00298 var $Version;
00299 var $ContentObjectAttributeID;
00300 var $Filename;
00301 var $RequestedWidth;
00302 var $RequestedHeight;
00303 var $Width;
00304 var $Height;
00305 var $IsOriginal;
00306 }
00307
00308 ?>