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
;
99 function getEscapeLocalURL()
101 return $this->title
->escapeLocalURL();
104 function wfImageUrl( $name )
106 global $wgUploadPath;
107 $hash = md5( $name );
109 $url = "{$wgUploadPath}/" . $hash{0} . "/" .
110 substr( $hash, 0, 2 ) . "/{$name}";
111 return wfUrlencode( $url );
117 return $this->fileExists
;
120 function thumbUrl( $width, $subdir='thumb' ) {
121 global $wgUploadPath;
123 $name = $this->thumbName( $width );
124 $hash = md5( $name );
125 $url = "{$wgUploadPath}/{$subdir}/" . $hash{0} . "/" . substr( $hash, 0, 2 ) . "/{$name}";
127 return wfUrlencode($url);
130 function thumbName( $width ) {
131 return $width."px-".$this->name
;
135 * Create a thumbnail of the image having the specified width.
136 * The thumbnail will not be created if the width is larger than the
137 * image's width. Let the browser do the scaling in this case.
138 * The thumbnail is stored on disk and is only computed if the thumbnail
139 * file does not exist OR if it is older than the image.
142 function createThumb( $width ) {
143 global $wgUploadDirectory;
144 global $wgImageMagickConvertCommand;
145 global $wgUseImageMagick;
146 global $wgUseSquid, $wgInternalServer;
148 $width = IntVal( $width );
150 $thumbName = $this->thumbName( $width );
151 $thumbPath = wfImageThumbDir( $thumbName ).'/'.$thumbName;
152 $thumbUrl = $this->thumbUrl( $width );
154 if ( ! $this->exists() )
156 # If there is no image, there will be no thumbnail
160 # Sanity check $width
166 if( $width > $this->width
) {
167 # Don't make an image bigger than the source
168 return $this->getURL();
171 if ( (! file_exists( $thumbPath ) ) ||
( filemtime($thumbPath) < filemtime($this->imagePath
) ) ) {
172 if ( $wgUseImageMagick ) {
174 # Specify white background color, will be used for transparent images
175 # in Internet Explorer/Windows instead of default black.
176 $cmd = $wgImageMagickConvertCommand .
177 " -quality 85 -background white -geometry {$width} ".
178 escapeshellarg($this->imagePath
) . " " .
179 escapeshellarg($thumbPath);
180 $conv = shell_exec( $cmd );
182 # Use PHP's builtin GD library functions.
184 # First find out what kind of file this is, and select the correct
185 # input routine for this.
189 switch( $this->type
) {
191 $src_image = imagecreatefromgif( $this->imagePath
);
194 $src_image = imagecreatefromjpeg( $this->imagePath
);
198 $src_image = imagecreatefrompng( $this->imagePath
);
199 $truecolor = ( $this->bits
> 8 );
201 case 15: # WBMP for WML
202 $src_image = imagecreatefromwbmp( $this->imagePath
);
205 $src_image = imagecreatefromxbm( $this->imagePath
);
208 return 'Image type not supported';
211 $height = floor( $this->height
* ( $width/$this->width
) );
213 $dst_image = imagecreatetruecolor( $width, $height );
215 $dst_image = imagecreate( $width, $height );
217 imagecopyresampled( $dst_image, $src_image,
219 $width, $height, $this->width
, $this->height
);
220 switch( $this->type
) {
225 #$thumbUrl .= ".png";
226 #$thumbPath .= ".png";
227 imagepng( $dst_image, $thumbPath );
230 #$thumbUrl .= ".jpg";
231 #$thumbPath .= ".jpg";
232 imageinterlace( $dst_image );
233 imagejpeg( $dst_image, $thumbPath, 95 );
238 imagedestroy( $dst_image );
239 imagedestroy( $src_image );
244 # Check for zero-sized thumbnails. Those can be generated when
245 # no disk space is available or some other error occurs
247 $thumbstat = stat( $thumbPath );
248 if( $thumbstat['size'] == 0 )
250 unlink( $thumbPath );
254 # This has to be done after the image is updated and present for all machines on NFS,
255 # or else the old version might be stored into the squid again
258 $wgInternalServer.$thumbUrl
260 wfPurgeSquidServers($urlArr);
264 } // END OF function createThumb
267 * Return the image history of this image, line by line.
268 * starts with current version, then old versions.
269 * uses $this->historyLine to check which line to return:
270 * 0 return line for current version
271 * 1 query for old versions, return first one
272 * 2, ... return next old version from above query
274 function nextHistoryLine()
276 $fname = 'Image::nextHistoryLine()';
277 $dbr =& wfGetDB( DB_SLAVE
);
278 if ( $this->historyLine
== 0 ) {// called for the first time, return line from cur
279 $this->historyRes
= $dbr->select( 'image',
280 array( 'img_size','img_description','img_user','img_user_text','img_timestamp', "'' AS oi_archive_name" ),
281 array( 'img_name' => $this->title
->getDBkey() ),
284 if ( 0 == wfNumRows( $this->historyRes
) ) {
287 } else if ( $this->historyLine
== 1 ) {
288 $this->historyRes
= $dbr->select( 'oldimage',
289 array( 'oi_size AS img_size', 'oi_description AS img_description', 'oi_user AS img_user',
290 'oi_user_text AS img_user_text', 'oi_timestamp AS img_timestamp', 'oi_archive_name'
291 ), array( 'oi_name' => $this->title
->getDBkey() ), $fname, array( 'ORDER BY' => 'oi_timestamp DESC' )
294 $this->historyLine ++
;
296 return $dbr->fetchObject( $this->historyRes
);
299 function resetHistory()
301 $this->historyLine
= 0;
308 function wfImageDir( $fname )
310 global $wgUploadDirectory;
312 $hash = md5( $fname );
313 $oldumask = umask(0);
314 $dest = $wgUploadDirectory . '/' . $hash{0};
315 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
316 $dest .= '/' . substr( $hash, 0, 2 );
317 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
323 function wfImageThumbDir( $fname , $subdir='thumb')
325 return wfImageArchiveDir( $fname, $subdir );
328 function wfImageArchiveDir( $fname , $subdir='archive')
330 global $wgUploadDirectory;
332 $hash = md5( $fname );
333 $oldumask = umask(0);
335 # Suppress warning messages here; if the file itself can't
336 # be written we'll worry about it then.
337 $archive = $wgUploadDirectory.'/'.$subdir;
338 if ( ! is_dir( $archive ) ) { @mkdir
( $archive, 0777 ); }
339 $archive .= '/' . $hash{0};
340 if ( ! is_dir( $archive ) ) { @mkdir
( $archive, 0777 ); }
341 $archive .= '/' . substr( $hash, 0, 2 );
342 if ( ! is_dir( $archive ) ) { @mkdir
( $archive, 0777 ); }
348 function wfRecordUpload( $name, $oldver, $size, $desc, $copyStatus = "", $source = "" )
350 global $wgUser, $wgLang, $wgTitle, $wgOut, $wgDeferredUpdateList;
351 global $wgUseCopyrightUpload;
353 $fname = 'wfRecordUpload';
354 $dbw =& wfGetDB( DB_MASTER
);
356 # img_name must be unique
357 if ( !$dbw->indexUnique( 'image', 'img_name' ) ) {
358 wfDebugDieBacktrace( 'Database schema not up to date, please run maintenance/archives/patch-image_name_unique.sql' );
362 $now = wfTimestampNow();
363 $won = wfInvertTimestamp( $now );
364 $size = IntVal( $size );
366 if ( $wgUseCopyrightUpload )
368 $textdesc = '== ' . wfMsg ( 'filedesc' ) . " ==\n" . $desc . "\n" .
369 '== ' . wfMsg ( 'filestatus' ) . " ==\n" . $copyStatus . "\n" .
370 '== ' . wfMsg ( 'filesource' ) . " ==\n" . $source ;
372 else $textdesc = $desc ;
374 $now = wfTimestampNow();
375 $won = wfInvertTimestamp( $now );
377 # Test to see if the row exists using INSERT IGNORE
378 # This avoids race conditions by locking the row until the commit, and also
379 # doesn't deadlock. SELECT FOR UPDATE causes a deadlock for every race condition.
380 $dbw->insertArray( 'image',
384 'img_timestamp' => $dbw->timestamp($now),
385 'img_description' => $desc,
386 'img_user' => $wgUser->getID(),
387 'img_user_text' => $wgUser->getName(),
390 $descTitle = Title
::makeTitleSafe( NS_IMAGE
, $name );
392 if ( $dbw->affectedRows() ) {
393 # Successfully inserted, this is a new image
394 $id = $descTitle->getArticleID();
397 $seqVal = $dbw->nextSequenceValue( 'cur_cur_id_seq' );
398 $dbw->insertArray( 'cur',
401 'cur_namespace' => NS_IMAGE
,
402 'cur_title' => $name,
403 'cur_comment' => $desc,
404 'cur_user' => $wgUser->getID(),
405 'cur_user_text' => $wgUser->getName(),
406 'cur_timestamp' => $dbw->timestamp($now),
408 'cur_text' => $textdesc,
409 'inverse_timestamp' => $won,
410 'cur_touched' => $dbw->timestamp($now)
413 $id = $dbw->insertId() or 0; # We should throw an error instead
415 RecentChange
::notifyNew( $now, $descTitle, 0, $wgUser, $desc );
417 $u = new SearchUpdate( $id, $name, $desc );
421 # Collision, this is an update of an image
422 # Get current image row for update
423 $s = $dbw->getArray( 'image', array( 'img_name','img_size','img_timestamp','img_description',
424 'img_user','img_user_text' ), array( 'img_name' => $name ), $fname, 'FOR UPDATE' );
426 # Insert it into oldimage
427 $dbw->insertArray( 'oldimage',
429 'oi_name' => $s->img_name
,
430 'oi_archive_name' => $oldver,
431 'oi_size' => $s->img_size
,
432 'oi_timestamp' => $dbw->timestamp($s->img_timestamp
),
433 'oi_description' => $s->img_description
,
434 'oi_user' => $s->img_user
,
435 'oi_user_text' => $s->img_user_text
439 # Update the current image row
440 $dbw->updateArray( 'image',
443 'img_timestamp' => $dbw->timestamp(),
444 'img_user' => $wgUser->getID(),
445 'img_user_text' => $wgUser->getName(),
446 'img_description' => $desc,
447 ), array( /* WHERE */
452 # Invalidate the cache for the description page
453 $descTitle->invalidateCache();
456 $log = new LogPage( 'upload' );
457 $log->addEntry( 'upload', $descTitle, $desc );
460 function wfImageArchiveUrl( $name )
462 global $wgUploadPath;
464 $hash = md5( substr( $name, 15) );
465 $url = $wgUploadPath.'/archive/' . $hash{0} . '/' .
466 substr( $hash, 0, 2 ) . '/'.$name;
467 return wfUrlencode($url);