wfImageUrl moved from Globalfunctions to Image
authorJens Frank <jeluf@users.mediawiki.org>
Sat, 24 Apr 2004 23:45:11 +0000 (23:45 +0000)
committerJens Frank <jeluf@users.mediawiki.org>
Sat, 24 Apr 2004 23:45:11 +0000 (23:45 +0000)
Preferred access via $image->getUrl(), but for compatibility
Image::wfImageUrl() still works

includes/GlobalFunctions.php
includes/Image.php
includes/ImagePage.php
includes/Skin.php
includes/SpecialImagelist.php
includes/SpecialUnusedimages.php
includes/SpecialUpload.php

index 6942edf..03392a2 100644 (file)
@@ -89,21 +89,6 @@ function wfFullUrlE( $a, $q = "" ) {
 
 }
 
-function wfImageUrl( $img )
-{
-       global $wgUploadPath;
-
-       $nt = Title::newFromText( $img );
-       if( !$nt ) return "";
-
-       $name = $nt->getDBkey();
-       $hash = md5( $name );
-
-       $url = "{$wgUploadPath}/" . $hash{0} . "/" .
-         substr( $hash, 0, 2 ) . "/{$name}";
-       return wfUrlencode( $url );
-}
-
 function wfImagePath( $img )
 {
        global $wgUploadDirectory;
index 024ae05..27ad21e 100644 (file)
@@ -9,15 +9,25 @@ class Image
        var     $name,          # name of the image
                $imagePath,     # Path of the image
                $url,           # Image URL
-               $title;         # Title object for this image. Initialized when needed.
+               $title,         # Title object for this image. Initialized when needed.
+               $fileExists,    # does the image file exist on disk?
+               $width,         # \
+               $height,        #  --- returned by getimagesize, see http://de3.php.net/manual/en/function.getimagesize.php
+               $type,          #  |
+               $attr;          # /
 
 
        function Image( $name )
        {
                $this->name      = $name;
+               $this->title     = Title::makeTitle( Namespace::getImage(), $this->name );
                $this->imagePath = wfImagePath( $name );
-               $this->url       = wfImageUrl( $name );
-               $this->title     = NULL;
+               $this->url       = $this->wfImageUrl( $name );
+               
+               if ( $this->fileExists = file_exists( $this->imagePath ) ) // Sic!, "=" is intended
+               {
+                       list($this->width, $this->height, $this->type, $this->attr) = getimagesize( $this->imagePath );
+               }
        }
 
        function newFromTitle( $nt )
@@ -42,15 +52,43 @@ class Image
                return $this->imagePath;
        }
 
+       function getWidth()
+       {
+               return $this->width;
+       }
+
+       function getHeight()
+       {
+               return $this->height;
+       }
+
+       function getType()
+       {
+               return $this->type;
+       }
+
        function getEscapeLocalURL()
        {
-               if ( $this->title == NULL )
-               {
-                       $this->title = Title::makeTitle( Namespace::getImage(), $this->name );
-               }
                return $this->title->escapeLocalURL();
        }
 
+       function wfImageUrl( $name )
+       {
+               global $wgUploadPath;
+               $hash = md5( $name );
+
+               $url = "{$wgUploadPath}/" . $hash{0} . "/" .
+               substr( $hash, 0, 2 ) . "/{$name}";
+               return wfUrlencode( $url );
+       }
+
+
+       function exists()
+       {
+               return $this->fileExists;
+       }
+
+
        function createThumb( $width ) {
                global $wgUploadDirectory;
                global $wgImageMagickConvertCommand;
@@ -60,14 +98,14 @@ class Image
                $thumbPath = wfImageThumbDir( $thumbName )."/".$thumbName;
                $thumbUrl  = wfImageThumbUrl( $thumbName );
 
-               if ( ! file_exists( $this->imagePath ) )
+               if ( ! $this->exists() )
                {
                        # If there is no image, there will be no thumbnail
                        return "";
                }
 
-               if (     (! file_exists( $thumbPath ) )
-               ||  ( filemtime($thumbPath) < filemtime($this->imagePath) ) ) {
+               if (    (! file_exists( $thumbPath ) )
+                    || ( filemtime($thumbPath) < filemtime($this->imagePath) ) ) {
                        # Squid purging
                        if ( $wgUseSquid ) {
                                $urlArr = Array(
@@ -88,8 +126,8 @@ class Image
                                #
                                # First find out what kind of file this is, and select the correct
                                # input routine for this.
-                               list($src_width, $src_height, $src_type, $src_attr) = getimagesize( $this->imagePath );
-                               switch( $src_type ) {
+                               
+                               switch( $this->type ) {
                                        case 1: # GIF
                                                $src_image = imagecreatefromgif( $this->imagePath );
                                                break;
@@ -109,12 +147,12 @@ class Image
                                                return "Image type not supported";
                                                break;
                                }
-                               $height = floor( $src_height * ( $width/$src_width ) );
+                               $height = floor( $this->height * ( $width/$this->width ) );
                                $dst_image = imagecreatetruecolor( $width, $height );
                                imagecopyresampled( $dst_image, $src_image, 
                                                        0,0,0,0,
-                                                       $width, $height, $src_width, $src_height );
-                               switch( $src_type ) {
+                                                       $width, $height, $this->width, $this->height );
+                               switch( $this->type ) {
                                        case 1:  # GIF
                                        case 3:  # PNG
                                        case 15: # WBMP
@@ -142,7 +180,6 @@ class Image
                        # no disk space is available or some other error occurs
                        #
                        $thumbstat = stat( $thumbPath );
-                       $imgstat   = stat( $this->imagePath );
                        if( $thumbstat["size"] == 0 )
                        {
                                unlink( $thumbPath );
index 75e45f6..9dcad80 100644 (file)
@@ -27,21 +27,20 @@ class ImagePage extends Article {
        function openShowImage()
        {
                global $wgOut, $wgUser,$wgRequest;
-               $name = $this->mTitle->getText();
-               $path = wfImagePath( $name );
-               $url   = wfImageUrl( $name );
+               $img  = Image::newFromTitle( $this->mTitle );
+               $url  = $img->getUrl();
 
-               if ( file_exists( $path ) ) {
-                       list($width, $height, $type, $attr) = getimagesize( $path );
+               if ( $img->exists() ) {
 
                        $sk = $wgUser->getSkin();
                        
-                       if ( $type != "" ) {
+                       if ( $img->getType() != "" ) {
                                # image
-                               $s = "<div class=\"fullImage\"><img src=\"{$url}\" width=\"{$width}\" height=\"{$height}\"".
-                               "alt=\"".$wgRequest->getVal( 'image' )."\" /></div>";
+                               $s = "<div class=\"fullImage\">" .
+                                    "<img src=\"{$url}\" width=\"" . $img->getWidth() . "\" height=\"" . $img->getHeight() .
+                                    "\" alt=\"".$wgRequest->getVal( 'image' )."\" /></div>";
                        } else {
-                               $s = "<div class=\"fullMedia\">".$sk->makeMediaLink($name,"")."</div>";
+                               $s = "<div class=\"fullMedia\">".$sk->makeMediaLink($img->getName(),"")."</div>";
                        }
                        $wgOut->addHTML( $s );
                }
index 8031a65..1bb040c 100644 (file)
@@ -457,7 +457,7 @@ class Skin {
                if ( $wgOut->isArticleRelated() ) {
                        if ( $wgTitle->getNamespace() == Namespace::getImage() ) {
                                $name = $wgTitle->getDBkey();
-                               $link = wfEscapeHTML( wfImageUrl( $name ) );
+                               $link = wfEscapeHTML( Image::wfImageUrl( $name ) );
                                $style = $this->getInternalLinkAttributes( $link, $name );
                                $s .= " | <a href=\"{$link}\"{$style}>{$name}</a>";
                        }
@@ -1673,15 +1673,15 @@ class Skin {
                global $wgUploadPath, $wgLang;
                # $image = Title::makeTitle( Namespace::getImage(), $name );
                $url  = $img->getURL();
-               $path = $img->getImagePath();
                
                #$label = htmlspecialchars( $label );
                $alt = preg_replace( "/<[^>]*>/", "", $label);
                $alt = htmlspecialchars( $alt );
                
-               if ( file_exists( $path ) )
+               if ( $img->exists() )
                {
-                       list($width, $height, $type, $attr) = getimagesize( $path );
+                       $width  = $img->getWidth();
+                       $height = $img->getHeight();
                } else {
                        $width = $height = 200;
                }
@@ -1736,7 +1736,7 @@ class Skin {
        function makeMediaLinkObj( $nt, $alt = "" )
        {
                $name = $nt->getDBKey();
-               $url = wfImageUrl( $name );
+               $url = Image::wfImageUrl( $name );
                if ( empty( $alt ) ) {
                        $alt = preg_replace( '/\.(.+?)^/', '', $name );
                }
@@ -2281,7 +2281,7 @@ class Skin {
                $cur = wfMsg( "cur" );
 
                if ( $iscur ) {
-                       $url = wfImageUrl( $img );
+                       $url = Image::wfImageUrl( $img );
                        $rlink = $cur;
                        if ( $wgUser->isSysop() ) {
                                $link = $wgTitle->escapeLocalURL( "image=" . $wgTitle->getPartialURL() .
index 39737fe..3702511 100644 (file)
@@ -89,7 +89,7 @@ function wfSpecialImagelist()
                else { $ul = $sk->makeLink( $wgLang->getNsText(
                  Namespace::getUser() ) . ":{$ut}", $ut ); }
 
-               $ilink = "<a href=\"" . wfImageUrl( $name ) .
+               $ilink = "<a href=\"" . Image::wfImageUrl( $name ) .
                  "\">{$name}</a>";
 
                $nb = wfMsg( "nbytes", $wgLang->formatNum( $s->img_size ) );
index 09ac253..09075f5 100644 (file)
@@ -26,7 +26,7 @@ function wfSpecialUnusedimages() {
        while ( $obj = wfFetchObject( $res ) ) {
                $name = $obj->img_name;
                $dlink = $sk->makeKnownLink( "{$ins}:{$name}", wfMsg( "imgdesc" ) );
-               $ilink = "<a href=\"" . wfImageUrl( $name ) . "\">{$name}</a>";
+               $ilink = "<a href=\"" . Image::wfImageUrl( $name ) . "\">{$name}</a>";
 
                $d = $wgLang->timeanddate( $obj->img_timestamp, true );
                $u = $obj->img_user;
index 219b70d..eeff398 100644 (file)
@@ -137,8 +137,7 @@ class UploadForm {
                  $this->mUploadDescription, $this->mUploadCopyStatus, $this->mUploadSource );
 
                $sk = $wgUser->getSkin();
-               $ilink = $sk->makeMediaLink( $this->mUploadSaveName, wfImageUrl(
-                 $this->mUploadSaveName ) );
+               $ilink = $sk->makeMediaLink( $this->mUploadSaveName, Image::wfImageUrl( $this->mUploadSaveName ) );
                $dname = $wgLang->getNsText( Namespace::getImage() ) . ":{$this->mUploadSaveName}";
                $dlink = $sk->makeKnownLink( $dname, $dname );