* (bug 1074) Add stock icons for non-image files in gallery/Newimages
authorBrion Vibber <brion@users.mediawiki.org>
Sun, 12 Dec 2004 09:56:02 +0000 (09:56 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Sun, 12 Dec 2004 09:56:02 +0000 (09:56 +0000)
* Add width and height attributes on thumbs in gallery/Newimages

includes/Image.php
includes/ImageGallery.php
skins/common/images/fileicon-ogg.png [new file with mode: 0644]
skins/common/images/fileicon-ogg.xcf [new file with mode: 0644]
skins/common/images/fileicon.png [new file with mode: 0644]
skins/common/images/fileicon.xcf [new file with mode: 0644]

index 654048e..90c4a9f 100644 (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;
+       }
+}
+
 ?>
index 44177d6..81af0cb 100644 (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" : '');
diff --git a/skins/common/images/fileicon-ogg.png b/skins/common/images/fileicon-ogg.png
new file mode 100644 (file)
index 0000000..aa82607
Binary files /dev/null and b/skins/common/images/fileicon-ogg.png differ
diff --git a/skins/common/images/fileicon-ogg.xcf b/skins/common/images/fileicon-ogg.xcf
new file mode 100644 (file)
index 0000000..a91024b
Binary files /dev/null and b/skins/common/images/fileicon-ogg.xcf differ
diff --git a/skins/common/images/fileicon.png b/skins/common/images/fileicon.png
new file mode 100644 (file)
index 0000000..2271fae
Binary files /dev/null and b/skins/common/images/fileicon.png differ
diff --git a/skins/common/images/fileicon.xcf b/skins/common/images/fileicon.xcf
new file mode 100644 (file)
index 0000000..45135de
Binary files /dev/null and b/skins/common/images/fileicon.xcf differ