From 55fa614fe820f7c4e45f04d33379e1b6e66970a5 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Mon, 6 Feb 2006 05:19:46 +0000 Subject: [PATCH] Report errors from ImageMagick convert back to the user. Specify output bit depth when calling convert, for those people who are using an inefficient Q16 binary for scaling (i.e. almost everyone). Tweaked -quality. --- includes/Image.php | 58 +++++++++++++++++++++++++++++++++--------- includes/Linker.php | 16 +++++++++--- languages/Messages.php | 1 + 3 files changed, 60 insertions(+), 15 deletions(-) diff --git a/includes/Image.php b/includes/Image.php index 479fdcc6ec..8d62b2faa0 100644 --- a/includes/Image.php +++ b/includes/Image.php @@ -47,8 +47,9 @@ class Image $type, # MEDIATYPE_xxx (bitmap, drawing, audio...) $mime, # MIME type, determined by MimeMagic::guessMimeType $size, # Size in bytes (loadFromXxx) - $metadata, # Metadata - $dataLoaded; # Whether or not all this has been loaded from the database (loadFromXxx) + $metadata, # Metadata + $dataLoaded, # Whether or not all this has been loaded from the database (loadFromXxx) + $lastError; # Error string associated with a thumbnail display error /**#@-*/ @@ -983,6 +984,7 @@ class Image clearstatcache(); } + $done = true; if ( !file_exists( $thumbPath ) || filemtime( $thumbPath ) < wfTimestamp( TS_UNIX, $wgThumbnailEpoch ) ) { $oldThumbPath = wfDeprecatedThumbDir( $thumbName, 'thumb', $this->fromSharedDirectory ). @@ -1009,7 +1011,10 @@ class Image } } if ( !$done ) { - $this->reallyRenderThumb( $thumbPath, $width, $height ); + $this->lastError = $this->reallyRenderThumb( $thumbPath, $width, $height ); + if ( $this->lastError === true ) { + $done = true; + } # Purge squid # This has to be done after the image is updated and present for all machines on NFS, @@ -1025,7 +1030,11 @@ class Image } } - $thumb = new ThumbnailImage( $url, $width, $height, $thumbPath ); + if ( $done ) { + $thumb = new ThumbnailImage( $url, $width, $height, $thumbPath ); + } else { + $thumb = null; + } wfProfileOut( $fname ); return $thumb; } // END OF function renderThumb @@ -1034,6 +1043,10 @@ class Image * Really render a thumbnail * Call this only for images for which canRender() returns true. * + * @param string $thumbPath Path to thumbnail + * @param int $width Desired width in pixels + * @param int $height Desired height in pixels + * @return bool True on error, false or error string on failure. * @access private */ function reallyRenderThumb( $thumbPath, $width, $height ) { @@ -1042,6 +1055,7 @@ class Image $this->load(); + $err = false; if( $this->mime === "image/svg" ) { #Right now we have only SVG @@ -1058,26 +1072,34 @@ class Image $wgSVGConverters[$wgSVGConverter] ); wfProfileIn( 'rsvg' ); wfDebug( "reallyRenderThumb SVG: $cmd\n" ); - $conv = wfShellExec( $cmd ); + $err = wfShellExec( $cmd ); wfProfileOut( 'rsvg' ); - } else { - $conv = false; } } elseif ( $wgUseImageMagick ) { # use ImageMagick + + if ( $this->mime == 'image/jpeg' ) { + $quality = "-quality 80"; // 80% + } elseif ( $this->mime == 'image/png' ) { + $quality = "-quality 95"; // zlib 9, adaptive filtering + } else { + $quality = ''; // default + } + # Specify white background color, will be used for transparent images # in Internet Explorer/Windows instead of default black. # Note, we specify "-size {$width}" and NOT "-size {$width}x{$height}". # It seems that ImageMagick has a bug wherein it produces thumbnails of # the wrong size in the second case. - $cmd = $wgImageMagickConvertCommand . - " -quality 85 -background white -size {$width} ". - wfEscapeShellArg($this->imagePath) . " -resize {$width}x{$height} " . - wfEscapeShellArg($thumbPath); + + $cmd = wfEscapeShellArg($wgImageMagickConvertCommand) . + " {$quality} -background white -size {$width} ". + wfEscapeShellArg($this->imagePath) . " -resize {$width}x{$height} -depth 8 " . + wfEscapeShellArg($thumbPath) . " 2>&1"; wfDebug("reallyRenderThumb: running ImageMagick: $cmd\n"); wfProfileIn( 'convert' ); - $conv = wfShellExec( $cmd ); + $err = wfShellExec( $cmd ); wfProfileOut( 'convert' ); } else { # Use PHP's builtin GD library functions. @@ -1134,8 +1156,20 @@ class Image $thumbstat = stat( $thumbPath ); if( $thumbstat['size'] == 0 ) { unlink( $thumbPath ); + } else { + // All good + $err = true; } } + if ( $err !== true ) { + return wfMsg( 'thumbnail_error', $err ); + } else { + return true; + } + } + + function getLastError() { + return $this->lastError; } function imageJpegWrapper( $dst_image, $thumbPath ) { diff --git a/includes/Linker.php b/includes/Linker.php index 3142c0f15e..fb3deefb4c 100644 --- a/includes/Linker.php +++ b/includes/Linker.php @@ -452,7 +452,7 @@ class Linker { } $url = $img->getViewURL(); - $prefix = $postfix = ''; + $error = $prefix = $postfix = ''; wfDebug( "makeImageLinkObj: '$width'x'$height'\n" ); @@ -514,6 +514,8 @@ class Linker { wfDebug( "makeImageLinkObj: client-size set to '$width x $height'\n" ); $url = $thumb->getUrl(); + } else { + $error = htmlspecialchars( $img->getLastError() ); } } } else { @@ -523,7 +525,9 @@ class Linker { wfDebug( "makeImageLinkObj2: '$width'x'$height'\n" ); $u = $nt->escapeLocalURL(); - if ( $url == '' ) { + if ( $error ) { + $s = $error; + } elseif ( $url == '' ) { $s = $this->makeBrokenImageLinkObj( $img->getTitle() ); //$s .= "
{$alt}
{$url}
\n"; } else { @@ -548,6 +552,7 @@ class Linker { global $wgStylePath, $wgContLang; $url = $img->getViewURL(); $thumbUrl = ''; + $error = ''; $width = $height = 0; if ( $img->exists() ) { @@ -574,6 +579,8 @@ class Linker { $thumbUrl = $thumb->getUrl(); $boxwidth = $thumb->width; $boxheight = $thumb->height; + } else { + $error = $img->getLastError(); } } } @@ -605,7 +612,10 @@ class Linker { // Couldn't generate thumbnail? Scale the image client-side. $thumbUrl = $url; } - if( !$img->exists() ) { + if ( $error ) { + $s .= htmlspecialchars( $error ); + $zoomicon = ''; + } elseif( !$img->exists() ) { $s .= $this->makeBrokenImageLinkObj( $img->getTitle() ); $zoomicon = ''; } else { diff --git a/languages/Messages.php b/languages/Messages.php index 69c9004937..8924eb998e 100644 --- a/languages/Messages.php +++ b/languages/Messages.php @@ -1377,6 +1377,7 @@ In the latter case you can also use a link, e.g. [[{{ns:Special}}:Export/{{Media 'thumbnail-more' => 'Enlarge', 'missingimage' => 'Missing image
$1', 'filemissing' => 'File missing', +'thumbnail_error' => 'Error creating thumbnail: $1', # Special:Import 'import' => 'Import pages', -- 2.20.1