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() );
79 function getImagePath()
81 return $this->imagePath
;
96 $st = stat( $this->getImagePath() );
105 function getEscapeLocalURL()
107 return $this->title
->escapeLocalURL();
110 function wfImageUrl( $name )
112 global $wgUploadPath;
113 $hash = md5( $name );
115 $url = "{$wgUploadPath}/" . $hash{0} . "/" .
116 substr( $hash, 0, 2 ) . "/{$name}";
117 return wfUrlencode( $url );
123 return $this->fileExists
;
126 function thumbUrl( $width, $subdir='thumb' ) {
127 global $wgUploadPath;
129 $name = $this->thumbName( $width );
130 $hash = md5( $name );
131 $url = "{$wgUploadPath}/{$subdir}/" . $hash{0} . "/" . substr( $hash, 0, 2 ) . "/{$name}";
133 return wfUrlencode($url);
136 function thumbName( $width ) {
137 return $width."px-".$this->name
;
141 * Create a thumbnail of the image having the specified width.
142 * The thumbnail will not be created if the width is larger than the
143 * image's width. Let the browser do the scaling in this case.
144 * The thumbnail is stored on disk and is only computed if the thumbnail
145 * file does not exist OR if it is older than the image.
148 function createThumb( $width ) {
149 global $wgUploadDirectory;
150 global $wgImageMagickConvertCommand;
151 global $wgUseImageMagick;
152 global $wgUseSquid, $wgInternalServer;
154 $width = IntVal( $width );
156 $thumbName = $this->thumbName( $width );
157 $thumbPath = wfImageThumbDir( $thumbName ).'/'.$thumbName;
158 $thumbUrl = $this->thumbUrl( $width );
160 if ( ! $this->exists() )
162 # If there is no image, there will be no thumbnail
166 # Sanity check $width
172 if( $width > $this->width
) {
173 # Don't make an image bigger than the source
174 return $this->getURL();
177 if ( (! file_exists( $thumbPath ) ) ||
( filemtime($thumbPath) < filemtime($this->imagePath
) ) ) {
178 if ( $wgUseImageMagick ) {
180 # Specify white background color, will be used for transparent images
181 # in Internet Explorer/Windows instead of default black.
182 $cmd = $wgImageMagickConvertCommand .
183 " -quality 85 -background white -geometry {$width} ".
184 escapeshellarg($this->imagePath
) . " " .
185 escapeshellarg($thumbPath);
186 $conv = shell_exec( $cmd );
188 # Use PHP's builtin GD library functions.
190 # First find out what kind of file this is, and select the correct
191 # input routine for this.
195 switch( $this->type
) {
197 $src_image = imagecreatefromgif( $this->imagePath
);
200 $src_image = imagecreatefromjpeg( $this->imagePath
);
204 $src_image = imagecreatefrompng( $this->imagePath
);
205 $truecolor = ( $this->bits
> 8 );
207 case 15: # WBMP for WML
208 $src_image = imagecreatefromwbmp( $this->imagePath
);
211 $src_image = imagecreatefromxbm( $this->imagePath
);
214 return 'Image type not supported';
217 $height = floor( $this->height
* ( $width/$this->width
) );
219 $dst_image = imagecreatetruecolor( $width, $height );
221 $dst_image = imagecreate( $width, $height );
223 imagecopyresampled( $dst_image, $src_image,
225 $width, $height, $this->width
, $this->height
);
226 switch( $this->type
) {
231 #$thumbUrl .= ".png";
232 #$thumbPath .= ".png";
233 imagepng( $dst_image, $thumbPath );
236 #$thumbUrl .= ".jpg";
237 #$thumbPath .= ".jpg";
238 imageinterlace( $dst_image );
239 imagejpeg( $dst_image, $thumbPath, 95 );
244 imagedestroy( $dst_image );
245 imagedestroy( $src_image );
250 # Check for zero-sized thumbnails. Those can be generated when
251 # no disk space is available or some other error occurs
253 $thumbstat = stat( $thumbPath );
254 if( $thumbstat['size'] == 0 )
256 unlink( $thumbPath );
260 # This has to be done after the image is updated and present for all machines on NFS,
261 # or else the old version might be stored into the squid again
264 $wgInternalServer.$thumbUrl
266 wfPurgeSquidServers($urlArr);
270 } // END OF function createThumb
273 * Return the image history of this image, line by line.
274 * starts with current version, then old versions.
275 * uses $this->historyLine to check which line to return:
276 * 0 return line for current version
277 * 1 query for old versions, return first one
278 * 2, ... return next old version from above query
280 function nextHistoryLine()
282 $fname = 'Image::nextHistoryLine()';
283 $dbr =& wfGetDB( DB_SLAVE
);
284 if ( $this->historyLine
== 0 ) {// called for the first time, return line from cur
285 $this->historyRes
= $dbr->select( 'image',
286 array( 'img_size','img_description','img_user','img_user_text','img_timestamp', "'' AS oi_archive_name" ),
287 array( 'img_name' => $this->title
->getDBkey() ),
290 if ( 0 == wfNumRows( $this->historyRes
) ) {
293 } else if ( $this->historyLine
== 1 ) {
294 $this->historyRes
= $dbr->select( 'oldimage',
295 array( 'oi_size AS img_size', 'oi_description AS img_description', 'oi_user AS img_user',
296 'oi_user_text AS img_user_text', 'oi_timestamp AS img_timestamp', 'oi_archive_name'
297 ), array( 'oi_name' => $this->title
->getDBkey() ), $fname, array( 'ORDER BY' => 'oi_timestamp DESC' )
300 $this->historyLine ++
;
302 return $dbr->fetchObject( $this->historyRes
);
305 function resetHistory()
307 $this->historyLine
= 0;
314 function wfImageDir( $fname )
316 global $wgUploadDirectory;
318 $hash = md5( $fname );
319 $oldumask = umask(0);
320 $dest = $wgUploadDirectory . '/' . $hash{0};
321 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
322 $dest .= '/' . substr( $hash, 0, 2 );
323 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
329 function wfImageThumbDir( $fname , $subdir='thumb')
331 return wfImageArchiveDir( $fname, $subdir );
334 function wfImageArchiveDir( $fname , $subdir='archive')
336 global $wgUploadDirectory;
338 $hash = md5( $fname );
339 $oldumask = umask(0);
341 # Suppress warning messages here; if the file itself can't
342 # be written we'll worry about it then.
343 $archive = $wgUploadDirectory.'/'.$subdir;
344 if ( ! is_dir( $archive ) ) { @mkdir
( $archive, 0777 ); }
345 $archive .= '/' . $hash{0};
346 if ( ! is_dir( $archive ) ) { @mkdir
( $archive, 0777 ); }
347 $archive .= '/' . substr( $hash, 0, 2 );
348 if ( ! is_dir( $archive ) ) { @mkdir
( $archive, 0777 ); }
354 function wfRecordUpload( $name, $oldver, $size, $desc, $copyStatus = "", $source = "" )
356 global $wgUser, $wgLang, $wgTitle, $wgOut, $wgDeferredUpdateList;
357 global $wgUseCopyrightUpload;
359 $fname = 'wfRecordUpload';
360 $dbw =& wfGetDB( DB_MASTER
);
362 # img_name must be unique
363 if ( !$dbw->indexUnique( 'image', 'img_name' ) ) {
364 wfDebugDieBacktrace( 'Database schema not up to date, please run maintenance/archives/patch-image_name_unique.sql' );
368 $now = wfTimestampNow();
369 $won = wfInvertTimestamp( $now );
370 $size = IntVal( $size );
372 if ( $wgUseCopyrightUpload )
374 $textdesc = '== ' . wfMsg ( 'filedesc' ) . " ==\n" . $desc . "\n" .
375 '== ' . wfMsg ( 'filestatus' ) . " ==\n" . $copyStatus . "\n" .
376 '== ' . wfMsg ( 'filesource' ) . " ==\n" . $source ;
378 else $textdesc = $desc ;
380 $now = wfTimestampNow();
381 $won = wfInvertTimestamp( $now );
383 # Test to see if the row exists using INSERT IGNORE
384 # This avoids race conditions by locking the row until the commit, and also
385 # doesn't deadlock. SELECT FOR UPDATE causes a deadlock for every race condition.
386 $dbw->insertArray( 'image',
390 'img_timestamp' => $dbw->timestamp($now),
391 'img_description' => $desc,
392 'img_user' => $wgUser->getID(),
393 'img_user_text' => $wgUser->getName(),
396 $descTitle = Title
::makeTitleSafe( NS_IMAGE
, $name );
398 if ( $dbw->affectedRows() ) {
399 # Successfully inserted, this is a new image
400 $id = $descTitle->getArticleID();
403 $seqVal = $dbw->nextSequenceValue( 'cur_cur_id_seq' );
404 $dbw->insertArray( 'cur',
407 'cur_namespace' => NS_IMAGE
,
408 'cur_title' => $name,
409 'cur_comment' => $desc,
410 'cur_user' => $wgUser->getID(),
411 'cur_user_text' => $wgUser->getName(),
412 'cur_timestamp' => $dbw->timestamp($now),
414 'cur_text' => $textdesc,
415 'inverse_timestamp' => $won,
416 'cur_touched' => $dbw->timestamp($now)
419 $id = $dbw->insertId() or 0; # We should throw an error instead
421 RecentChange
::notifyNew( $now, $descTitle, 0, $wgUser, $desc );
423 $u = new SearchUpdate( $id, $name, $desc );
427 # Collision, this is an update of an image
428 # Get current image row for update
429 $s = $dbw->getArray( 'image', array( 'img_name','img_size','img_timestamp','img_description',
430 'img_user','img_user_text' ), array( 'img_name' => $name ), $fname, 'FOR UPDATE' );
432 # Insert it into oldimage
433 $dbw->insertArray( 'oldimage',
435 'oi_name' => $s->img_name
,
436 'oi_archive_name' => $oldver,
437 'oi_size' => $s->img_size
,
438 'oi_timestamp' => $dbw->timestamp($s->img_timestamp
),
439 'oi_description' => $s->img_description
,
440 'oi_user' => $s->img_user
,
441 'oi_user_text' => $s->img_user_text
445 # Update the current image row
446 $dbw->updateArray( 'image',
449 'img_timestamp' => $dbw->timestamp(),
450 'img_user' => $wgUser->getID(),
451 'img_user_text' => $wgUser->getName(),
452 'img_description' => $desc,
453 ), array( /* WHERE */
458 # Invalidate the cache for the description page
459 $descTitle->invalidateCache();
462 $log = new LogPage( 'upload' );
463 $log->addEntry( 'upload', $descTitle, $desc );
466 function wfImageArchiveUrl( $name )
468 global $wgUploadPath;
470 $hash = md5( substr( $name, 15) );
471 $url = $wgUploadPath.'/archive/' . $hash{0} . '/' .
472 substr( $hash, 0, 2 ) . '/'.$name;
473 return wfUrlencode($url);