From bbe9f6a37b40adb6b0bf58ee0798241cbc4029e8 Mon Sep 17 00:00:00 2001 From: Jens Frank Date: Sat, 24 Apr 2004 22:33:06 +0000 Subject: [PATCH] Move image-specific methods to Image-class Step 1: createThumb and related functions --- includes/Image.php | 155 +++++++++++++++++++++++++++++++++++++++++++++ includes/Skin.php | 131 +++++--------------------------------- 2 files changed, 171 insertions(+), 115 deletions(-) create mode 100644 includes/Image.php diff --git a/includes/Image.php b/includes/Image.php new file mode 100644 index 0000000000..024ae05d42 --- /dev/null +++ b/includes/Image.php @@ -0,0 +1,155 @@ +name = $name; + $this->imagePath = wfImagePath( $name ); + $this->url = wfImageUrl( $name ); + $this->title = NULL; + } + + function newFromTitle( $nt ) + { + $img = new Image( $nt->getDBKey() ); + $img->title = $nt; + return $img; + } + + function getName() + { + return $this->name; + } + + function getURL() + { + return $this->url; + } + + function getImagePath() + { + return $this->imagePath; + } + + function getEscapeLocalURL() + { + if ( $this->title == NULL ) + { + $this->title = Title::makeTitle( Namespace::getImage(), $this->name ); + } + return $this->title->escapeLocalURL(); + } + + function createThumb( $width ) { + global $wgUploadDirectory; + global $wgImageMagickConvertCommand; + global $wgUseImageMagick; + global $wgUseSquid, $wgInternalServer; + $thumbName = $width."px-".$this->name; + $thumbPath = wfImageThumbDir( $thumbName )."/".$thumbName; + $thumbUrl = wfImageThumbUrl( $thumbName ); + + if ( ! file_exists( $this->imagePath ) ) + { + # If there is no image, there will be no thumbnail + return ""; + } + + if ( (! file_exists( $thumbPath ) ) + || ( filemtime($thumbPath) < filemtime($this->imagePath) ) ) { + # Squid purging + if ( $wgUseSquid ) { + $urlArr = Array( + $wgInternalServer.$thumbUrl + ); + wfPurgeSquidServers($urlArr); + } + + if ( $wgUseImageMagick ) { + # use ImageMagick + $cmd = $wgImageMagickConvertCommand . + " -quality 85 -geometry {$width} ". + escapeshellarg($this->imagePath) . " " . + escapeshellarg($thumbPath); + $conv = shell_exec( $cmd ); + } else { + # Use PHP's builtin GD library functions. + # + # 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 ) { + case 1: # GIF + $src_image = imagecreatefromgif( $this->imagePath ); + break; + case 2: # JPG + $src_image = imagecreatefromjpeg( $this->imagePath ); + break; + case 3: # PNG + $src_image = imagecreatefrompng( $this->imagePath ); + break; + case 15: # WBMP for WML + $src_image = imagecreatefromwbmp( $this->imagePath ); + break; + case 16: # XBM + $src_image = imagecreatefromxbm( $this->imagePath ); + break; + default: + return "Image type not supported"; + break; + } + $height = floor( $src_height * ( $width/$src_width ) ); + $dst_image = imagecreatetruecolor( $width, $height ); + imagecopyresampled( $dst_image, $src_image, + 0,0,0,0, + $width, $height, $src_width, $src_height ); + switch( $src_type ) { + case 1: # GIF + case 3: # PNG + case 15: # WBMP + case 16: # XBM + #$thumbUrl .= ".png"; + #$thumbPath .= ".png"; + imagepng( $dst_image, $thumbPath ); + break; + case 2: # JPEG + #$thumbUrl .= ".jpg"; + #$thumbPath .= ".jpg"; + imageinterlace( $dst_image ); + imagejpeg( $dst_image, $thumbPath, 95 ); + break; + default: + break; + } + imagedestroy( $dst_image ); + imagedestroy( $src_image ); + + + } + # + # Check for zero-sized thumbnails. Those can be generated when + # no disk space is available or some other error occurs + # + $thumbstat = stat( $thumbPath ); + $imgstat = stat( $this->imagePath ); + if( $thumbstat["size"] == 0 ) + { + unlink( $thumbPath ); + } + + } + return $thumbUrl; + } //function createThumb + +} //class diff --git a/includes/Skin.php b/includes/Skin.php index 3faa1a2dde..8031a6571b 100644 --- a/includes/Skin.php +++ b/includes/Skin.php @@ -1,6 +1,7 @@ getDBKey(); - $url = wfImageUrl( $name ); + $img = Image::newFromTitle( $nt ); + $url = $img->getURL(); + $align = ""; $prefix = $postfix = ""; @@ -1636,25 +1638,26 @@ class Skin { if ( ! isset($width) ) { $width = 180; } - return $prefix.$this->makeThumbLinkObj( $nt, $alt, $align, $width, $framed ).$postfix; + return $prefix.$this->makeThumbLinkObj( $img, $alt, $align, $width, $framed ).$postfix; } elseif ( isset($width) ) { # Create a resized image, without the additional thumbnail # features - $url = $this->createThumb( $name, $width ); + $url = $img->createThumb( $width ); } } # endif $wgUseImageResize if ( empty( $alt ) ) { - $alt = preg_replace( '/\.(.+?)^/', '', $name ); + $alt = preg_replace( '/\.(.+?)^/', '', $img->getName() ); } $alt = htmlspecialchars( $alt ); $u = $nt->escapeLocalURL(); if ( $url == "" ) { - $s = str_replace( "$1", $name, wfMsg("missingimage") ); + $s = str_replace( "$1", $img->getName(), wfMsg("missingimage") ); + $s .= "
{$alt}
{$url}
\n"; } else { $s = "" . "\"{$alt}\""; @@ -1665,114 +1668,12 @@ class Skin { return $prefix.$s.$postfix; } - function createThumb( $name, $width ) { - global $wgUploadDirectory; - global $wgImageMagickConvertCommand; - global $wgUseImageMagick; - global $wgUseSquid, $wgInternalServer; - $imgPath = wfImagePath( $name ); - $thumbName = $width."px-".$name; - $thumbPath = wfImageThumbDir( $thumbName )."/".$thumbName; - $thumbUrl = wfImageThumbUrl( $thumbName ); - - if ( ! file_exists( $imgPath ) ) - { - # If there is no image, there will be no thumbnail - return ""; - } - - if ( (! file_exists( $thumbPath ) ) - || ( filemtime($thumbPath) < filemtime($imgPath) ) ) { - # Squid purging - if ( $wgUseSquid ) { - $urlArr = Array( - $wgInternalServer.$thumbUrl - ); - wfPurgeSquidServers($urlArr); - } - - if ( $wgUseImageMagick ) { - # use ImageMagick - $cmd = $wgImageMagickConvertCommand . - " -quality 85 -geometry {$width} ". - escapeshellarg($imgPath) . " " . - escapeshellarg($thumbPath); - $conv = shell_exec( $cmd ); - } else { - # Use PHP's builtin GD library functions. - # - # 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( $imgPath ); - switch( $src_type ) { - case 1: # GIF - $src_image = imagecreatefromgif( $imgPath ); - break; - case 2: # JPG - $src_image = imagecreatefromjpeg( $imgPath ); - break; - case 3: # PNG - $src_image = imagecreatefrompng( $imgPath ); - break; - case 15: # WBMP for WML - $src_image = imagecreatefromwbmp( $imgPath ); - break; - case 16: # XBM - $src_image = imagecreatefromxbm( $imgPath ); - break; - default: - return "Image type not supported"; - break; - } - $height = floor( $src_height * ( $width/$src_width ) ); - $dst_image = imagecreatetruecolor( $width, $height ); - imagecopyresampled( $dst_image, $src_image, - 0,0,0,0, - $width, $height, $src_width, $src_height ); - switch( $src_type ) { - case 1: # GIF - case 3: # PNG - case 15: # WBMP - case 16: # XBM - #$thumbUrl .= ".png"; - #$thumbPath .= ".png"; - imagepng( $dst_image, $thumbPath ); - break; - case 2: # JPEG - #$thumbUrl .= ".jpg"; - #$thumbPath .= ".jpg"; - imageinterlace( $dst_image ); - imagejpeg( $dst_image, $thumbPath, 95 ); - break; - default: - break; - } - imagedestroy( $dst_image ); - imagedestroy( $src_image ); - - - } - # - # Check for zero-sized thumbnails. Those can be generated when - # no disk space is available or some other error occurs - # - $thumbstat = stat( $thumbPath ); - $imgstat = stat( $imgPath ); - if( $thumbstat["size"] == 0 ) - { - unlink( $thumbPath ); - } - - } - return $thumbUrl; - } - function makeThumbLinkObj( $nt, $label = "", $align = "right", $boxwidth = 180, $framed=false ) { + function makeThumbLinkObj( $img, $label = "", $align = "right", $boxwidth = 180, $framed=false ) { global $wgUploadPath, $wgLang; - $name = $nt->getDBKey(); - $image = Title::makeTitle( Namespace::getImage(), $name ); - $url = wfImageUrl( $name ); - $path = wfImagePath( $name ); + # $image = Title::makeTitle( Namespace::getImage(), $name ); + $url = $img->getURL(); + $path = $img->getImagePath(); #$label = htmlspecialchars( $label ); $alt = preg_replace( "/<[^>]*>/", "", $label); @@ -1796,11 +1697,11 @@ class Skin { $boxwidth = $width; $boxheight = $height; } - $thumbUrl = $this->createThumb( $name, $boxwidth ); + $thumbUrl = $img->createThumb( $boxwidth ); } $oboxwidth = $boxwidth + 2; - $u = $nt->escapeLocalURL(); + $u = $img->getEscapeLocalURL(); $more = htmlspecialchars( wfMsg( "thumbnail-more" ) ); $magnifyalign = $wgLang->isRTL() ? "left" : "right"; @@ -1808,7 +1709,7 @@ class Skin { $s = "
"; if ( $thumbUrl == "" ) { - $s .= str_replace( "$1", $name, wfMsg("missingimage") ); + $s .= str_replace( "$1", $img->getName(), wfMsg("missingimage") ); $zoomicon = ''; } else { $s .= ''. -- 2.20.1