8 * Class to represent an image
10 * Provides methods to retrieve paths (physical, logical, URL),
11 * to generate thumbnails or for uploading.
17 var $name, # name of the image
18 $imagePath, # Path of the image
20 $title, # Title object for this image. Initialized when needed.
21 $fileExists, # does the image file exist on disk?
22 $historyLine, # Number of line to return by nextHistoryLine()
23 $historyRes, # result of the query for the image's history
26 $bits, # --- returned by getimagesize, see http://de3.php.net/manual/en/function.getimagesize.php
32 function Image( $name )
34 global $wgUploadDirectory;
37 $this->title
= Title
::makeTitleSafe( NS_IMAGE
, $this->name
);
38 //$this->imagePath = wfImagePath( $name );
39 $hash = md5( $this->title
->getDBkey() );
40 $this->imagePath
= $wgUploadDirectory . '/' . $hash{0} . '/' .substr( $hash, 0, 2 ) . "/{$name}";
42 $this->url
= $this->wfImageUrl( $name );
44 if ( $this->fileExists
= file_exists( $this->imagePath
) ) // Sic!, "=" is intended
46 @$gis = getimagesize( $this->imagePath
);
47 if( $gis !== false ) {
48 $this->width
= $gis[0];
49 $this->height
= $gis[1];
50 $this->type
= $gis[2];
51 $this->attr
= $gis[3];
52 if ( isset( $gis['bits'] ) ) {
53 $this->bits
= $gis['bits'];
59 $this->historyLine
= 0;
62 function newFromTitle( $nt )
64 $img = new Image( $nt->getDBKey() );
84 function getImagePath()
86 return $this->imagePath
;
101 $st = stat( $this->getImagePath() );
110 function getEscapeLocalURL()
112 return $this->title
->escapeLocalURL();
115 function wfImageUrl( $name )
117 global $wgUploadPath;
118 $hash = md5( $name );
120 $url = "{$wgUploadPath}/" . $hash{0} . "/" .
121 substr( $hash, 0, 2 ) . "/{$name}";
122 return wfUrlencode( $url );
127 return $this->fileExists
;
130 function thumbUrl( $width, $subdir='thumb' ) {
131 global $wgUploadPath;
133 $name = $this->thumbName( $width );
134 $hash = md5( $name );
135 $url = "{$wgUploadPath}/{$subdir}/" . $hash{0} . "/" . substr( $hash, 0, 2 ) . "/{$name}";
137 return wfUrlencode($url);
140 function thumbName( $width ) {
141 return $width."px-".$this->name
;
144 function createThumb( $width, $height=-1 ) {
145 if ( $height == -1 ) {
146 return $this->renderThumb( $width );
148 if ( $width < $this->width
) {
149 $thumbheight = $this->height
* $width / $this->width
;
150 $thumbwidth = $width;
152 $thumbheight = $this->height
;
153 $thumbwidth = $this->width
;
155 if ( $thumbheight > $height ) {
156 $thumbwidth = $thumbwidth * $height / $thumbheight;
157 $thumbheight = $height;
159 return $this->renderThumb( $thumbwidth );
163 * Create a thumbnail of the image having the specified width.
164 * The thumbnail will not be created if the width is larger than the
165 * image's width. Let the browser do the scaling in this case.
166 * The thumbnail is stored on disk and is only computed if the thumbnail
167 * file does not exist OR if it is older than the image.
170 function /* private */ renderThumb( $width ) {
171 global $wgUploadDirectory;
172 global $wgImageMagickConvertCommand;
173 global $wgUseImageMagick;
174 global $wgUseSquid, $wgInternalServer;
176 $width = IntVal( $width );
178 $thumbName = $this->thumbName( $width );
179 $thumbPath = wfImageThumbDir( $thumbName ).'/'.$thumbName;
180 $thumbUrl = $this->thumbUrl( $width );
182 if ( ! $this->exists() )
184 # If there is no image, there will be no thumbnail
188 # Sanity check $width
194 if( $width > $this->width
) {
195 # Don't make an image bigger than the source
196 return $this->getURL();
199 if ( (! file_exists( $thumbPath ) ) ||
( filemtime($thumbPath) < filemtime($this->imagePath
) ) ) {
200 if ( $wgUseImageMagick ) {
202 # Specify white background color, will be used for transparent images
203 # in Internet Explorer/Windows instead of default black.
204 $cmd = $wgImageMagickConvertCommand .
205 " -quality 85 -background white -geometry {$width} ".
206 escapeshellarg($this->imagePath
) . " " .
207 escapeshellarg($thumbPath);
208 $conv = shell_exec( $cmd );
210 # Use PHP's builtin GD library functions.
212 # First find out what kind of file this is, and select the correct
213 # input routine for this.
217 switch( $this->type
) {
219 $src_image = imagecreatefromgif( $this->imagePath
);
222 $src_image = imagecreatefromjpeg( $this->imagePath
);
226 $src_image = imagecreatefrompng( $this->imagePath
);
227 $truecolor = ( $this->bits
> 8 );
229 case 15: # WBMP for WML
230 $src_image = imagecreatefromwbmp( $this->imagePath
);
233 $src_image = imagecreatefromxbm( $this->imagePath
);
236 return 'Image type not supported';
239 $height = floor( $this->height
* ( $width/$this->width
) );
241 $dst_image = imagecreatetruecolor( $width, $height );
243 $dst_image = imagecreate( $width, $height );
245 imagecopyresampled( $dst_image, $src_image,
247 $width, $height, $this->width
, $this->height
);
248 switch( $this->type
) {
253 #$thumbUrl .= ".png";
254 #$thumbPath .= ".png";
255 imagepng( $dst_image, $thumbPath );
258 #$thumbUrl .= ".jpg";
259 #$thumbPath .= ".jpg";
260 imageinterlace( $dst_image );
261 imagejpeg( $dst_image, $thumbPath, 95 );
266 imagedestroy( $dst_image );
267 imagedestroy( $src_image );
272 # Check for zero-sized thumbnails. Those can be generated when
273 # no disk space is available or some other error occurs
275 $thumbstat = stat( $thumbPath );
276 if( $thumbstat['size'] == 0 )
278 unlink( $thumbPath );
282 # This has to be done after the image is updated and present for all machines on NFS,
283 # or else the old version might be stored into the squid again
286 $wgInternalServer.$thumbUrl
288 wfPurgeSquidServers($urlArr);
292 } // END OF function createThumb
295 * Return the image history of this image, line by line.
296 * starts with current version, then old versions.
297 * uses $this->historyLine to check which line to return:
298 * 0 return line for current version
299 * 1 query for old versions, return first one
300 * 2, ... return next old version from above query
302 function nextHistoryLine()
304 $fname = 'Image::nextHistoryLine()';
305 $dbr =& wfGetDB( DB_SLAVE
);
306 if ( $this->historyLine
== 0 ) {// called for the first time, return line from cur
307 $this->historyRes
= $dbr->select( 'image',
308 array( 'img_size','img_description','img_user','img_user_text','img_timestamp', "'' AS oi_archive_name" ),
309 array( 'img_name' => $this->title
->getDBkey() ),
312 if ( 0 == wfNumRows( $this->historyRes
) ) {
315 } else if ( $this->historyLine
== 1 ) {
316 $this->historyRes
= $dbr->select( 'oldimage',
317 array( 'oi_size AS img_size', 'oi_description AS img_description', 'oi_user AS img_user',
318 'oi_user_text AS img_user_text', 'oi_timestamp AS img_timestamp', 'oi_archive_name'
319 ), array( 'oi_name' => $this->title
->getDBkey() ), $fname, array( 'ORDER BY' => 'oi_timestamp DESC' )
322 $this->historyLine ++
;
324 return $dbr->fetchObject( $this->historyRes
);
327 function resetHistory()
329 $this->historyLine
= 0;
336 function wfImageDir( $fname )
338 global $wgUploadDirectory;
340 $hash = md5( $fname );
341 $oldumask = umask(0);
342 $dest = $wgUploadDirectory . '/' . $hash{0};
343 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
344 $dest .= '/' . substr( $hash, 0, 2 );
345 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
351 function wfImageThumbDir( $fname , $subdir='thumb')
353 return wfImageArchiveDir( $fname, $subdir );
356 function wfImageArchiveDir( $fname , $subdir='archive')
358 global $wgUploadDirectory;
360 $hash = md5( $fname );
361 $oldumask = umask(0);
363 # Suppress warning messages here; if the file itself can't
364 # be written we'll worry about it then.
365 $archive = $wgUploadDirectory.'/'.$subdir;
366 if ( ! is_dir( $archive ) ) { @mkdir
( $archive, 0777 ); }
367 $archive .= '/' . $hash{0};
368 if ( ! is_dir( $archive ) ) { @mkdir
( $archive, 0777 ); }
369 $archive .= '/' . substr( $hash, 0, 2 );
370 if ( ! is_dir( $archive ) ) { @mkdir
( $archive, 0777 ); }
376 function wfRecordUpload( $name, $oldver, $size, $desc, $copyStatus = "", $source = "" )
378 global $wgUser, $wgLang, $wgTitle, $wgOut, $wgDeferredUpdateList;
379 global $wgUseCopyrightUpload;
381 $fname = 'wfRecordUpload';
382 $dbw =& wfGetDB( DB_MASTER
);
384 # img_name must be unique
385 if ( !$dbw->indexUnique( 'image', 'img_name' ) ) {
386 wfDebugDieBacktrace( 'Database schema not up to date, please run maintenance/archives/patch-image_name_unique.sql' );
390 $now = wfTimestampNow();
391 $won = wfInvertTimestamp( $now );
392 $size = IntVal( $size );
394 if ( $wgUseCopyrightUpload )
396 $textdesc = '== ' . wfMsg ( 'filedesc' ) . " ==\n" . $desc . "\n" .
397 '== ' . wfMsg ( 'filestatus' ) . " ==\n" . $copyStatus . "\n" .
398 '== ' . wfMsg ( 'filesource' ) . " ==\n" . $source ;
400 else $textdesc = $desc ;
402 $now = wfTimestampNow();
403 $won = wfInvertTimestamp( $now );
405 # Test to see if the row exists using INSERT IGNORE
406 # This avoids race conditions by locking the row until the commit, and also
407 # doesn't deadlock. SELECT FOR UPDATE causes a deadlock for every race condition.
408 $dbw->insertArray( 'image',
412 'img_timestamp' => $dbw->timestamp($now),
413 'img_description' => $desc,
414 'img_user' => $wgUser->getID(),
415 'img_user_text' => $wgUser->getName(),
418 $descTitle = Title
::makeTitleSafe( NS_IMAGE
, $name );
420 if ( $dbw->affectedRows() ) {
421 # Successfully inserted, this is a new image
422 $id = $descTitle->getArticleID();
425 $seqVal = $dbw->nextSequenceValue( 'cur_cur_id_seq' );
426 $dbw->insertArray( 'cur',
429 'cur_namespace' => NS_IMAGE
,
430 'cur_title' => $name,
431 'cur_comment' => $desc,
432 'cur_user' => $wgUser->getID(),
433 'cur_user_text' => $wgUser->getName(),
434 'cur_timestamp' => $dbw->timestamp($now),
436 'cur_text' => $textdesc,
437 'inverse_timestamp' => $won,
438 'cur_touched' => $dbw->timestamp($now)
441 $id = $dbw->insertId() or 0; # We should throw an error instead
443 RecentChange
::notifyNew( $now, $descTitle, 0, $wgUser, $desc );
445 $u = new SearchUpdate( $id, $name, $desc );
449 # Collision, this is an update of an image
450 # Get current image row for update
451 $s = $dbw->getArray( 'image', array( 'img_name','img_size','img_timestamp','img_description',
452 'img_user','img_user_text' ), array( 'img_name' => $name ), $fname, 'FOR UPDATE' );
454 # Insert it into oldimage
455 $dbw->insertArray( 'oldimage',
457 'oi_name' => $s->img_name
,
458 'oi_archive_name' => $oldver,
459 'oi_size' => $s->img_size
,
460 'oi_timestamp' => $dbw->timestamp($s->img_timestamp
),
461 'oi_description' => $s->img_description
,
462 'oi_user' => $s->img_user
,
463 'oi_user_text' => $s->img_user_text
467 # Update the current image row
468 $dbw->updateArray( 'image',
471 'img_timestamp' => $dbw->timestamp(),
472 'img_user' => $wgUser->getID(),
473 'img_user_text' => $wgUser->getName(),
474 'img_description' => $desc,
475 ), array( /* WHERE */
480 # Invalidate the cache for the description page
481 $descTitle->invalidateCache();
484 $log = new LogPage( 'upload' );
485 $log->addEntry( 'upload', $descTitle, $desc );
488 function wfImageArchiveUrl( $name )
490 global $wgUploadPath;
492 $hash = md5( substr( $name, 15) );
493 $url = $wgUploadPath.'/archive/' . $hash{0} . '/' .
494 substr( $hash, 0, 2 ) . '/'.$name;
495 return wfUrlencode($url);