2 # Class to represent an image
3 # Provides methods to retrieve paths (physical, logical, URL),
4 # to generate thumbnails or for uploading.
9 var $name, # name of the image
10 $imagePath, # Path of the image
12 $title, # Title object for this image. Initialized when needed.
13 $fileExists, # does the image file exist on disk?
14 $historyLine, # Number of line to return by nextHistoryLine()
15 $historyRes, # result of the query for the image's history
18 $bits, # --- returned by getimagesize, see http://de3.php.net/manual/en/function.getimagesize.php
24 function Image( $name )
26 global $wgUploadDirectory;
29 $this->title
= Title
::makeTitle( Namespace::getImage(), $this->name
);
30 //$this->imagePath = wfImagePath( $name );
31 $hash = md5( $this->title
->getDBkey() );
32 $this->imagePath
= $wgUploadDirectory . "/" . $hash{0} . "/" .substr( $hash, 0, 2 ) . "/{$name}";
34 $this->url
= $this->wfImageUrl( $name );
36 if ( $this->fileExists
= file_exists( $this->imagePath
) ) // Sic!, "=" is intended
38 @$gis = getimagesize( $this->imagePath
);
39 if( $gis !== false ) {
40 $this->width
= $gis[0];
41 $this->height
= $gis[1];
42 $this->type
= $gis[2];
43 $this->attr
= $gis[3];
44 if ( isset( $gis["bits"] ) ) {
45 $this->bits
= $gis["bits"];
51 $this->historyLine
= 0;
54 function newFromTitle( $nt )
56 $img = new Image( $nt->getDBKey() );
71 function getImagePath()
73 return $this->imagePath
;
91 function getEscapeLocalURL()
93 return $this->title
->escapeLocalURL();
96 function wfImageUrl( $name )
101 $url = "{$wgUploadPath}/" . $hash{0} . "/" .
102 substr( $hash, 0, 2 ) . "/{$name}";
103 return wfUrlencode( $url );
109 return $this->fileExists
;
112 function thumbUrl( $width, $subdir="thumb" ) {
113 global $wgUploadPath;
115 $name = $this->thumbName( $width );
116 $hash = md5( $name );
117 $url = "{$wgUploadPath}/{$subdir}/" . $hash{0} . "/" . substr( $hash, 0, 2 ) . "/{$name}";
119 return wfUrlencode($url);
122 function thumbName( $width ) {
123 return $width."px-".$this->name
;
126 //**********************************************************************
127 // Create a thumbnail of the image having the specified width.
128 // The thumbnail will not be created if the width is larger than the
129 // image's width. Let the browser do the scaling in this case.
130 // The thumbnail is stored on disk and is only computed if the thumbnail
131 // file does not exist OR if it is older than the image.
132 function createThumb( $width ) {
133 global $wgUploadDirectory;
134 global $wgImageMagickConvertCommand;
135 global $wgUseImageMagick;
136 global $wgUseSquid, $wgInternalServer;
138 $width = IntVal( $width );
140 $thumbName = $this->thumbName( $width );
141 $thumbPath = wfImageThumbDir( $thumbName )."/".$thumbName;
142 $thumbUrl = $this->thumbUrl( $width );
144 if ( ! $this->exists() )
146 # If there is no image, there will be no thumbnail
150 # Sanity check $width
156 if( $width > $this->width
) {
157 # Don't make an image bigger than the source
158 return $this->getURL();
161 if ( (! file_exists( $thumbPath ) )
162 ||
( filemtime($thumbPath) < filemtime($this->imagePath
) ) ) {
166 $wgInternalServer.$thumbUrl
168 wfPurgeSquidServers($urlArr);
171 if ( $wgUseImageMagick ) {
173 $cmd = $wgImageMagickConvertCommand .
174 " -quality 85 -geometry {$width} ".
175 escapeshellarg($this->imagePath
) . " " .
176 escapeshellarg($thumbPath);
177 $conv = shell_exec( $cmd );
179 # Use PHP's builtin GD library functions.
181 # First find out what kind of file this is, and select the correct
182 # input routine for this.
186 switch( $this->type
) {
188 $src_image = imagecreatefromgif( $this->imagePath
);
191 $src_image = imagecreatefromjpeg( $this->imagePath
);
195 $src_image = imagecreatefrompng( $this->imagePath
);
196 $truecolor = ( $this->bits
> 8 );
198 case 15: # WBMP for WML
199 $src_image = imagecreatefromwbmp( $this->imagePath
);
202 $src_image = imagecreatefromxbm( $this->imagePath
);
205 return "Image type not supported";
208 $height = floor( $this->height
* ( $width/$this->width
) );
210 $dst_image = imagecreatetruecolor( $width, $height );
212 $dst_image = imagecreate( $width, $height );
214 imagecopyresampled( $dst_image, $src_image,
216 $width, $height, $this->width
, $this->height
);
217 switch( $this->type
) {
222 #$thumbUrl .= ".png";
223 #$thumbPath .= ".png";
224 imagepng( $dst_image, $thumbPath );
227 #$thumbUrl .= ".jpg";
228 #$thumbPath .= ".jpg";
229 imageinterlace( $dst_image );
230 imagejpeg( $dst_image, $thumbPath, 95 );
235 imagedestroy( $dst_image );
236 imagedestroy( $src_image );
241 # Check for zero-sized thumbnails. Those can be generated when
242 # no disk space is available or some other error occurs
244 $thumbstat = stat( $thumbPath );
245 if( $thumbstat["size"] == 0 )
247 unlink( $thumbPath );
252 } // END OF function createThumb
254 //**********************************************************************
255 // Return the image history of this image, line by line.
256 // starts with current version, then old versions.
257 // uses $this->historyLine to check which line to return:
258 // 0 return line for current version
259 // 1 query for old versions, return first one
260 // 2, ... return next old version from above query
261 function nextHistoryLine()
263 $fname = "Image::nextHistoryLine()";
264 $dbr =& wfGetDB( DB_SLAVE
);
265 if ( $this->historyLine
== 0 ) {// called for the first time, return line from cur
266 $this->historyRes
= $dbr->select( 'image',
267 array( 'img_size','img_description','img_user','img_user_text','img_timestamp', "'' AS oi_archive_name" ),
268 array( 'img_name' => $this->title
->getDBkey() ),
271 if ( 0 == wfNumRows( $this->historyRes
) ) {
274 } else if ( $this->historyLine
== 1 ) {
275 $this->historyRes
= $dbr->select( 'oldimage',
276 array( 'oi_size AS img_size', 'oi_description AS img_description', 'oi_user AS img_user',
277 'oi_user_text AS img_user_text', 'oi_timestamp AS img_timestamp', 'oi_archive_name'
278 ), array( 'oi_name' => $this->title
->getDBkey() ), $fname, array( 'ORDER BY' => 'oi_timestamp DESC' )
281 $this->historyLine ++
;
283 return $dbr->fetchObject( $this->historyRes
);
286 function resetHistory()
288 $this->historyLine
= 0;