From 42f39da880674ff4fb5e43a37b3b5ba6fde8a797 Mon Sep 17 00:00:00 2001 From: Jens Frank Date: Sat, 24 Apr 2004 23:45:11 +0000 Subject: [PATCH] wfImageUrl moved from Globalfunctions to Image Preferred access via $image->getUrl(), but for compatibility Image::wfImageUrl() still works --- includes/GlobalFunctions.php | 15 ------- includes/Image.php | 69 ++++++++++++++++++++++++-------- includes/ImagePage.php | 17 ++++---- includes/Skin.php | 12 +++--- includes/SpecialImagelist.php | 2 +- includes/SpecialUnusedimages.php | 2 +- includes/SpecialUpload.php | 3 +- 7 files changed, 70 insertions(+), 50 deletions(-) diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 6942edf8aa..03392a22aa 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -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; diff --git a/includes/Image.php b/includes/Image.php index 024ae05d42..27ad21e326 100644 --- a/includes/Image.php +++ b/includes/Image.php @@ -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 ); diff --git a/includes/ImagePage.php b/includes/ImagePage.php index 75e45f6b59..9dcad80ebe 100644 --- a/includes/ImagePage.php +++ b/includes/ImagePage.php @@ -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 = "
getVal( 'image' )."\" />
"; + $s = "
" . + "getWidth() . "\" height=\"" . $img->getHeight() . + "\" alt=\"".$wgRequest->getVal( 'image' )."\" />
"; } else { - $s = "
".$sk->makeMediaLink($name,"")."
"; + $s = "
".$sk->makeMediaLink($img->getName(),"")."
"; } $wgOut->addHTML( $s ); } diff --git a/includes/Skin.php b/includes/Skin.php index 8031a6571b..1bb040cf3b 100644 --- a/includes/Skin.php +++ b/includes/Skin.php @@ -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 .= " | {$name}"; } @@ -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() . diff --git a/includes/SpecialImagelist.php b/includes/SpecialImagelist.php index 39737fef29..370251153b 100644 --- a/includes/SpecialImagelist.php +++ b/includes/SpecialImagelist.php @@ -89,7 +89,7 @@ function wfSpecialImagelist() else { $ul = $sk->makeLink( $wgLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut ); } - $ilink = "{$name}"; $nb = wfMsg( "nbytes", $wgLang->formatNum( $s->img_size ) ); diff --git a/includes/SpecialUnusedimages.php b/includes/SpecialUnusedimages.php index 09ac253157..09075f5be1 100644 --- a/includes/SpecialUnusedimages.php +++ b/includes/SpecialUnusedimages.php @@ -26,7 +26,7 @@ function wfSpecialUnusedimages() { while ( $obj = wfFetchObject( $res ) ) { $name = $obj->img_name; $dlink = $sk->makeKnownLink( "{$ins}:{$name}", wfMsg( "imgdesc" ) ); - $ilink = "{$name}"; + $ilink = "{$name}"; $d = $wgLang->timeanddate( $obj->img_timestamp, true ); $u = $obj->img_user; diff --git a/includes/SpecialUpload.php b/includes/SpecialUpload.php index 219b70d57f..eeff398988 100644 --- a/includes/SpecialUpload.php +++ b/includes/SpecialUpload.php @@ -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 ); -- 2.20.1