r6657 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r6656‎ | r6657 | r6658 >
Date:09:56, 12 December 2004
Author:vibber
Status:old
Tags:
Comment:
* (bug 1074) Add stock icons for non-image files in gallery/Newimages
* Add width and height attributes on thumbs in gallery/Newimages
Modified paths:
  • /trunk/phase3/includes/Image.php (modified) (history)
  • /trunk/phase3/includes/ImageGallery.php (modified) (history)
  • /trunk/phase3/skins/common/images/fileicon-ogg.png (added) (history)
  • /trunk/phase3/skins/common/images/fileicon-ogg.xcf (added) (history)
  • /trunk/phase3/skins/common/images/fileicon.png (added) (history)
  • /trunk/phase3/skins/common/images/fileicon.xcf (added) (history)

Diff [purge]

Index: trunk/phase3/skins/common/images/fileicon-ogg.xcf
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Property changes on: trunk/phase3/skins/common/images/fileicon-ogg.xcf
___________________________________________________________________
Added: svn:mime-type
11 + application/octet-stream
Index: trunk/phase3/skins/common/images/fileicon.xcf
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Property changes on: trunk/phase3/skins/common/images/fileicon.xcf
___________________________________________________________________
Added: svn:mime-type
22 + application/octet-stream
Index: trunk/phase3/skins/common/images/fileicon-ogg.png
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Property changes on: trunk/phase3/skins/common/images/fileicon-ogg.png
___________________________________________________________________
Added: svn:mime-type
33 + application/octet-stream
Index: trunk/phase3/skins/common/images/fileicon.png
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Property changes on: trunk/phase3/skins/common/images/fileicon.png
___________________________________________________________________
Added: svn:mime-type
44 + application/octet-stream
Index: trunk/phase3/includes/Image.php
@@ -323,6 +323,22 @@
324324 * @access public
325325 */
326326 function createThumb( $width, $height=-1 ) {
 327+ $thumb = $this->getThumbnail( $width, $height );
 328+ if( is_null( $thumb ) ) return '';
 329+ return $thumb->getUrl();
 330+ }
 331+
 332+ /**
 333+ * As createThumb, but returns a ThumbnailImage object. This can
 334+ * provide access to the actual file, the real size of the thumb,
 335+ * and can produce a convenient <img> tag for you.
 336+ *
 337+ * @param integer $width maximum width of the generated thumbnail
 338+ * @param integer $height maximum height of the image (optional)
 339+ * @return ThumbnailImage
 340+ * @access public
 341+ */
 342+ function &getThumbnail( $width, $height=-1 ) {
327343 if ( $height == -1 ) {
328344 return $this->renderThumb( $width );
329345 }
@@ -337,17 +353,40 @@
338354 $thumbwidth = $thumbwidth * $height / $thumbheight;
339355 $thumbheight = $height;
340356 }
341 - return $this->renderThumb( $thumbwidth );
 357+ $thumb = $this->renderThumb( $thumbwidth );
 358+ if( is_null( $thumb ) ) {
 359+ $thumb = $this->iconThumb();
 360+ }
 361+ return $thumb;
342362 }
 363+
 364+ /**
 365+ * @return ThumbnailImage
 366+ */
 367+ function iconThumb() {
 368+ global $wgStylePath, $wgStyleDirectory;
343369
 370+ $try = array( 'fileicon-' . $this->extension . '.png', 'fileicon.png' );
 371+ foreach( $try as $icon ) {
 372+ $path = '/common/images/' . $icon;
 373+ $filepath = $wgStyleDirectory . $path;
 374+ if( file_exists( $filepath ) ) {
 375+ return new ThumbnailImage( $filepath, $wgStylePath . $path );
 376+ }
 377+ }
 378+ return null;
 379+ }
 380+
344381 /**
345382 * Create a thumbnail of the image having the specified width.
346383 * The thumbnail will not be created if the width is larger than the
347384 * image's width. Let the browser do the scaling in this case.
348385 * The thumbnail is stored on disk and is only computed if the thumbnail
349386 * file does not exist OR if it is older than the image.
350 - * Returns the URL.
 387+ * Returns an object which can return the pathname, URL, and physical
 388+ * pixel size of the thumbnail -- or null on failure.
351389 *
 390+ * @return ThumbnailImage
352391 * @access private
353392 */
354393 function /* private */ renderThumb( $width ) {
@@ -364,18 +403,18 @@
365404 if ( ! $this->exists() )
366405 {
367406 # If there is no image, there will be no thumbnail
368 - return '';
 407+ return null;
369408 }
370409
371410 # Sanity check $width
372411 if( $width <= 0 ) {
373412 # BZZZT
374 - return '';
 413+ return null;
375414 }
376415
377416 if( $width > $this->width && !$this->mustRender() ) {
378417 # Don't make an image bigger than the source
379 - return $this->getViewURL();
 418+ return new ThumbnailImage( $this->getImagePath(), $this->getViewURL() );
380419 }
381420
382421 if ( (! file_exists( $thumbPath ) ) || ( filemtime($thumbPath) < filemtime($this->imagePath) ) ) {
@@ -462,8 +501,6 @@
463502 }
464503 imagedestroy( $dst_image );
465504 imagedestroy( $src_image );
466 -
467 -
468505 }
469506 #
470507 # Check for zero-sized thumbnails. Those can be generated when
@@ -485,7 +522,7 @@
486523 wfPurgeSquidServers($urlArr);
487524 }
488525 }
489 - return $thumbUrl;
 526+ return new ThumbnailImage( $thumbPath, $thumbUrl );
490527 } // END OF function createThumb
491528
492529 /**
@@ -867,4 +904,58 @@
868905 "width=\"$width\" height=\"$height\"" );
869906 }
870907
 908+
 909+/**
 910+ * Wrapper class for thumbnail images
 911+ */
 912+class ThumbnailImage {
 913+ /**
 914+ * @param string $path Filesystem path to the thumb
 915+ * @param string $url URL path to the thumb
 916+ * @access private
 917+ */
 918+ function ThumbnailImage( $path, $url ) {
 919+ $this->url = $url;
 920+ $this->path = $path;
 921+ $size = @getimagesize( $this->path );
 922+ if( $size ) {
 923+ $this->width = $size[0];
 924+ $this->height = $size[1];
 925+ } else {
 926+ $this->width = 0;
 927+ $this->height = 0;
 928+ }
 929+ }
 930+
 931+ function getUrl() {
 932+ return $this->url;
 933+ }
 934+
 935+ /**
 936+ * Return HTML <img ... /> tag for the thumbnail, will include
 937+ * width and height attributes and a blank alt text (as required).
 938+ *
 939+ * You can set or override additional attributes by passing an
 940+ * associative array of name => data pairs. The data will be escaped
 941+ * for HTML output, so should be in plaintext.
 942+ *
 943+ * @param array $attribs
 944+ * @return string
 945+ * @access public
 946+ */
 947+ function toHtml( $attribs = array() ) {
 948+ $attribs['src'] = $this->url;
 949+ $attribs['width'] = $this->width;
 950+ $attribs['height'] = $this->height;
 951+ if( !isset( $attribs['alt'] ) ) $attribs['alt'] = '';
 952+
 953+ $html = '<img ';
 954+ foreach( $attribs as $name => $data ) {
 955+ $html .= $name . '="' . htmlspecialchars( $data ) . '" ';
 956+ }
 957+ $html .= '/>';
 958+ return $html;
 959+ }
 960+}
 961+
871962 ?>
Index: trunk/phase3/includes/ImageGallery.php
@@ -119,10 +119,11 @@
120120 '' ;
121121
122122 $s .= ($i%4==0) ? '<tr>' : '';
 123+ $thumb = $img->getThumbnail(120,120);
123124 $s .= '<td valign="top" width="150px" style="background-color:#F0F0F0;">' .
124125 '<table width="100%" height="150px">'.
125126 '<tr><td align="center" valign="center" style="background-color:#F8F8F8;border:solid 1px #888888;">' .
126 - $sk->makeKnownLinkObj( $nt, '<img src="'.$img->createThumb(120,120).'" alt="" />' ) . '</td></tr></table> ' .
 127+ $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</td></tr></table> ' .
127128 $textlink . $text . $nb;
128129
129130 $s .= "</td>\n" . (($i%4==3) ? "</tr>\n" : '');

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r6656* (bug 1074) Add stock icons for non-image files in gallery/Newimages...vibber09:53, 12 December 2004

Status & tagging log