Offer GD-based image resizing as well as ImageMagick based one
authorJens Frank <jeluf@users.mediawiki.org>
Tue, 13 Jan 2004 21:03:47 +0000 (21:03 +0000)
committerJens Frank <jeluf@users.mediawiki.org>
Tue, 13 Jan 2004 21:03:47 +0000 (21:03 +0000)
LocalSettings.sample
includes/Skin.php

index 758631a..01a8657 100644 (file)
@@ -38,13 +38,18 @@ $wgDBsqlpassword    = "sqlpass";
 $wgDBminWordLen                = 3;     # Match this to your MySQL fulltext
 $wgDBtransactions   = false; # Set to true if using InnoDB tables
 
-# This is the location of the convert program
-# from the ImageMagick toolbox. You need it for 
-# the image resizing functionality.
 #
-# Set $wgUseImageResize to true if you want to use this feature
+# Set $wgUseImageResize to true if you want to enable dynamic
+# server side image resizing ("Thumbnails")
 # 
 $wgUseImageResize              = false;
+# Resizing can be done using PHP's internal image libraries
+# or using ImageMagick. The later supports more file formats
+# than PHP, which only supports PNG, GIF, JPG, XBM and WBMP.
+#
+# Set $wgUseImageMagick to true to use Image Magick instead
+# of the builtin functions
+$wgUseImageMagick              = false;
 $wgImageMagickConvertCommand    = "/usr/bin/convert";
 
 # Turn this on during database maintenance
index 4b1a81f..1211182 100644 (file)
@@ -1509,6 +1509,7 @@ class Skin {
        function createThumb( $name, $width ) {
                global $wgUploadDirectory;
                global $wgImageMagickConvertCommand;
+               global $wgUseImageMagick;
                $imgPath   = wfImagePath( $name );
                $thumbName = $width."px-".$icon.$name;
                $thumbPath = wfImageArchiveDir( $thumbName, "thumb" )."/".$thumbName;
@@ -1516,11 +1517,68 @@ class Skin {
 
                if (     (! file_exists( $thumbPath )) 
                     ||  ( filemtime($thumbPath) < filemtime($imgPath) ) ) {
-                       $cmd  =  $wgImageMagickConvertCommand .
+                       if ( $wgUseImageMagick ) {
+                               # use ImageMagick
+                               $cmd  =  $wgImageMagickConvertCommand .
                                        " -quality 95 -geometry {$width}x ".
                                        escapeshellarg($imgPath) . " " .
                                        escapeshellarg($thumbPath);
-                       $conv = shell_exec( $cmd );
+                               $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 );
+
+
+                       }
+
                }
                return $thumbUrl;
        }