7 * Class to represent an image
9 * Provides methods to retrieve paths (physical, logical, URL),
10 * to generate thumbnails or for uploading.
16 var $name, # name of the image
17 $imagePath, # Path of the image
19 $title, # Title object for this image. Initialized when needed.
20 $fileExists, # does the image file exist on disk?
21 $historyLine, # Number of line to return by nextHistoryLine()
22 $historyRes, # result of the query for the image's history
25 $bits, # --- returned by getimagesize, see http://de3.php.net/manual/en/function.getimagesize.php
31 function Image( $name )
33 global $wgUploadDirectory;
36 $this->title
= Title
::makeTitleSafe( NS_IMAGE
, $this->name
);
37 //$this->imagePath = wfImagePath( $name );
38 $hash = md5( $this->title
->getDBkey() );
39 $this->imagePath
= $wgUploadDirectory . '/' . $hash{0} . '/' .substr( $hash, 0, 2 ) . "/{$name}";
41 $this->url
= $this->wfImageUrl( $name );
43 if ( $this->fileExists
= file_exists( $this->imagePath
) ) // Sic!, "=" is intended
45 @$gis = getimagesize( $this->imagePath
);
46 if( $gis !== false ) {
47 $this->width
= $gis[0];
48 $this->height
= $gis[1];
49 $this->type
= $gis[2];
50 $this->attr
= $gis[3];
51 if ( isset( $gis['bits'] ) ) {
52 $this->bits
= $gis['bits'];
58 $this->historyLine
= 0;
61 function newFromTitle( $nt )
63 $img = new Image( $nt->getDBKey() );
78 function getImagePath()
80 return $this->imagePath
;
98 function getEscapeLocalURL()
100 return $this->title
->escapeLocalURL();
103 function wfImageUrl( $name )
105 global $wgUploadPath;
106 $hash = md5( $name );
108 $url = "{$wgUploadPath}/" . $hash{0} . "/" .
109 substr( $hash, 0, 2 ) . "/{$name}";
110 return wfUrlencode( $url );
116 return $this->fileExists
;
119 function thumbUrl( $width, $subdir='thumb' ) {
120 global $wgUploadPath;
122 $name = $this->thumbName( $width );
123 $hash = md5( $name );
124 $url = "{$wgUploadPath}/{$subdir}/" . $hash{0} . "/" . substr( $hash, 0, 2 ) . "/{$name}";
126 return wfUrlencode($url);
129 function thumbName( $width ) {
130 return $width."px-".$this->name
;
134 * Create a thumbnail of the image having the specified width.
135 * The thumbnail will not be created if the width is larger than the
136 * image's width. Let the browser do the scaling in this case.
137 * The thumbnail is stored on disk and is only computed if the thumbnail
138 * file does not exist OR if it is older than the image.
141 function createThumb( $width ) {
142 global $wgUploadDirectory;
143 global $wgImageMagickConvertCommand;
144 global $wgUseImageMagick;
145 global $wgUseSquid, $wgInternalServer;
147 $width = IntVal( $width );
149 $thumbName = $this->thumbName( $width );
150 $thumbPath = wfImageThumbDir( $thumbName ).'/'.$thumbName;
151 $thumbUrl = $this->thumbUrl( $width );
153 if ( ! $this->exists() )
155 # If there is no image, there will be no thumbnail
159 # Sanity check $width
165 if( $width > $this->width
) {
166 # Don't make an image bigger than the source
167 return $this->getURL();
170 if ( (! file_exists( $thumbPath ) ) ||
( filemtime($thumbPath) < filemtime($this->imagePath
) ) ) {
171 if ( $wgUseImageMagick ) {
173 # Specify white background color, will be used for transparent images
174 # in Internet Explorer/Windows instead of default black.
175 $cmd = $wgImageMagickConvertCommand .
176 " -quality 85 -background white -geometry {$width} ".
177 escapeshellarg($this->imagePath
) . " " .
178 escapeshellarg($thumbPath);
179 $conv = shell_exec( $cmd );
181 # Use PHP's builtin GD library functions.
183 # First find out what kind of file this is, and select the correct
184 # input routine for this.
188 switch( $this->type
) {
190 $src_image = imagecreatefromgif( $this->imagePath
);
193 $src_image = imagecreatefromjpeg( $this->imagePath
);
197 $src_image = imagecreatefrompng( $this->imagePath
);
198 $truecolor = ( $this->bits
> 8 );
200 case 15: # WBMP for WML
201 $src_image = imagecreatefromwbmp( $this->imagePath
);
204 $src_image = imagecreatefromxbm( $this->imagePath
);
207 return 'Image type not supported';
210 $height = floor( $this->height
* ( $width/$this->width
) );
212 $dst_image = imagecreatetruecolor( $width, $height );
214 $dst_image = imagecreate( $width, $height );
216 imagecopyresampled( $dst_image, $src_image,
218 $width, $height, $this->width
, $this->height
);
219 switch( $this->type
) {
224 #$thumbUrl .= ".png";
225 #$thumbPath .= ".png";
226 imagepng( $dst_image, $thumbPath );
229 #$thumbUrl .= ".jpg";
230 #$thumbPath .= ".jpg";
231 imageinterlace( $dst_image );
232 imagejpeg( $dst_image, $thumbPath, 95 );
237 imagedestroy( $dst_image );
238 imagedestroy( $src_image );
243 # Check for zero-sized thumbnails. Those can be generated when
244 # no disk space is available or some other error occurs
246 $thumbstat = stat( $thumbPath );
247 if( $thumbstat['size'] == 0 )
249 unlink( $thumbPath );
253 # This has to be done after the image is updated and present for all machines on NFS,
254 # or else the old version might be stored into the squid again
257 $wgInternalServer.$thumbUrl
259 wfPurgeSquidServers($urlArr);
263 } // END OF function createThumb
266 * Return the image history of this image, line by line.
267 * starts with current version, then old versions.
268 * uses $this->historyLine to check which line to return:
269 * 0 return line for current version
270 * 1 query for old versions, return first one
271 * 2, ... return next old version from above query
273 function nextHistoryLine()
275 $fname = 'Image::nextHistoryLine()';
276 $dbr =& wfGetDB( DB_SLAVE
);
277 if ( $this->historyLine
== 0 ) {// called for the first time, return line from cur
278 $this->historyRes
= $dbr->select( 'image',
279 array( 'img_size','img_description','img_user','img_user_text','img_timestamp', "'' AS oi_archive_name" ),
280 array( 'img_name' => $this->title
->getDBkey() ),
283 if ( 0 == wfNumRows( $this->historyRes
) ) {
286 } else if ( $this->historyLine
== 1 ) {
287 $this->historyRes
= $dbr->select( 'oldimage',
288 array( 'oi_size AS img_size', 'oi_description AS img_description', 'oi_user AS img_user',
289 'oi_user_text AS img_user_text', 'oi_timestamp AS img_timestamp', 'oi_archive_name'
290 ), array( 'oi_name' => $this->title
->getDBkey() ), $fname, array( 'ORDER BY' => 'oi_timestamp DESC' )
293 $this->historyLine ++
;
295 return $dbr->fetchObject( $this->historyRes
);
298 function resetHistory()
300 $this->historyLine
= 0;
307 function wfImageDir( $fname )
309 global $wgUploadDirectory;
311 $hash = md5( $fname );
312 $oldumask = umask(0);
313 $dest = $wgUploadDirectory . '/' . $hash{0};
314 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
315 $dest .= '/' . substr( $hash, 0, 2 );
316 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
322 function wfImageThumbDir( $fname , $subdir='thumb')
324 return wfImageArchiveDir( $fname, $subdir );
327 function wfImageArchiveDir( $fname , $subdir='archive')
329 global $wgUploadDirectory;
331 $hash = md5( $fname );
332 $oldumask = umask(0);
334 # Suppress warning messages here; if the file itself can't
335 # be written we'll worry about it then.
336 $archive = $wgUploadDirectory.'/'.$subdir;
337 if ( ! is_dir( $archive ) ) { @mkdir
( $archive, 0777 ); }
338 $archive .= '/' . $hash{0};
339 if ( ! is_dir( $archive ) ) { @mkdir
( $archive, 0777 ); }
340 $archive .= '/' . substr( $hash, 0, 2 );
341 if ( ! is_dir( $archive ) ) { @mkdir
( $archive, 0777 ); }
347 function wfRecordUpload( $name, $oldver, $size, $desc, $copyStatus = "", $source = "" )
349 global $wgUser, $wgLang, $wgTitle, $wgOut, $wgDeferredUpdateList;
350 global $wgUseCopyrightUpload;
352 $fname = 'wfRecordUpload';
353 $dbw =& wfGetDB( DB_MASTER
);
355 # img_name must be unique
356 if ( !$dbw->indexUnique( 'image', 'img_name' ) ) {
357 wfDebugDieBacktrace( 'Database schema not up to date, please run maintenance/archives/patch-image_name_unique.sql' );
361 $now = wfTimestampNow();
362 $won = wfInvertTimestamp( $now );
363 $size = IntVal( $size );
365 if ( $wgUseCopyrightUpload )
367 $textdesc = '== ' . wfMsg ( 'filedesc' ) . " ==\n" . $desc . "\n" .
368 '== ' . wfMsg ( 'filestatus' ) . " ==\n" . $copyStatus . "\n" .
369 '== ' . wfMsg ( 'filesource' ) . " ==\n" . $source ;
371 else $textdesc = $desc ;
373 $now = wfTimestampNow();
374 $won = wfInvertTimestamp( $now );
376 # Test to see if the row exists using INSERT IGNORE
377 # This avoids race conditions by locking the row until the commit, and also
378 # doesn't deadlock. SELECT FOR UPDATE causes a deadlock for every race condition.
379 $dbw->insert( 'image',
383 'img_timestamp' => $now,
384 'img_description' => $desc,
385 'img_user' => $wgUser->getID(),
386 'img_user_text' => $wgUser->getName(),
389 $descTitle = Title
::makeTitleSafe( NS_IMAGE
, $name );
391 if ( $dbw->affectedRows() ) {
392 # Successfully inserted, this is a new image
393 $id = $descTitle->getArticleID();
396 $seqVal = $dbw->nextSequenceValue( 'cur_cur_id_seq' );
397 $dbw->insertArray( 'cur',
400 'cur_namespace' => NS_IMAGE
,
401 'cur_title' => $name,
402 'cur_comment' => $desc,
403 'cur_user' => $wgUser->getID(),
404 'cur_user_text' => $wgUser->getName(),
405 'cur_timestamp' => $now,
407 'cur_text' => $textdesc,
408 'inverse_timestamp' => $won,
409 'cur_touched' => $now
412 $id = $dbw->insertId() or 0; # We should throw an error instead
414 RecentChange
::notifyNew( $now, $descTitle, 0, $wgUser, $desc );
416 $u = new SearchUpdate( $id, $name, $desc );
420 # Collision, this is an update of an image
421 # Get current image row for update
422 $s = $dbw->getArray( 'image', array( 'img_name','img_size','img_timestamp','img_description',
423 'img_user','img_user_text' ), array( 'img_name' => $name ), $fname, 'FOR UPDATE' );
425 # Insert it into oldimage
426 $dbw->insertArray( 'oldimage',
428 'oi_name' => $s->img_name
,
429 'oi_archive_name' => $oldver,
430 'oi_size' => $s->img_size
,
431 'oi_timestamp' => $s->img_timestamp
,
432 'oi_description' => $s->img_description
,
433 'oi_user' => $s->img_user
,
434 'oi_user_text' => $s->img_user_text
438 # Update the current image row
439 $dbw->updateArray( 'image',
442 'img_timestamp' => wfTimestampNow(),
443 'img_user' => $wgUser->getID(),
444 'img_user_text' => $wgUser->getName(),
445 'img_description' => $desc,
446 ), array( /* WHERE */
451 # Invalidate the cache for the description page
452 $descTitle->invalidateCache();
455 $log = new LogPage( 'upload' );
456 $log->addEntry( 'upload', $descTitle, $desc );
459 function wfImageArchiveUrl( $name )
461 global $wgUploadPath;
463 $hash = md5( substr( $name, 15) );
464 $url = $wgUploadPath.'/archive/' . $hash{0} . '/' .
465 substr( $hash, 0, 2 ) . '/'.$name;
466 return wfUrlencode($url);