* (bug 1074) Add stock icons for non-image files in gallery/Newimages

* Add width and height attributes on thumbs in gallery/Newimages
This commit is contained in:
Brion Vibber 2004-12-12 09:56:02 +00:00
parent 2264b8a92e
commit fada5faa4b
6 changed files with 101 additions and 9 deletions

View file

@ -323,6 +323,22 @@ class Image
* @access public
*/
function createThumb( $width, $height=-1 ) {
$thumb = $this->getThumbnail( $width, $height );
if( is_null( $thumb ) ) return '';
return $thumb->getUrl();
}
/**
* As createThumb, but returns a ThumbnailImage object. This can
* provide access to the actual file, the real size of the thumb,
* and can produce a convenient <img> tag for you.
*
* @param integer $width maximum width of the generated thumbnail
* @param integer $height maximum height of the image (optional)
* @return ThumbnailImage
* @access public
*/
function &getThumbnail( $width, $height=-1 ) {
if ( $height == -1 ) {
return $this->renderThumb( $width );
}
@ -337,7 +353,28 @@ class Image
$thumbwidth = $thumbwidth * $height / $thumbheight;
$thumbheight = $height;
}
return $this->renderThumb( $thumbwidth );
$thumb = $this->renderThumb( $thumbwidth );
if( is_null( $thumb ) ) {
$thumb = $this->iconThumb();
}
return $thumb;
}
/**
* @return ThumbnailImage
*/
function iconThumb() {
global $wgStylePath, $wgStyleDirectory;
$try = array( 'fileicon-' . $this->extension . '.png', 'fileicon.png' );
foreach( $try as $icon ) {
$path = '/common/images/' . $icon;
$filepath = $wgStyleDirectory . $path;
if( file_exists( $filepath ) ) {
return new ThumbnailImage( $filepath, $wgStylePath . $path );
}
}
return null;
}
/**
@ -346,8 +383,10 @@ class Image
* image's width. Let the browser do the scaling in this case.
* The thumbnail is stored on disk and is only computed if the thumbnail
* file does not exist OR if it is older than the image.
* Returns the URL.
* Returns an object which can return the pathname, URL, and physical
* pixel size of the thumbnail -- or null on failure.
*
* @return ThumbnailImage
* @access private
*/
function /* private */ renderThumb( $width ) {
@ -364,18 +403,18 @@ class Image
if ( ! $this->exists() )
{
# If there is no image, there will be no thumbnail
return '';
return null;
}
# Sanity check $width
if( $width <= 0 ) {
# BZZZT
return '';
return null;
}
if( $width > $this->width && !$this->mustRender() ) {
# Don't make an image bigger than the source
return $this->getViewURL();
return new ThumbnailImage( $this->getImagePath(), $this->getViewURL() );
}
if ( (! file_exists( $thumbPath ) ) || ( filemtime($thumbPath) < filemtime($this->imagePath) ) ) {
@ -462,8 +501,6 @@ class Image
}
imagedestroy( $dst_image );
imagedestroy( $src_image );
}
#
# Check for zero-sized thumbnails. Those can be generated when
@ -485,7 +522,7 @@ class Image
wfPurgeSquidServers($urlArr);
}
}
return $thumbUrl;
return new ThumbnailImage( $thumbPath, $thumbUrl );
} // END OF function createThumb
/**
@ -867,4 +904,58 @@ function getSVGsize( $filename ) {
"width=\"$width\" height=\"$height\"" );
}
/**
* Wrapper class for thumbnail images
*/
class ThumbnailImage {
/**
* @param string $path Filesystem path to the thumb
* @param string $url URL path to the thumb
* @access private
*/
function ThumbnailImage( $path, $url ) {
$this->url = $url;
$this->path = $path;
$size = @getimagesize( $this->path );
if( $size ) {
$this->width = $size[0];
$this->height = $size[1];
} else {
$this->width = 0;
$this->height = 0;
}
}
function getUrl() {
return $this->url;
}
/**
* Return HTML <img ... /> tag for the thumbnail, will include
* width and height attributes and a blank alt text (as required).
*
* You can set or override additional attributes by passing an
* associative array of name => data pairs. The data will be escaped
* for HTML output, so should be in plaintext.
*
* @param array $attribs
* @return string
* @access public
*/
function toHtml( $attribs = array() ) {
$attribs['src'] = $this->url;
$attribs['width'] = $this->width;
$attribs['height'] = $this->height;
if( !isset( $attribs['alt'] ) ) $attribs['alt'] = '';
$html = '<img ';
foreach( $attribs as $name => $data ) {
$html .= $name . '="' . htmlspecialchars( $data ) . '" ';
}
$html .= '/>';
return $html;
}
}
?>

View file

@ -119,10 +119,11 @@ class ImageGallery
'' ;
$s .= ($i%4==0) ? '<tr>' : '';
$thumb = $img->getThumbnail(120,120);
$s .= '<td valign="top" width="150px" style="background-color:#F0F0F0;">' .
'<table width="100%" height="150px">'.
'<tr><td align="center" valign="center" style="background-color:#F8F8F8;border:solid 1px #888888;">' .
$sk->makeKnownLinkObj( $nt, '<img src="'.$img->createThumb(120,120).'" alt="" />' ) . '</td></tr></table> ' .
$sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</td></tr></table> ' .
$textlink . $text . $nb;
$s .= "</td>\n" . (($i%4==3) ? "</tr>\n" : '');

Binary file not shown.

After

Width:  |  Height:  |  Size: 7 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.