|
eZ Publish
[4.0]
|
00001 <?php 00002 // 00003 // Definition of eZImageVariation class 00004 // 00005 // Created on: <01-Jul-2002 15:12:40 sp> 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 ezimagevariation.php 00032 */ 00033 00034 /*! 00035 \class eZImageVariation ezimagevariation.php 00036 \ingroup eZDatatype 00037 \brief The class eZImageVariation does 00038 00039 \deprecated 00040 */ 00041 00042 //include_once( "kernel/classes/ezpersistentobject.php" ); 00043 //include_once( "kernel/classes/ezcontentclassattribute.php" ); 00044 require_once( "kernel/common/image.php" ); 00045 00046 class eZImageVariation extends eZPersistentObject 00047 { 00048 /*! 00049 Constructor 00050 */ 00051 function eZImageVariation( $row ) 00052 { 00053 $this->eZPersistentObject( $row ); 00054 $this->IsOriginal = false; 00055 } 00056 00057 static 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 // VS-DBFILE 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 // $file->fetch(); 00126 // $info = getimagesize( $fullPath ); 00127 // $file->deleteLocal(); 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 { 00140 eZDebug::writeError( "Unknown function 'getimagesize' cannot get image size", 'eZImageVariation::createOriginal' ); 00141 } 00142 } 00143 else 00144 { 00145 eZDebug::writeError( "Unknown imagefile '$fullPath'", 'eZImageVariation::createOriginal' ); 00146 } 00147 return $variation; 00148 } 00149 00150 function fetchVariation( $contentobjectAttributeID, $version, $rwidth, $rheight ) 00151 { 00152 $ret = eZPersistentObject::fetchObjectList( eZImageVariation::definition(), 00153 null, array( "contentobject_attribute_id" => $contentobjectAttributeID, 00154 "version" => $version, 00155 "requested_width" => $rwidth, 00156 "requested_height" => $rheight 00157 ), 00158 null, null, 00159 true ); 00160 if ( count( $ret ) ) 00161 { 00162 return $ret[0]; 00163 } 00164 else 00165 { 00166 return null; 00167 } 00168 } 00169 00170 function removeVariation( $id, $version ) 00171 { 00172 if( $version == null ) 00173 { 00174 eZPersistentObject::removeObject( eZImageVariation::definition(), 00175 array( "contentobject_attribute_id" => $id ) ); 00176 } 00177 else 00178 { 00179 eZPersistentObject::removeObject( eZImageVariation::definition(), 00180 array( "contentobject_attribute_id" => $id, 00181 "version" => $version ) ); 00182 } 00183 } 00184 00185 function &requestVariation( $ezimageobj, $rwidth = 50, $rheight = 50 ) 00186 { 00187 $contentobjectAttributeID = $ezimageobj->attribute( "contentobject_attribute_id" ); 00188 $version = $ezimageobj->attribute( "version" ); 00189 if( !(( $imagevariation = eZImageVariation::fetchVariation( $contentobjectAttributeID, $version, $rwidth, $rheight ) ) === null) ) 00190 { 00191 $variationFileName = $imagevariation->attribute( "filename" ); 00192 $fileName = $ezimageobj->attribute( "filename" ); 00193 $fileName = preg_replace('/\.(.*)$/', "", $fileName ) ; 00194 if( preg_match( "/$fileName/", $variationFileName ) ) 00195 return $imagevariation; 00196 else 00197 $imagevariation->removeVariation( $contentobjectAttributeID, $version ); 00198 } 00199 00200 //include_once( "lib/ezutils/classes/ezini.php" ); 00201 //include_once( "lib/ezfile/classes/ezdir.php" ); 00202 00203 $ini = eZINI::instance(); 00204 $sys = eZSys::instance(); 00205 $StoragePath = $sys->storageDirectory(); 00206 $cat = $ezimageobj->attribute( "mime_type_category" ); 00207 00208 $referencePath = $StoragePath . '/' . $ini->variable( "ImageSettings", "ReferenceDir" ) . '/' . $cat; 00209 $variationPath = $StoragePath . '/' . $ini->variable( "ImageSettings", "VariationsDir" ) . '/' . $cat; 00210 $origFilename = $ezimageobj->attribute( "filename" ); 00211 $imgSuffix = '_' . $rwidth . 'x' . $rheight . '_' . $contentobjectAttributeID ; 00212 $destFilename = preg_replace('/\.(.*)$/', "$imgSuffix.$1", $origFilename) ; 00213 00214 $additionalPath = eZDir::getPathFromFilename( $origFilename ); 00215 eZDir::mkdir( $variationPath . '/' . $additionalPath , 0777, true); 00216 00217 $img = imageInit(); 00218 00219 $convertedName = $ezimageobj->attribute( "filename" ); 00220 00221 $imgINI = eZINI::instance( 'image.ini' ); 00222 $ruleList = $imgINI->variableArray( 'Rules', 'Rules' ); 00223 foreach ( $ruleList as $items ) 00224 { 00225 $sourceMIME = $items[0]; 00226 $destMIME = $items[1]; 00227 $type = $items[2]; 00228 if ( $type == 'convert' or 00229 $type == 'gd' ) 00230 { 00231 $sourceMIME = str_replace("image/", "", $sourceMIME ); 00232 $destMIME = str_replace("image/", "", $destMIME ); 00233 $convertedName = str_replace( $sourceMIME, $destMIME, $convertedName ); 00234 } 00235 } 00236 00237 // VS-DBFILE 00238 00239 $refImagename = $img->convert( $referencePath . '/' . $convertedName, 00240 $variationPath . '/' . $additionalPath . '/' . $destFilename, 00241 array( "width" => $rwidth, "height" => $rheight ), 00242 false 00243 ); 00244 00245 $refImageFilename = explode( ':', $refImagename); 00246 $refImagename = $variationPath . '/' . $additionalPath . '/' . $refImageFilename[ 1 ]; 00247 00248 $imgsize = getimagesize ( $refImagename ); 00249 unset( $refImagename ); 00250 00251 $imageVariation = new eZImageVariation( array("contentobject_attribute_id" => $ezimageobj->attribute( "contentobject_attribute_id" ), 00252 "version" => $ezimageobj->attribute( "version" ), 00253 "filename" => $refImageFilename[1], 00254 "additional_path" => $additionalPath, 00255 "requested_width" => $rwidth, 00256 "requested_height" => $rheight, 00257 "width" => $imgsize[0], 00258 "height" => $imgsize[1] ) ); 00259 00260 $imageFullPath = $variationPath . '/' . $additionalPath . '/' . $refImageFilename[1]; 00261 00262 // VS-DBFILE 00263 00264 require_once( 'kernel/classes/ezclusterfilehandler.php' ); 00265 $file = eZClusterFileHandler::instance( $imageFullPath ); 00266 00267 if ( !$file->exists() || $file->size() == 0 ) 00268 { 00269 eZDebug::writeError( "Could not create variation for $imageFullPath" ); 00270 $retValue = false; 00271 return $retValue; 00272 } 00273 else 00274 { 00275 $imageVariation->store(); 00276 } 00277 00278 return $imageVariation; 00279 } 00280 00281 function fullPath() 00282 { 00283 $sys = eZSys::instance(); 00284 $ini = eZINI::instance(); 00285 $contentobjectAttributeID = $this->attribute( "contentobject_attribute_id" ); 00286 $version = $this->attribute( "version" ); 00287 $img_obj = eZImage::fetch( $contentobjectAttributeID, $version ); 00288 00289 $storageDir = $sys->storageDirectory(); 00290 $category = $img_obj->attribute( "mime_type_category" ); 00291 $additionalPath = $this->attribute( "additional_path" ); 00292 $filename = $this->attribute( "filename" ); 00293 if ( $this->IsOriginal ) 00294 $variationPath = $ini->variable( "ImageSettings", "OriginalDir" ); 00295 else 00296 $variationPath = $ini->variable( "ImageSettings", "VariationsDir" ); 00297 00298 return eZDir::path( array( $storageDir, $variationPath, $category, $additionalPath, $filename ) ); 00299 } 00300 00301 public $Version; 00302 public $ContentObjectAttributeID; 00303 public $Filename; 00304 public $RequestedWidth; 00305 public $RequestedHeight; 00306 public $Width; 00307 public $Height; 00308 public $IsOriginal; 00309 } 00310 00311 ?>