7 * Class to represent an image
9 * Provides methods to retrieve paths (physical, logical, URL),
10 * to generate thumbnails or for uploading.
15 var $name, # name of the image
16 $imagePath, # Path of the image
18 $title, # Title object for this image. Initialized when needed.
19 $fileExists, # does the image file exist on disk?
20 $historyLine, # Number of line to return by nextHistoryLine()
21 $historyRes, # result of the query for the image's history
24 $bits, # --- returned by getimagesize, see http://de3.php.net/manual/en/function.getimagesize.php
30 function Image( $name )
32 global $wgUploadDirectory;
35 $this->title
= Title
::makeTitleSafe( NS_IMAGE
, $this->name
);
36 //$this->imagePath = wfImagePath( $name );
37 $hash = md5( $this->title
->getDBkey() );
38 $this->imagePath
= $wgUploadDirectory . '/' . $hash{0} . '/' .substr( $hash, 0, 2 ) . "/{$name}";
40 $this->url
= $this->wfImageUrl( $name );
42 if ( $this->fileExists
= file_exists( $this->imagePath
) ) // Sic!, "=" is intended
44 @$gis = getimagesize( $this->imagePath
);
45 if( $gis !== false ) {
46 $this->width
= $gis[0];
47 $this->height
= $gis[1];
48 $this->type
= $gis[2];
49 $this->attr
= $gis[3];
50 if ( isset( $gis['bits'] ) ) {
51 $this->bits
= $gis['bits'];
57 $this->historyLine
= 0;
60 function newFromTitle( $nt )
62 $img = new Image( $nt->getDBKey() );
77 function getImagePath()
79 return $this->imagePath
;
97 function getEscapeLocalURL()
99 return $this->title
->escapeLocalURL();
102 function wfImageUrl( $name )
104 global $wgUploadPath;
105 $hash = md5( $name );
107 $url = "{$wgUploadPath}/" . $hash{0} . "/" .
108 substr( $hash, 0, 2 ) . "/{$name}";
109 return wfUrlencode( $url );
115 return $this->fileExists
;
118 function thumbUrl( $width, $subdir='thumb' ) {
119 global $wgUploadPath;
121 $name = $this->thumbName( $width );
122 $hash = md5( $name );
123 $url = "{$wgUploadPath}/{$subdir}/" . $hash{0} . "/" . substr( $hash, 0, 2 ) . "/{$name}";
125 return wfUrlencode($url);
128 function thumbName( $width ) {
129 return $width."px-".$this->name
;
133 * Create a thumbnail of the image having the specified width.
134 * The thumbnail will not be created if the width is larger than the
135 * image's width. Let the browser do the scaling in this case.
136 * The thumbnail is stored on disk and is only computed if the thumbnail
137 * file does not exist OR if it is older than the image.
140 function createThumb( $width ) {
141 global $wgUploadDirectory;
142 global $wgImageMagickConvertCommand;
143 global $wgUseImageMagick;
144 global $wgUseSquid, $wgInternalServer;
146 $width = IntVal( $width );
148 $thumbName = $this->thumbName( $width );
149 $thumbPath = wfImageThumbDir( $thumbName ).'/'.$thumbName;
150 $thumbUrl = $this->thumbUrl( $width );
152 if ( ! $this->exists() )
154 # If there is no image, there will be no thumbnail
158 # Sanity check $width
164 if( $width > $this->width
) {
165 # Don't make an image bigger than the source
166 return $this->getURL();
169 if ( (! file_exists( $thumbPath ) ) ||
( filemtime($thumbPath) < filemtime($this->imagePath
) ) ) {
170 if ( $wgUseImageMagick ) {
172 # Specify white background color, will be used for transparent images
173 # in Internet Explorer/Windows instead of default black.
174 $cmd = $wgImageMagickConvertCommand .
175 " -quality 85 -background white -geometry {$width} ".
176 escapeshellarg($this->imagePath
) . " " .
177 escapeshellarg($thumbPath);
178 $conv = shell_exec( $cmd );
180 # Use PHP's builtin GD library functions.
182 # First find out what kind of file this is, and select the correct
183 # input routine for this.
187 switch( $this->type
) {
189 $src_image = imagecreatefromgif( $this->imagePath
);
192 $src_image = imagecreatefromjpeg( $this->imagePath
);
196 $src_image = imagecreatefrompng( $this->imagePath
);
197 $truecolor = ( $this->bits
> 8 );
199 case 15: # WBMP for WML
200 $src_image = imagecreatefromwbmp( $this->imagePath
);
203 $src_image = imagecreatefromxbm( $this->imagePath
);
206 return 'Image type not supported';
209 $height = floor( $this->height
* ( $width/$this->width
) );
211 $dst_image = imagecreatetruecolor( $width, $height );
213 $dst_image = imagecreate( $width, $height );
215 imagecopyresampled( $dst_image, $src_image,
217 $width, $height, $this->width
, $this->height
);
218 switch( $this->type
) {
223 #$thumbUrl .= ".png";
224 #$thumbPath .= ".png";
225 imagepng( $dst_image, $thumbPath );
228 #$thumbUrl .= ".jpg";
229 #$thumbPath .= ".jpg";
230 imageinterlace( $dst_image );
231 imagejpeg( $dst_image, $thumbPath, 95 );
236 imagedestroy( $dst_image );
237 imagedestroy( $src_image );
242 # Check for zero-sized thumbnails. Those can be generated when
243 # no disk space is available or some other error occurs
245 $thumbstat = stat( $thumbPath );
246 if( $thumbstat['size'] == 0 )
248 unlink( $thumbPath );
252 # This has to be done after the image is updated and present for all machines on NFS,
253 # or else the old version might be stored into the squid again
256 $wgInternalServer.$thumbUrl
258 wfPurgeSquidServers($urlArr);
262 } // END OF function createThumb
265 * Return the image history of this image, line by line.
266 * starts with current version, then old versions.
267 * uses $this->historyLine to check which line to return:
268 * 0 return line for current version
269 * 1 query for old versions, return first one
270 * 2, ... return next old version from above query
272 function nextHistoryLine()
274 $fname = 'Image::nextHistoryLine()';
275 $dbr =& wfGetDB( DB_SLAVE
);
276 if ( $this->historyLine
== 0 ) {// called for the first time, return line from cur
277 $this->historyRes
= $dbr->select( 'image',
278 array( 'img_size','img_description','img_user','img_user_text','img_timestamp', "'' AS oi_archive_name" ),
279 array( 'img_name' => $this->title
->getDBkey() ),
282 if ( 0 == wfNumRows( $this->historyRes
) ) {
285 } else if ( $this->historyLine
== 1 ) {
286 $this->historyRes
= $dbr->select( 'oldimage',
287 array( 'oi_size AS img_size', 'oi_description AS img_description', 'oi_user AS img_user',
288 'oi_user_text AS img_user_text', 'oi_timestamp AS img_timestamp', 'oi_archive_name'
289 ), array( 'oi_name' => $this->title
->getDBkey() ), $fname, array( 'ORDER BY' => 'oi_timestamp DESC' )
292 $this->historyLine ++
;
294 return $dbr->fetchObject( $this->historyRes
);
297 function resetHistory()
299 $this->historyLine
= 0;
306 function wfImageDir( $fname )
308 global $wgUploadDirectory;
310 $hash = md5( $fname );
311 $oldumask = umask(0);
312 $dest = $wgUploadDirectory . '/' . $hash{0};
313 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
314 $dest .= '/' . substr( $hash, 0, 2 );
315 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
321 function wfImageThumbDir( $fname , $subdir='thumb')
323 return wfImageArchiveDir( $fname, $subdir );
326 function wfImageArchiveDir( $fname , $subdir='archive')
328 global $wgUploadDirectory;
330 $hash = md5( $fname );
331 $oldumask = umask(0);
333 # Suppress warning messages here; if the file itself can't
334 # be written we'll worry about it then.
335 $archive = $wgUploadDirectory.'/'.$subdir;
336 if ( ! is_dir( $archive ) ) { @mkdir
( $archive, 0777 ); }
337 $archive .= '/' . $hash{0};
338 if ( ! is_dir( $archive ) ) { @mkdir
( $archive, 0777 ); }
339 $archive .= '/' . substr( $hash, 0, 2 );
340 if ( ! is_dir( $archive ) ) { @mkdir
( $archive, 0777 ); }
346 function wfRecordUpload( $name, $oldver, $size, $desc, $copyStatus = "", $source = "" )
348 global $wgUser, $wgLang, $wgTitle, $wgOut, $wgDeferredUpdateList;
349 global $wgUseCopyrightUpload;
351 $fname = 'wfRecordUpload';
352 $dbw =& wfGetDB( DB_MASTER
);
354 # img_name must be unique
355 if ( !$dbw->indexUnique( 'image', 'img_name' ) ) {
356 wfDebugDieBacktrace( 'Database schema not up to date, please run maintenance/archives/patch-image_name_unique.sql' );
360 $now = wfTimestampNow();
361 $won = wfInvertTimestamp( $now );
362 $size = IntVal( $size );
364 if ( $wgUseCopyrightUpload )
366 $textdesc = '== ' . wfMsg ( 'filedesc' ) . " ==\n" . $desc . "\n" .
367 '== ' . wfMsg ( 'filestatus' ) . " ==\n" . $copyStatus . "\n" .
368 '== ' . wfMsg ( 'filesource' ) . " ==\n" . $source ;
370 else $textdesc = $desc ;
372 $now = wfTimestampNow();
373 $won = wfInvertTimestamp( $now );
375 # Test to see if the row exists using INSERT IGNORE
376 # This avoids race conditions by locking the row until the commit, and also
377 # doesn't deadlock. SELECT FOR UPDATE causes a deadlock for every race condition.
378 $dbw->insert( 'image',
382 'img_timestamp' => $now,
383 'img_description' => $desc,
384 'img_user' => $wgUser->getID(),
385 'img_user_text' => $wgUser->getName(),
388 $descTitle = Title
::makeTitleSafe( NS_IMAGE
, $name );
390 if ( $dbw->affectedRows() ) {
391 # Successfully inserted, this is a new image
392 $id = $descTitle->getArticleID();
395 $seqVal = $dbw->nextSequenceValue( 'cur_cur_id_seq' );
396 $dbw->insertArray( 'cur',
399 'cur_namespace' => NS_IMAGE
,
400 'cur_title' => $name,
401 'cur_comment' => $desc,
402 'cur_user' => $wgUser->getID(),
403 'cur_user_text' => $wgUser->getName(),
404 'cur_timestamp' => $now,
406 'cur_text' => $textdesc,
407 'inverse_timestamp' => $won,
408 'cur_touched' => $now
411 $id = $dbw->insertId() or 0; # We should throw an error instead
413 RecentChange
::notifyNew( $now, $descTitle, 0, $wgUser, $desc );
415 $u = new SearchUpdate( $id, $name, $desc );
419 # Collision, this is an update of an image
420 # Get current image row for update
421 $s = $dbw->getArray( 'image', array( 'img_name','img_size','img_timestamp','img_description',
422 'img_user','img_user_text' ), array( 'img_name' => $name ), $fname, 'FOR UPDATE' );
424 # Insert it into oldimage
425 $dbw->insertArray( 'oldimage',
427 'oi_name' => $s->img_name
,
428 'oi_archive_name' => $oldver,
429 'oi_size' => $s->img_size
,
430 'oi_timestamp' => $s->img_timestamp
,
431 'oi_description' => $s->img_description
,
432 'oi_user' => $s->img_user
,
433 'oi_user_text' => $s->img_user_text
437 # Update the current image row
438 $dbw->updateArray( 'image',
441 'img_timestamp' => wfTimestampNow(),
442 'img_user' => $wgUser->getID(),
443 'img_user_text' => $wgUser->getName(),
444 'img_description' => $desc,
445 ), array( /* WHERE */
450 # Invalidate the cache for the description page
451 $descTitle->invalidateCache();
454 $log = new LogPage( 'upload' );
455 $log->addEntry( 'upload', $descTitle, $desc );
458 function wfImageArchiveUrl( $name )
460 global $wgUploadPath;
462 $hash = md5( substr( $name, 15) );
463 $url = $wgUploadPath.'/archive/' . $hash{0} . '/' .
464 substr( $hash, 0, 2 ) . '/'.$name;
465 return wfUrlencode($url);