Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

Image.php

Go to the documentation of this file.
00001 <?php 00002 # Class to represent an image 00003 # Provides methods to retrieve paths (physical, logical, URL), 00004 # to generate thumbnails or for uploading. 00005 00006 class Image 00007 { 00008 /* private */ 00009 var $name, # name of the image 00010 $imagePath, # Path of the image 00011 $url, # Image URL 00012 $title, # Title object for this image. Initialized when needed. 00013 $fileExists, # does the image file exist on disk? 00014 $historyLine, # Number of line to return by nextHistoryLine() 00015 $historyRes, # result of the query for the image's history 00016 $width, # \ 00017 $height, # | 00018 $bits, # --- returned by getimagesize, see http://de3.php.net/manual/en/function.getimagesize.php 00019 $type, # | 00020 $attr; # / 00021 00022 00023 00024 function Image( $name ) 00025 { 00026 global $wgUploadDirectory; 00027 00028 $this->name = $name; 00029 $this->title = Title::makeTitle( Namespace::getImage(), $this->name ); 00030 //$this->imagePath = wfImagePath( $name ); 00031 $hash = md5( $this->title->getDBkey() ); 00032 $this->imagePath = $wgUploadDirectory . "/" . $hash{0} . "/" .substr( $hash, 0, 2 ) . "/{$name}"; 00033 00034 $this->url = $this->wfImageUrl( $name ); 00035 00036 if ( $this->fileExists = file_exists( $this->imagePath ) ) // Sic!, "=" is intended 00037 { 00038 @$gis = getimagesize( $this->imagePath ); 00039 if( $gis !== false ) { 00040 $this->width = $gis[0]; 00041 $this->height = $gis[1]; 00042 $this->type = $gis[2]; 00043 $this->attr = $gis[3]; 00044 if ( isset( $gis["bits"] ) ) { 00045 $this->bits = $gis["bits"]; 00046 } else { 00047 $this->bits = 0; 00048 } 00049 } 00050 } 00051 $this->historyLine = 0; 00052 } 00053 00054 function newFromTitle( $nt ) 00055 { 00056 $img = new Image( $nt->getDBKey() ); 00057 $img->title = $nt; 00058 return $img; 00059 } 00060 00061 function getName() 00062 { 00063 return $this->name; 00064 } 00065 00066 function getURL() 00067 { 00068 return $this->url; 00069 } 00070 00071 function getImagePath() 00072 { 00073 return $this->imagePath; 00074 } 00075 00076 function getWidth() 00077 { 00078 return $this->width; 00079 } 00080 00081 function getHeight() 00082 { 00083 return $this->height; 00084 } 00085 00086 function getType() 00087 { 00088 return $this->type; 00089 } 00090 00091 function getEscapeLocalURL() 00092 { 00093 return $this->title->escapeLocalURL(); 00094 } 00095 00096 function wfImageUrl( $name ) 00097 { 00098 global $wgUploadPath; 00099 $hash = md5( $name ); 00100 00101 $url = "{$wgUploadPath}/" . $hash{0} . "/" . 00102 substr( $hash, 0, 2 ) . "/{$name}"; 00103 return wfUrlencode( $url ); 00104 } 00105 00106 00107 function exists() 00108 { 00109 return $this->fileExists; 00110 } 00111 00112 function thumbUrl( $width, $subdir="thumb" ) { 00113 global $wgUploadPath; 00114 00115 $name = $this->thumbName( $width ); 00116 $hash = md5( $name ); 00117 $url = "{$wgUploadPath}/{$subdir}/" . $hash{0} . "/" . substr( $hash, 0, 2 ) . "/{$name}"; 00118 00119 return wfUrlencode($url); 00120 } 00121 00122 function thumbName( $width ) { 00123 return $width."px-".$this->name; 00124 } 00125 00126 //********************************************************************** 00127 // Create a thumbnail of the image having the specified width. 00128 // The thumbnail will not be created if the width is larger than the 00129 // image's width. Let the browser do the scaling in this case. 00130 // The thumbnail is stored on disk and is only computed if the thumbnail 00131 // file does not exist OR if it is older than the image. 00132 function createThumb( $width ) { 00133 global $wgUploadDirectory; 00134 global $wgImageMagickConvertCommand; 00135 global $wgUseImageMagick; 00136 global $wgUseSquid, $wgInternalServer; 00137 00138 $width = IntVal( $width ); 00139 00140 $thumbName = $this->thumbName( $width ); 00141 $thumbPath = wfImageThumbDir( $thumbName )."/".$thumbName; 00142 $thumbUrl = $this->thumbUrl( $width ); 00143 00144 if ( ! $this->exists() ) 00145 { 00146 # If there is no image, there will be no thumbnail 00147 return ""; 00148 } 00149 00150 # Sanity check $width 00151 if( $width <= 0 ) { 00152 # BZZZT 00153 return ""; 00154 } 00155 00156 if( $width > $this->width ) { 00157 # Don't make an image bigger than the source 00158 return $this->getURL(); 00159 } 00160 00161 if ( (! file_exists( $thumbPath ) ) 00162 || ( filemtime($thumbPath) < filemtime($this->imagePath) ) ) { 00163 # Squid purging 00164 if ( $wgUseSquid ) { 00165 $urlArr = Array( 00166 $wgInternalServer.$thumbUrl 00167 ); 00168 wfPurgeSquidServers($urlArr); 00169 } 00170 00171 if ( $wgUseImageMagick ) { 00172 # use ImageMagick 00173 $cmd = $wgImageMagickConvertCommand . 00174 " -quality 85 -geometry {$width} ". 00175 escapeshellarg($this->imagePath) . " " . 00176 escapeshellarg($thumbPath); 00177 $conv = shell_exec( $cmd ); 00178 } else { 00179 # Use PHP's builtin GD library functions. 00180 # 00181 # First find out what kind of file this is, and select the correct 00182 # input routine for this. 00183 00184 $truecolor = false; 00185 00186 switch( $this->type ) { 00187 case 1: # GIF 00188 $src_image = imagecreatefromgif( $this->imagePath ); 00189 break; 00190 case 2: # JPG 00191 $src_image = imagecreatefromjpeg( $this->imagePath ); 00192 $truecolor = true; 00193 break; 00194 case 3: # PNG 00195 $src_image = imagecreatefrompng( $this->imagePath ); 00196 $truecolor = ( $this->bits > 8 ); 00197 break; 00198 case 15: # WBMP for WML 00199 $src_image = imagecreatefromwbmp( $this->imagePath ); 00200 break; 00201 case 16: # XBM 00202 $src_image = imagecreatefromxbm( $this->imagePath ); 00203 break; 00204 default: 00205 return "Image type not supported"; 00206 break; 00207 } 00208 $height = floor( $this->height * ( $width/$this->width ) ); 00209 if ( $truecolor ) { 00210 $dst_image = imagecreatetruecolor( $width, $height ); 00211 } else { 00212 $dst_image = imagecreate( $width, $height ); 00213 } 00214 imagecopyresampled( $dst_image, $src_image, 00215 0,0,0,0, 00216 $width, $height, $this->width, $this->height ); 00217 switch( $this->type ) { 00218 case 1: # GIF 00219 case 3: # PNG 00220 case 15: # WBMP 00221 case 16: # XBM 00222 #$thumbUrl .= ".png"; 00223 #$thumbPath .= ".png"; 00224 imagepng( $dst_image, $thumbPath ); 00225 break; 00226 case 2: # JPEG 00227 #$thumbUrl .= ".jpg"; 00228 #$thumbPath .= ".jpg"; 00229 imageinterlace( $dst_image ); 00230 imagejpeg( $dst_image, $thumbPath, 95 ); 00231 break; 00232 default: 00233 break; 00234 } 00235 imagedestroy( $dst_image ); 00236 imagedestroy( $src_image ); 00237 00238 00239 } 00240 # 00241 # Check for zero-sized thumbnails. Those can be generated when 00242 # no disk space is available or some other error occurs 00243 # 00244 $thumbstat = stat( $thumbPath ); 00245 if( $thumbstat["size"] == 0 ) 00246 { 00247 unlink( $thumbPath ); 00248 } 00249 00250 } 00251 return $thumbUrl; 00252 } // END OF function createThumb 00253 00254 //********************************************************************** 00255 // Return the image history of this image, line by line. 00256 // start with current version, than old versions. 00257 // use $this->historyLine to check which line to return: 00258 // 0 return line for current version 00259 // 1 query for old versions, return first one 00260 // 2, ... return next old version from above query 00261 function nextHistoryLine() 00262 { 00263 $fname = "Image::nextHistoryLine()"; 00264 00265 if ( $this->historyLine == 0 ) // called for the first time, return line from cur 00266 { 00267 $sql = "SELECT img_size,img_description,img_user," . 00268 "img_user_text,img_timestamp, '' AS oi_archive_name FROM image WHERE " . 00269 "img_name='" . wfStrencode( $this->title->getDBkey() ) . "'"; 00270 $this->historyRes = wfQuery( $sql, DB_READ, $fname ); 00271 00272 if ( 0 == wfNumRows( $this->historyRes ) ) { return FALSE; } 00273 00274 } else if ( $this->historyLine == 1 ) 00275 { 00276 $sql = "SELECT oi_size AS img_size, oi_description AS img_description," . 00277 "oi_user AS img_user," . 00278 "oi_user_text AS img_user_text, oi_timestamp AS img_timestamp , oi_archive_name FROM oldimage WHERE " . 00279 "oi_name='" . wfStrencode( $this->title->getDBkey() ) . "' " . 00280 "ORDER BY oi_timestamp DESC"; 00281 $this->historyRes = wfQuery( $sql, DB_READ, $fname ); 00282 } 00283 $this->historyLine ++; 00284 00285 return wfFetchObject( $this->historyRes ); 00286 } 00287 00288 function resetHistory() 00289 { 00290 $this->historyLine = 0; 00291 } 00292 00293 00294 } //class 00295

Generated on Tue Jun 29 23:40:03 2004 for Mediawiki by doxygen 1.3.7