2007-04-20 12:31:36 +00:00
|
|
|
<?php
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
/**
|
2012-05-03 20:13:10 +00:00
|
|
|
* Generic handler for bitmap images.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-15 17:27:41 +00:00
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup Media
|
|
|
|
|
*/
|
2007-04-20 12:31:36 +00:00
|
|
|
|
2007-04-24 06:53:31 +00:00
|
|
|
/**
|
2010-08-15 17:27:41 +00:00
|
|
|
* Generic handler for bitmap images
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Media
|
2007-04-24 06:53:31 +00:00
|
|
|
*/
|
2007-04-20 12:31:36 +00:00
|
|
|
class BitmapHandler extends ImageHandler {
|
2011-02-18 23:34:24 +00:00
|
|
|
/**
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param File $image
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params Transform parameters. Entries with the keys 'width'
|
2011-09-09 20:13:09 +00:00
|
|
|
* and 'height' are the respective screen width and height, while the keys
|
|
|
|
|
* 'physicalWidth' and 'physicalHeight' indicate the thumbnail dimensions.
|
2011-02-18 23:34:24 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2009-08-19 02:07:00 +00:00
|
|
|
function normaliseParams( $image, &$params ) {
|
2007-04-20 12:31:36 +00:00
|
|
|
if ( !parent::normaliseParams( $image, $params ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-09 20:13:09 +00:00
|
|
|
# Obtain the source, pre-rotation dimensions
|
2007-04-20 12:31:36 +00:00
|
|
|
$srcWidth = $image->getWidth( $params['page'] );
|
|
|
|
|
$srcHeight = $image->getHeight( $params['page'] );
|
2011-04-28 20:18:32 +00:00
|
|
|
|
2011-09-09 20:13:09 +00:00
|
|
|
# Don't make an image bigger than the source
|
|
|
|
|
if ( $params['physicalWidth'] >= $srcWidth ) {
|
|
|
|
|
$params['physicalWidth'] = $srcWidth;
|
|
|
|
|
$params['physicalHeight'] = $srcHeight;
|
|
|
|
|
|
|
|
|
|
# Skip scaling limit checks if no scaling is required
|
|
|
|
|
# due to requested size being bigger than source.
|
|
|
|
|
if ( !$image->mustRender() ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-11-02 20:48:50 +00:00
|
|
|
|
|
|
|
|
# Check if the file is smaller than the maximum image area for thumbnailing
|
|
|
|
|
$checkImageAreaHookResult = null;
|
2013-12-05 11:33:18 +00:00
|
|
|
wfRunHooks(
|
|
|
|
|
'BitmapHandlerCheckImageArea',
|
|
|
|
|
array( $image, &$params, &$checkImageAreaHookResult )
|
|
|
|
|
);
|
|
|
|
|
|
2011-11-02 20:48:50 +00:00
|
|
|
if ( is_null( $checkImageAreaHookResult ) ) {
|
|
|
|
|
global $wgMaxImageArea;
|
2011-12-27 00:28:23 +00:00
|
|
|
|
2013-12-05 10:05:05 +00:00
|
|
|
if ( $srcWidth * $srcHeight > $wgMaxImageArea
|
|
|
|
|
&& !( $image->getMimeType() == 'image/jpeg'
|
2013-12-05 20:55:30 +00:00
|
|
|
&& self::getScalerType( false, false ) == 'im' )
|
2013-12-05 10:05:05 +00:00
|
|
|
) {
|
2011-11-02 20:48:50 +00:00
|
|
|
# Only ImageMagick can efficiently downsize jpg images without loading
|
|
|
|
|
# the entire file in memory
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return $checkImageAreaHookResult;
|
2011-10-15 20:36:02 +00:00
|
|
|
}
|
2011-12-27 00:28:23 +00:00
|
|
|
|
2011-11-02 20:48:50 +00:00
|
|
|
return true;
|
2007-04-20 12:31:36 +00:00
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-07-15 15:13:18 +00:00
|
|
|
/**
|
|
|
|
|
* Extracts the width/height if the image will be scaled before rotating
|
2011-07-25 22:01:19 +00:00
|
|
|
*
|
* (bug 6672, 31024) Fixes for handling of images with an EXIF orientation
- sets an image's reported width/height to the logical form (portait image reports itself as portait)
- everything works in logical coordinates when sizing -- we don't touch the physical pre-rotation dimensions again until it's actual low-level resize time. This fixes several problems with incorrect thumb sizing (eg getting a 600x800 image when we asked for something that fits in 800x600 box)
- fixes unit test cases in ExifRotationTest that were reporting that the width/height were coming back with the physical form which we don't want
- removes some test cases on ExifRotationTest that tested dimension swapping in a place where we don't want it
- ensures that only logical width/height need be exposed to API etc, making exif-rotated images work via ForeignAPIRepo
Note that this may actually cause file metadata to get loaded twice during File::getPropsFromPath, as the $image parameter it passes in to the handler's getImageSize function is bogus and can't be used to fetch an already-loaded metadata blob. This should not generally be too expensive though; it's not a fast path.
Rotated files that were uploaded under previous versions may still have their width/height reversed; an action=purge on the file page will refresh it and cause thumbs to be regenerated.
Follows up on r79845, r90016, r92246, r92279, r96687, r97651, r97656, r97659.
Needs merge to 1.18.
2011-09-20 22:13:34 +00:00
|
|
|
* This will match the physical size/aspect ratio of the original image
|
|
|
|
|
* prior to application of the rotation -- so for a portrait image that's
|
|
|
|
|
* stored as raw landscape with 90-degress rotation, the resulting size
|
|
|
|
|
* will be wider than it is tall.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params Parameters as returned by normaliseParams
|
|
|
|
|
* @param int $rotation The rotation angle that will be applied
|
2011-07-15 15:13:18 +00:00
|
|
|
* @return array ($width, $height) array
|
|
|
|
|
*/
|
|
|
|
|
public function extractPreRotationDimensions( $params, $rotation ) {
|
|
|
|
|
if ( $rotation == 90 || $rotation == 270 ) {
|
|
|
|
|
# We'll resize before rotation, so swap the dimensions again
|
|
|
|
|
$width = $params['physicalHeight'];
|
|
|
|
|
$height = $params['physicalWidth'];
|
|
|
|
|
} else {
|
|
|
|
|
$width = $params['physicalWidth'];
|
|
|
|
|
$height = $params['physicalHeight'];
|
|
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2011-07-15 15:13:18 +00:00
|
|
|
return array( $width, $height );
|
|
|
|
|
}
|
2010-10-30 19:11:30 +00:00
|
|
|
|
2011-02-18 23:34:24 +00:00
|
|
|
/**
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param File $image
|
|
|
|
|
* @param string $dstPath
|
|
|
|
|
* @param string $dstUrl
|
|
|
|
|
* @param array $params
|
2011-02-18 23:34:24 +00:00
|
|
|
* @param int $flags
|
|
|
|
|
* @return MediaTransformError|ThumbnailImage|TransformParameterError
|
|
|
|
|
*/
|
2009-08-19 02:07:00 +00:00
|
|
|
function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
|
2007-04-20 12:31:36 +00:00
|
|
|
if ( !$this->normaliseParams( $image, $params ) ) {
|
|
|
|
|
return new TransformParameterError( $params );
|
|
|
|
|
}
|
2014-06-03 16:09:30 +00:00
|
|
|
|
2010-10-30 19:04:26 +00:00
|
|
|
# Create a parameter array to pass to the scaler
|
|
|
|
|
$scalerParams = array(
|
|
|
|
|
# The size to which the image will be resized
|
|
|
|
|
'physicalWidth' => $params['physicalWidth'],
|
|
|
|
|
'physicalHeight' => $params['physicalHeight'],
|
2010-10-30 20:02:53 +00:00
|
|
|
'physicalDimensions' => "{$params['physicalWidth']}x{$params['physicalHeight']}",
|
2010-10-30 19:04:26 +00:00
|
|
|
# The size of the image on the page
|
|
|
|
|
'clientWidth' => $params['width'],
|
|
|
|
|
'clientHeight' => $params['height'],
|
2013-05-10 11:51:06 +00:00
|
|
|
# Comment as will be added to the Exif of the thumbnail
|
2013-12-05 10:05:05 +00:00
|
|
|
'comment' => isset( $params['descriptionUrl'] )
|
|
|
|
|
? "File source: {$params['descriptionUrl']}"
|
|
|
|
|
: '',
|
2010-10-30 19:04:26 +00:00
|
|
|
# Properties of the original image
|
|
|
|
|
'srcWidth' => $image->getWidth(),
|
|
|
|
|
'srcHeight' => $image->getHeight(),
|
|
|
|
|
'mimeType' => $image->getMimeType(),
|
|
|
|
|
'dstPath' => $dstPath,
|
2011-03-18 22:28:19 +00:00
|
|
|
'dstUrl' => $dstUrl,
|
2010-10-30 19:04:26 +00:00
|
|
|
);
|
|
|
|
|
|
2014-05-09 05:51:59 +00:00
|
|
|
if ( isset( $params['quality'] ) && $params['quality'] === 'low' ) {
|
|
|
|
|
$scalerParams['quality'] = 30;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-02 20:48:50 +00:00
|
|
|
# Determine scaler type
|
|
|
|
|
$scaler = self::getScalerType( $dstPath );
|
2011-12-27 00:28:23 +00:00
|
|
|
|
2013-12-05 11:33:18 +00:00
|
|
|
wfDebug( __METHOD__ . ": creating {$scalerParams['physicalDimensions']} " .
|
|
|
|
|
"thumbnail at $dstPath using scaler $scaler\n" );
|
2007-04-20 12:31:36 +00:00
|
|
|
|
2010-10-30 19:11:30 +00:00
|
|
|
if ( !$image->mustRender() &&
|
2013-12-05 10:05:05 +00:00
|
|
|
$scalerParams['physicalWidth'] == $scalerParams['srcWidth']
|
|
|
|
|
&& $scalerParams['physicalHeight'] == $scalerParams['srcHeight']
|
2014-05-09 05:51:59 +00:00
|
|
|
&& !isset( $scalerParams['quality'] )
|
2013-12-05 10:05:05 +00:00
|
|
|
) {
|
2010-10-30 19:11:30 +00:00
|
|
|
|
2007-04-20 12:31:36 +00:00
|
|
|
# normaliseParams (or the user) wants us to return the unscaled image
|
2010-10-30 19:04:26 +00:00
|
|
|
wfDebug( __METHOD__ . ": returning unscaled image\n" );
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2010-10-30 19:04:26 +00:00
|
|
|
return $this->getClientScalingThumbnailImage( $image, $scalerParams );
|
2007-04-20 12:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $scaler == 'client' ) {
|
|
|
|
|
# Client-side image scaling, use the source URL
|
|
|
|
|
# Using the destination URL in a TRANSFORM_LATER request would be incorrect
|
2010-10-30 19:04:26 +00:00
|
|
|
return $this->getClientScalingThumbnailImage( $image, $scalerParams );
|
2007-04-20 12:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $flags & self::TRANSFORM_LATER ) {
|
2010-10-30 19:04:26 +00:00
|
|
|
wfDebug( __METHOD__ . ": Transforming later per flags.\n" );
|
2014-05-09 05:51:59 +00:00
|
|
|
$newParams = array(
|
2012-09-01 16:12:48 +00:00
|
|
|
'width' => $scalerParams['clientWidth'],
|
|
|
|
|
'height' => $scalerParams['clientHeight']
|
|
|
|
|
);
|
2014-05-09 05:51:59 +00:00
|
|
|
if ( isset( $params['quality'] ) ) {
|
|
|
|
|
$newParams['quality'] = $params['quality'];
|
|
|
|
|
}
|
|
|
|
|
return new ThumbnailImage( $image, $dstUrl, false, $newParams );
|
2007-04-20 12:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
2010-10-30 19:04:26 +00:00
|
|
|
# Try to make a target path for the thumbnail
|
2011-07-26 02:44:27 +00:00
|
|
|
if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) {
|
2013-12-05 11:33:18 +00:00
|
|
|
wfDebug( __METHOD__ . ": Unable to create thumbnail destination " .
|
|
|
|
|
"directory, falling back to client scaling\n" );
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2010-10-30 19:04:26 +00:00
|
|
|
return $this->getClientScalingThumbnailImage( $image, $scalerParams );
|
2007-04-20 12:31:36 +00:00
|
|
|
}
|
2011-04-28 20:18:32 +00:00
|
|
|
|
2012-02-01 04:44:08 +00:00
|
|
|
# Transform functions and binaries need a FS source file
|
2014-06-03 16:09:30 +00:00
|
|
|
$thumbnailSource = $image->getThumbnailSource( $params );
|
|
|
|
|
|
|
|
|
|
$scalerParams['srcPath'] = $thumbnailSource['path'];
|
|
|
|
|
$scalerParams['srcWidth'] = $thumbnailSource['width'];
|
|
|
|
|
$scalerParams['srcHeight'] = $thumbnailSource['height'];
|
|
|
|
|
|
2013-11-08 19:23:17 +00:00
|
|
|
if ( $scalerParams['srcPath'] === false ) { // Failed to get local copy
|
|
|
|
|
wfDebugLog( 'thumbnail',
|
|
|
|
|
sprintf( 'Thumbnail failed on %s: could not get local copy of "%s"',
|
|
|
|
|
wfHostname(), $image->getName() ) );
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2013-11-08 19:23:17 +00:00
|
|
|
return new MediaTransformError( 'thumbnail_error',
|
2014-01-08 09:38:42 +00:00
|
|
|
$scalerParams['clientWidth'], $scalerParams['clientHeight'],
|
|
|
|
|
wfMessage( 'filemissing' )->text()
|
|
|
|
|
);
|
2013-11-08 19:23:17 +00:00
|
|
|
}
|
2012-02-01 04:44:08 +00:00
|
|
|
|
2011-03-18 22:28:19 +00:00
|
|
|
# Try a hook
|
|
|
|
|
$mto = null;
|
|
|
|
|
wfRunHooks( 'BitmapHandlerTransform', array( $this, $image, &$scalerParams, &$mto ) );
|
|
|
|
|
if ( !is_null( $mto ) ) {
|
|
|
|
|
wfDebug( __METHOD__ . ": Hook to BitmapHandlerTransform created an mto\n" );
|
|
|
|
|
$scaler = 'hookaborted';
|
|
|
|
|
}
|
2011-10-29 01:17:26 +00:00
|
|
|
|
2010-10-30 19:04:26 +00:00
|
|
|
switch ( $scaler ) {
|
2011-03-18 22:28:19 +00:00
|
|
|
case 'hookaborted':
|
|
|
|
|
# Handled by the hook above
|
2013-12-05 19:27:27 +00:00
|
|
|
/** @var MediaTransformOutput $mto */
|
2011-03-18 22:28:19 +00:00
|
|
|
$err = $mto->isError() ? $mto : false;
|
|
|
|
|
break;
|
2010-10-30 19:04:26 +00:00
|
|
|
case 'im':
|
|
|
|
|
$err = $this->transformImageMagick( $image, $scalerParams );
|
|
|
|
|
break;
|
|
|
|
|
case 'custom':
|
|
|
|
|
$err = $this->transformCustom( $image, $scalerParams );
|
|
|
|
|
break;
|
2011-03-12 19:32:39 +00:00
|
|
|
case 'imext':
|
|
|
|
|
$err = $this->transformImageMagickExt( $image, $scalerParams );
|
2011-03-12 21:35:27 +00:00
|
|
|
break;
|
2010-10-30 19:04:26 +00:00
|
|
|
case 'gd':
|
|
|
|
|
default:
|
|
|
|
|
$err = $this->transformGd( $image, $scalerParams );
|
|
|
|
|
break;
|
|
|
|
|
}
|
2010-10-30 19:11:30 +00:00
|
|
|
|
2010-10-30 19:04:26 +00:00
|
|
|
# Remove the file if a zero-byte thumbnail was created, or if there was an error
|
|
|
|
|
$removed = $this->removeBadFile( $dstPath, (bool)$err );
|
|
|
|
|
if ( $err ) {
|
|
|
|
|
# transform returned MediaTransforError
|
|
|
|
|
return $err;
|
|
|
|
|
} elseif ( $removed ) {
|
|
|
|
|
# Thumbnail was zero-byte and had to be removed
|
2010-10-30 19:11:30 +00:00
|
|
|
return new MediaTransformError( 'thumbnail_error',
|
2014-01-08 09:38:42 +00:00
|
|
|
$scalerParams['clientWidth'], $scalerParams['clientHeight'],
|
|
|
|
|
wfMessage( 'unknown-error' )->text()
|
|
|
|
|
);
|
2011-03-18 22:28:19 +00:00
|
|
|
} elseif ( $mto ) {
|
|
|
|
|
return $mto;
|
2010-10-30 19:04:26 +00:00
|
|
|
} else {
|
2014-05-09 05:51:59 +00:00
|
|
|
$newParams = array(
|
2012-09-01 16:12:48 +00:00
|
|
|
'width' => $scalerParams['clientWidth'],
|
|
|
|
|
'height' => $scalerParams['clientHeight']
|
|
|
|
|
);
|
2014-05-09 05:51:59 +00:00
|
|
|
if ( isset( $params['quality'] ) ) {
|
|
|
|
|
$newParams['quality'] = $params['quality'];
|
|
|
|
|
}
|
|
|
|
|
return new ThumbnailImage( $image, $dstUrl, $dstPath, $newParams );
|
2010-10-30 19:04:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2011-04-28 20:18:32 +00:00
|
|
|
|
2011-01-07 22:12:05 +00:00
|
|
|
/**
|
2011-04-28 20:18:32 +00:00
|
|
|
* Returns which scaler type should be used. Creates parent directories
|
2011-01-07 22:12:05 +00:00
|
|
|
* for $dstPath and returns 'client' on error
|
2011-04-28 20:18:32 +00:00
|
|
|
*
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param string $dstPath
|
|
|
|
|
* @param bool $checkDstPath
|
|
|
|
|
* @return string One of client, im, custom, gd, imext
|
2011-01-07 22:12:05 +00:00
|
|
|
*/
|
2011-01-22 22:34:36 +00:00
|
|
|
protected static function getScalerType( $dstPath, $checkDstPath = true ) {
|
2011-01-07 22:12:05 +00:00
|
|
|
global $wgUseImageResize, $wgUseImageMagick, $wgCustomConvertCommand;
|
2011-04-28 20:18:32 +00:00
|
|
|
|
2011-01-07 22:12:05 +00:00
|
|
|
if ( !$dstPath && $checkDstPath ) {
|
|
|
|
|
# No output path available, client side scaling only
|
|
|
|
|
$scaler = 'client';
|
|
|
|
|
} elseif ( !$wgUseImageResize ) {
|
|
|
|
|
$scaler = 'client';
|
2011-03-12 22:51:45 +00:00
|
|
|
} elseif ( $wgUseImageMagick ) {
|
2011-01-07 22:12:05 +00:00
|
|
|
$scaler = 'im';
|
|
|
|
|
} elseif ( $wgCustomConvertCommand ) {
|
|
|
|
|
$scaler = 'custom';
|
|
|
|
|
} elseif ( function_exists( 'imagecreatetruecolor' ) ) {
|
|
|
|
|
$scaler = 'gd';
|
2011-03-12 22:51:45 +00:00
|
|
|
} elseif ( class_exists( 'Imagick' ) ) {
|
2011-03-12 19:32:39 +00:00
|
|
|
$scaler = 'imext';
|
2011-01-07 22:12:05 +00:00
|
|
|
} else {
|
|
|
|
|
$scaler = 'client';
|
|
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2011-01-07 22:12:05 +00:00
|
|
|
return $scaler;
|
|
|
|
|
}
|
2010-10-30 19:11:30 +00:00
|
|
|
|
2010-10-30 19:04:26 +00:00
|
|
|
/**
|
|
|
|
|
* Get a ThumbnailImage that respresents an image that will be scaled
|
|
|
|
|
* client side
|
2010-10-30 19:11:30 +00:00
|
|
|
*
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param File $image File associated with this thumbnail
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $scalerParams Array with scaler params
|
2010-10-30 19:04:26 +00:00
|
|
|
* @return ThumbnailImage
|
* (bug 6672, 31024) Fixes for handling of images with an EXIF orientation
- sets an image's reported width/height to the logical form (portait image reports itself as portait)
- everything works in logical coordinates when sizing -- we don't touch the physical pre-rotation dimensions again until it's actual low-level resize time. This fixes several problems with incorrect thumb sizing (eg getting a 600x800 image when we asked for something that fits in 800x600 box)
- fixes unit test cases in ExifRotationTest that were reporting that the width/height were coming back with the physical form which we don't want
- removes some test cases on ExifRotationTest that tested dimension swapping in a place where we don't want it
- ensures that only logical width/height need be exposed to API etc, making exif-rotated images work via ForeignAPIRepo
Note that this may actually cause file metadata to get loaded twice during File::getPropsFromPath, as the $image parameter it passes in to the handler's getImageSize function is bogus and can't be used to fetch an already-loaded metadata blob. This should not generally be too expensive though; it's not a fast path.
Rotated files that were uploaded under previous versions may still have their width/height reversed; an action=purge on the file page will refresh it and cause thumbs to be regenerated.
Follows up on r79845, r90016, r92246, r92279, r96687, r97651, r97656, r97659.
Needs merge to 1.18.
2011-09-20 22:13:34 +00:00
|
|
|
*
|
2013-12-05 19:27:27 +00:00
|
|
|
* @todo FIXME: No rotation support
|
2010-10-30 19:04:26 +00:00
|
|
|
*/
|
2012-09-01 16:12:48 +00:00
|
|
|
protected function getClientScalingThumbnailImage( $image, $scalerParams ) {
|
|
|
|
|
$params = array(
|
|
|
|
|
'width' => $scalerParams['clientWidth'],
|
|
|
|
|
'height' => $scalerParams['clientHeight']
|
|
|
|
|
);
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2012-09-01 16:12:48 +00:00
|
|
|
return new ThumbnailImage( $image, $image->getURL(), null, $params );
|
2010-10-30 19:04:26 +00:00
|
|
|
}
|
2011-04-28 20:18:32 +00:00
|
|
|
|
2010-10-30 19:04:26 +00:00
|
|
|
/**
|
|
|
|
|
* Transform an image using ImageMagick
|
2010-10-30 19:11:30 +00:00
|
|
|
*
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param File $image File associated with this thumbnail
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params Array with scaler params
|
2010-10-30 19:11:30 +00:00
|
|
|
*
|
2012-08-10 08:59:55 +00:00
|
|
|
* @return MediaTransformError Error object if error occurred, false (=no error) otherwise
|
2010-10-30 19:04:26 +00:00
|
|
|
*/
|
|
|
|
|
protected function transformImageMagick( $image, $params ) {
|
2010-10-30 19:11:30 +00:00
|
|
|
# use ImageMagick
|
2013-12-05 10:05:05 +00:00
|
|
|
global $wgSharpenReductionThreshold, $wgSharpenParameter, $wgMaxAnimatedGifArea,
|
2010-10-30 19:11:30 +00:00
|
|
|
$wgImageMagickTempDir, $wgImageMagickConvertCommand;
|
|
|
|
|
|
2014-01-27 21:49:30 +00:00
|
|
|
$quality = array();
|
|
|
|
|
$sharpen = array();
|
2010-10-30 19:11:30 +00:00
|
|
|
$scene = false;
|
2014-01-27 21:49:30 +00:00
|
|
|
$animation_pre = array();
|
|
|
|
|
$animation_post = array();
|
|
|
|
|
$decoderHint = array();
|
2010-10-30 19:11:30 +00:00
|
|
|
if ( $params['mimeType'] == 'image/jpeg' ) {
|
2014-05-09 05:51:59 +00:00
|
|
|
$qualityVal = isset( $params['quality'] ) ? (string) $params['quality'] : null;
|
|
|
|
|
$quality = array( '-quality', $qualityVal ?: '80' ); // 80%
|
2010-10-30 19:11:30 +00:00
|
|
|
# Sharpening, see bug 6193
|
|
|
|
|
if ( ( $params['physicalWidth'] + $params['physicalHeight'] )
|
2013-12-05 10:05:05 +00:00
|
|
|
/ ( $params['srcWidth'] + $params['srcHeight'] )
|
|
|
|
|
< $wgSharpenReductionThreshold
|
|
|
|
|
) {
|
2014-01-27 21:49:30 +00:00
|
|
|
$sharpen = array( '-sharpen', $wgSharpenParameter );
|
2010-10-30 19:11:30 +00:00
|
|
|
}
|
2011-12-08 18:07:26 +00:00
|
|
|
if ( version_compare( $this->getMagickVersion(), "6.5.6" ) >= 0 ) {
|
2011-12-08 16:18:18 +00:00
|
|
|
// JPEG decoder hint to reduce memory, available since IM 6.5.6-2
|
2014-01-27 21:49:30 +00:00
|
|
|
$decoderHint = array( '-define', "jpeg:size={$params['physicalDimensions']}" );
|
2011-12-08 16:18:18 +00:00
|
|
|
}
|
2010-10-30 19:11:30 +00:00
|
|
|
} elseif ( $params['mimeType'] == 'image/png' ) {
|
2014-01-27 21:49:30 +00:00
|
|
|
$quality = array( '-quality', '95' ); // zlib 9, adaptive filtering
|
2010-10-30 19:11:30 +00:00
|
|
|
|
|
|
|
|
} elseif ( $params['mimeType'] == 'image/gif' ) {
|
2011-11-28 04:40:31 +00:00
|
|
|
if ( $this->getImageArea( $image ) > $wgMaxAnimatedGifArea ) {
|
2010-10-30 19:11:30 +00:00
|
|
|
// Extract initial frame only; we're so big it'll
|
|
|
|
|
// be a total drag. :P
|
|
|
|
|
$scene = 0;
|
|
|
|
|
} elseif ( $this->isAnimatedImage( $image ) ) {
|
|
|
|
|
// Coalesce is needed to scale animated GIFs properly (bug 1017).
|
2014-01-27 21:49:30 +00:00
|
|
|
$animation_pre = array( '-coalesce' );
|
2010-10-30 19:11:30 +00:00
|
|
|
// We optimize the output, but -optimize is broken,
|
|
|
|
|
// use optimizeTransparency instead (bug 11822)
|
|
|
|
|
if ( version_compare( $this->getMagickVersion(), "6.3.5" ) >= 0 ) {
|
2014-01-27 21:49:30 +00:00
|
|
|
$animation_post = array( '-fuzz', '5%', '-layers', 'optimizeTransparency' );
|
2008-10-13 21:50:16 +00:00
|
|
|
}
|
2007-04-20 12:31:36 +00:00
|
|
|
}
|
2011-12-27 00:46:44 +00:00
|
|
|
} elseif ( $params['mimeType'] == 'image/x-xcf' ) {
|
2014-07-04 04:46:10 +00:00
|
|
|
// Before merging layers, we need to set the background
|
|
|
|
|
// to be transparent to preserve alpha, as -layers merge
|
|
|
|
|
// merges all layers on to a canvas filled with the
|
|
|
|
|
// background colour. After merging we reset the background
|
|
|
|
|
// to be white for the default background colour setting
|
|
|
|
|
// in the PNG image (which is used in old IE)
|
|
|
|
|
$animation_post = array(
|
|
|
|
|
'-background', 'transparent',
|
|
|
|
|
'-layers', 'merge',
|
|
|
|
|
'-background', 'white',
|
|
|
|
|
);
|
2014-06-07 21:29:18 +00:00
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$xcfMeta = unserialize( $image->getMetadata() );
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
if ( $xcfMeta
|
|
|
|
|
&& isset( $xcfMeta['colorType'] )
|
|
|
|
|
&& $xcfMeta['colorType'] === 'greyscale-alpha'
|
|
|
|
|
&& version_compare( $this->getMagickVersion(), "6.8.9-3" ) < 0
|
|
|
|
|
) {
|
|
|
|
|
// bug 66323 - Greyscale images not rendered properly.
|
|
|
|
|
// So only take the "red" channel.
|
|
|
|
|
$channelOnly = array( '-channel', 'R', '-separate' );
|
|
|
|
|
$animation_post = array_merge( $animation_post, $channelOnly );
|
|
|
|
|
}
|
2010-10-30 19:11:30 +00:00
|
|
|
}
|
2007-04-20 12:31:36 +00:00
|
|
|
|
2010-10-30 19:11:30 +00:00
|
|
|
// Use one thread only, to avoid deadlock bugs on OOM
|
|
|
|
|
$env = array( 'OMP_NUM_THREADS' => 1 );
|
|
|
|
|
if ( strval( $wgImageMagickTempDir ) !== '' ) {
|
|
|
|
|
$env['MAGICK_TMPDIR'] = $wgImageMagickTempDir;
|
|
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-07-15 15:13:18 +00:00
|
|
|
$rotation = $this->getRotation( $image );
|
|
|
|
|
list( $width, $height ) = $this->extractPreRotationDimensions( $params, $rotation );
|
2008-11-02 16:45:52 +00:00
|
|
|
|
2014-01-27 21:49:30 +00:00
|
|
|
$cmd = call_user_func_array( 'wfEscapeShellArg', array_merge(
|
|
|
|
|
array( $wgImageMagickConvertCommand ),
|
|
|
|
|
$quality,
|
2010-10-30 19:11:30 +00:00
|
|
|
// Specify white background color, will be used for transparent images
|
|
|
|
|
// in Internet Explorer/Windows instead of default black.
|
2014-01-27 21:49:30 +00:00
|
|
|
array( '-background', 'white' ),
|
|
|
|
|
$decoderHint,
|
|
|
|
|
array( $this->escapeMagickInput( $params['srcPath'], $scene ) ),
|
|
|
|
|
$animation_pre,
|
2010-10-30 19:11:30 +00:00
|
|
|
// For the -thumbnail option a "!" is needed to force exact size,
|
|
|
|
|
// or ImageMagick may decide your ratio is wrong and slice off
|
|
|
|
|
// a pixel.
|
2014-01-27 21:49:30 +00:00
|
|
|
array( '-thumbnail', "{$width}x{$height}!" ),
|
2010-11-24 22:50:46 +00:00
|
|
|
// Add the source url as a comment to the thumb, but don't add the flag if there's no comment
|
2010-11-25 12:34:21 +00:00
|
|
|
( $params['comment'] !== ''
|
2014-01-27 21:49:30 +00:00
|
|
|
? array( '-set', 'comment', $this->escapeMagickProperty( $params['comment'] ) )
|
|
|
|
|
: array() ),
|
|
|
|
|
array( '-depth', 8 ),
|
|
|
|
|
$sharpen,
|
|
|
|
|
array( '-rotate', "-$rotation" ),
|
|
|
|
|
$animation_post,
|
|
|
|
|
array( $this->escapeMagickOutput( $params['dstPath'] ) ) ) );
|
2010-10-30 19:11:30 +00:00
|
|
|
|
|
|
|
|
wfDebug( __METHOD__ . ": running ImageMagick: $cmd\n" );
|
|
|
|
|
wfProfileIn( 'convert' );
|
|
|
|
|
$retval = 0;
|
2013-09-12 01:40:56 +00:00
|
|
|
$err = wfShellExecWithStderr( $cmd, $retval, $env );
|
2010-10-30 19:11:30 +00:00
|
|
|
wfProfileOut( 'convert' );
|
|
|
|
|
|
|
|
|
|
if ( $retval !== 0 ) {
|
|
|
|
|
$this->logErrorForExternalProcess( $retval, $err, $cmd );
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2013-09-28 21:35:41 +00:00
|
|
|
return $this->getMediaTransformError( $params, "$err\nError code: $retval" );
|
2010-10-30 19:11:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false; # No error
|
2010-10-30 19:04:26 +00:00
|
|
|
}
|
2011-04-28 20:18:32 +00:00
|
|
|
|
2011-03-12 19:32:39 +00:00
|
|
|
/**
|
|
|
|
|
* Transform an image using the Imagick PHP extension
|
2011-04-28 20:18:32 +00:00
|
|
|
*
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param File $image File associated with this thumbnail
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params Array with scaler params
|
2011-03-12 19:32:39 +00:00
|
|
|
*
|
2012-08-10 08:59:55 +00:00
|
|
|
* @return MediaTransformError Error object if error occurred, false (=no error) otherwise
|
2011-03-12 19:32:39 +00:00
|
|
|
*/
|
|
|
|
|
protected function transformImageMagickExt( $image, $params ) {
|
|
|
|
|
global $wgSharpenReductionThreshold, $wgSharpenParameter, $wgMaxAnimatedGifArea;
|
2011-04-28 20:18:32 +00:00
|
|
|
|
2011-03-12 19:32:39 +00:00
|
|
|
try {
|
|
|
|
|
$im = new Imagick();
|
|
|
|
|
$im->readImage( $params['srcPath'] );
|
2011-04-28 20:18:32 +00:00
|
|
|
|
2011-03-12 19:32:39 +00:00
|
|
|
if ( $params['mimeType'] == 'image/jpeg' ) {
|
|
|
|
|
// Sharpening, see bug 6193
|
|
|
|
|
if ( ( $params['physicalWidth'] + $params['physicalHeight'] )
|
2013-12-05 10:05:05 +00:00
|
|
|
/ ( $params['srcWidth'] + $params['srcHeight'] )
|
|
|
|
|
< $wgSharpenReductionThreshold
|
|
|
|
|
) {
|
2011-03-12 19:32:39 +00:00
|
|
|
// Hack, since $wgSharpenParamater is written specifically for the command line convert
|
|
|
|
|
list( $radius, $sigma ) = explode( 'x', $wgSharpenParameter );
|
|
|
|
|
$im->sharpenImage( $radius, $sigma );
|
|
|
|
|
}
|
2014-05-09 05:51:59 +00:00
|
|
|
$qualityVal = isset( $params['quality'] ) ? (string) $params['quality'] : null;
|
|
|
|
|
$im->setCompressionQuality( $qualityVal ?: 80 );
|
2013-04-20 21:11:46 +00:00
|
|
|
} elseif ( $params['mimeType'] == 'image/png' ) {
|
2011-03-12 19:32:39 +00:00
|
|
|
$im->setCompressionQuality( 95 );
|
|
|
|
|
} elseif ( $params['mimeType'] == 'image/gif' ) {
|
2011-11-28 04:40:31 +00:00
|
|
|
if ( $this->getImageArea( $image ) > $wgMaxAnimatedGifArea ) {
|
2011-03-12 19:32:39 +00:00
|
|
|
// Extract initial frame only; we're so big it'll
|
|
|
|
|
// be a total drag. :P
|
|
|
|
|
$im->setImageScene( 0 );
|
|
|
|
|
} elseif ( $this->isAnimatedImage( $image ) ) {
|
|
|
|
|
// Coalesce is needed to scale animated GIFs properly (bug 1017).
|
|
|
|
|
$im = $im->coalesceImages();
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-28 20:18:32 +00:00
|
|
|
|
2011-03-12 19:32:39 +00:00
|
|
|
$rotation = $this->getRotation( $image );
|
2011-07-15 15:13:18 +00:00
|
|
|
list( $width, $height ) = $this->extractPreRotationDimensions( $params, $rotation );
|
2011-04-28 20:18:32 +00:00
|
|
|
|
2011-03-12 19:32:39 +00:00
|
|
|
$im->setImageBackgroundColor( new ImagickPixel( 'white' ) );
|
2011-04-28 20:18:32 +00:00
|
|
|
|
2011-03-12 19:32:39 +00:00
|
|
|
// Call Imagick::thumbnailImage on each frame
|
|
|
|
|
foreach ( $im as $i => $frame ) {
|
|
|
|
|
if ( !$frame->thumbnailImage( $width, $height, /* fit */ false ) ) {
|
|
|
|
|
return $this->getMediaTransformError( $params, "Error scaling frame $i" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$im->setImageDepth( 8 );
|
2011-04-28 20:18:32 +00:00
|
|
|
|
2011-03-12 19:32:39 +00:00
|
|
|
if ( $rotation ) {
|
2011-03-12 22:50:43 +00:00
|
|
|
if ( !$im->rotateImage( new ImagickPixel( 'white' ), 360 - $rotation ) ) {
|
2011-03-12 19:32:39 +00:00
|
|
|
return $this->getMediaTransformError( $params, "Error rotating $rotation degrees" );
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-28 20:18:32 +00:00
|
|
|
|
2011-03-12 19:32:39 +00:00
|
|
|
if ( $this->isAnimatedImage( $image ) ) {
|
|
|
|
|
wfDebug( __METHOD__ . ": Writing animated thumbnail\n" );
|
|
|
|
|
// This is broken somehow... can't find out how to fix it
|
|
|
|
|
$result = $im->writeImages( $params['dstPath'], true );
|
|
|
|
|
} else {
|
|
|
|
|
$result = $im->writeImage( $params['dstPath'] );
|
|
|
|
|
}
|
|
|
|
|
if ( !$result ) {
|
2011-04-28 20:18:32 +00:00
|
|
|
return $this->getMediaTransformError( $params,
|
2011-03-12 19:32:39 +00:00
|
|
|
"Unable to write thumbnail to {$params['dstPath']}" );
|
|
|
|
|
}
|
|
|
|
|
} catch ( ImagickException $e ) {
|
2011-04-28 20:18:32 +00:00
|
|
|
return $this->getMediaTransformError( $params, $e->getMessage() );
|
2011-03-12 19:32:39 +00:00
|
|
|
}
|
2011-04-28 20:18:32 +00:00
|
|
|
|
2011-03-12 19:32:39 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2010-10-30 19:11:30 +00:00
|
|
|
|
2010-10-30 19:04:26 +00:00
|
|
|
/**
|
|
|
|
|
* Transform an image using a custom command
|
2010-10-30 19:11:30 +00:00
|
|
|
*
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param File $image File associated with this thumbnail
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params Array with scaler params
|
2010-10-30 19:11:30 +00:00
|
|
|
*
|
2012-08-10 08:59:55 +00:00
|
|
|
* @return MediaTransformError Error object if error occurred, false (=no error) otherwise
|
2010-10-30 19:04:26 +00:00
|
|
|
*/
|
|
|
|
|
protected function transformCustom( $image, $params ) {
|
2010-10-30 19:11:30 +00:00
|
|
|
# Use a custom convert command
|
|
|
|
|
global $wgCustomConvertCommand;
|
|
|
|
|
|
|
|
|
|
# Variables: %s %d %w %h
|
|
|
|
|
$src = wfEscapeShellArg( $params['srcPath'] );
|
|
|
|
|
$dst = wfEscapeShellArg( $params['dstPath'] );
|
|
|
|
|
$cmd = $wgCustomConvertCommand;
|
|
|
|
|
$cmd = str_replace( '%s', $src, str_replace( '%d', $dst, $cmd ) ); # Filenames
|
2014-01-27 21:49:30 +00:00
|
|
|
$cmd = str_replace( '%h', wfEscapeShellArg( $params['physicalHeight'] ),
|
|
|
|
|
str_replace( '%w', wfEscapeShellArg( $params['physicalWidth'] ), $cmd ) ); # Size
|
2010-10-30 19:11:30 +00:00
|
|
|
wfDebug( __METHOD__ . ": Running custom convert command $cmd\n" );
|
|
|
|
|
wfProfileIn( 'convert' );
|
|
|
|
|
$retval = 0;
|
2013-09-12 01:40:56 +00:00
|
|
|
$err = wfShellExecWithStderr( $cmd, $retval );
|
2010-10-30 19:11:30 +00:00
|
|
|
wfProfileOut( 'convert' );
|
|
|
|
|
|
|
|
|
|
if ( $retval !== 0 ) {
|
|
|
|
|
$this->logErrorForExternalProcess( $retval, $err, $cmd );
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2010-10-30 19:11:30 +00:00
|
|
|
return $this->getMediaTransformError( $params, $err );
|
|
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2010-10-30 19:11:30 +00:00
|
|
|
return false; # No error
|
2010-10-30 19:04:26 +00:00
|
|
|
}
|
2010-10-30 19:11:30 +00:00
|
|
|
|
2010-10-30 19:04:26 +00:00
|
|
|
/**
|
2010-10-30 20:02:53 +00:00
|
|
|
* Get a MediaTransformError with error 'thumbnail_error'
|
2011-04-28 20:18:32 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params Parameter array as passed to the transform* functions
|
|
|
|
|
* @param string $errMsg Error message
|
2010-10-30 20:02:53 +00:00
|
|
|
* @return MediaTransformError
|
2010-10-30 19:04:26 +00:00
|
|
|
*/
|
2011-03-18 22:28:19 +00:00
|
|
|
public function getMediaTransformError( $params, $errMsg ) {
|
2010-10-30 19:11:30 +00:00
|
|
|
return new MediaTransformError( 'thumbnail_error', $params['clientWidth'],
|
2013-12-05 10:05:05 +00:00
|
|
|
$params['clientHeight'], $errMsg );
|
2010-10-30 19:04:26 +00:00
|
|
|
}
|
2010-10-30 19:11:30 +00:00
|
|
|
|
2010-10-30 19:04:26 +00:00
|
|
|
/**
|
|
|
|
|
* Transform an image using the built in GD library
|
2010-10-30 19:11:30 +00:00
|
|
|
*
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param File $image File associated with this thumbnail
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params Array with scaler params
|
2010-10-30 19:11:30 +00:00
|
|
|
*
|
2012-08-10 08:59:55 +00:00
|
|
|
* @return MediaTransformError Error object if error occurred, false (=no error) otherwise
|
2010-10-30 19:04:26 +00:00
|
|
|
*/
|
|
|
|
|
protected function transformGd( $image, $params ) {
|
2010-10-30 19:11:30 +00:00
|
|
|
# Use PHP's builtin GD library functions.
|
|
|
|
|
#
|
|
|
|
|
# First find out what kind of file this is, and select the correct
|
|
|
|
|
# input routine for this.
|
|
|
|
|
|
|
|
|
|
$typemap = array(
|
2014-05-09 05:51:59 +00:00
|
|
|
'image/gif' => array( 'imagecreatefromgif', 'palette', false, 'imagegif' ),
|
|
|
|
|
'image/jpeg' => array( 'imagecreatefromjpeg', 'truecolor', true,
|
2013-12-05 11:33:18 +00:00
|
|
|
array( __CLASS__, 'imageJpegWrapper' ) ),
|
2014-05-09 05:51:59 +00:00
|
|
|
'image/png' => array( 'imagecreatefrompng', 'bits', false, 'imagepng' ),
|
|
|
|
|
'image/vnd.wap.wbmp' => array( 'imagecreatefromwbmp', 'palette', false, 'imagewbmp' ),
|
|
|
|
|
'image/xbm' => array( 'imagecreatefromxbm', 'palette', false, 'imagexbm' ),
|
2010-10-30 19:11:30 +00:00
|
|
|
);
|
2014-05-09 05:51:59 +00:00
|
|
|
|
2010-10-30 19:11:30 +00:00
|
|
|
if ( !isset( $typemap[$params['mimeType']] ) ) {
|
|
|
|
|
$err = 'Image type not supported';
|
|
|
|
|
wfDebug( "$err\n" );
|
2012-08-19 23:05:20 +00:00
|
|
|
$errMsg = wfMessage( 'thumbnail_image-type' )->text();
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2010-10-30 19:11:30 +00:00
|
|
|
return $this->getMediaTransformError( $params, $errMsg );
|
|
|
|
|
}
|
2014-05-09 05:51:59 +00:00
|
|
|
list( $loader, $colorStyle, $useQuality, $saveType ) = $typemap[$params['mimeType']];
|
2007-04-20 12:31:36 +00:00
|
|
|
|
2010-10-30 19:11:30 +00:00
|
|
|
if ( !function_exists( $loader ) ) {
|
|
|
|
|
$err = "Incomplete GD library configuration: missing function $loader";
|
|
|
|
|
wfDebug( "$err\n" );
|
2012-08-19 23:05:20 +00:00
|
|
|
$errMsg = wfMessage( 'thumbnail_gd-library', $loader )->text();
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2010-10-30 19:11:30 +00:00
|
|
|
return $this->getMediaTransformError( $params, $errMsg );
|
|
|
|
|
}
|
2007-04-20 12:31:36 +00:00
|
|
|
|
2010-10-30 19:11:30 +00:00
|
|
|
if ( !file_exists( $params['srcPath'] ) ) {
|
|
|
|
|
$err = "File seems to be missing: {$params['srcPath']}";
|
|
|
|
|
wfDebug( "$err\n" );
|
2012-08-19 23:05:20 +00:00
|
|
|
$errMsg = wfMessage( 'thumbnail_image-missing', $params['srcPath'] )->text();
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2010-10-30 19:11:30 +00:00
|
|
|
return $this->getMediaTransformError( $params, $errMsg );
|
|
|
|
|
}
|
2009-04-13 15:05:47 +00:00
|
|
|
|
2010-10-30 19:11:30 +00:00
|
|
|
$src_image = call_user_func( $loader, $params['srcPath'] );
|
2011-04-28 20:18:32 +00:00
|
|
|
|
2011-03-02 19:42:30 +00:00
|
|
|
$rotation = function_exists( 'imagerotate' ) ? $this->getRotation( $image ) : 0;
|
2011-07-15 15:13:18 +00:00
|
|
|
list( $width, $height ) = $this->extractPreRotationDimensions( $params, $rotation );
|
2011-01-07 22:12:05 +00:00
|
|
|
$dst_image = imagecreatetruecolor( $width, $height );
|
2010-10-30 19:11:30 +00:00
|
|
|
|
|
|
|
|
// Initialise the destination image to transparent instead of
|
|
|
|
|
// the default solid black, to support PNG and GIF transparency nicely
|
|
|
|
|
$background = imagecolorallocate( $dst_image, 0, 0, 0 );
|
|
|
|
|
imagecolortransparent( $dst_image, $background );
|
|
|
|
|
imagealphablending( $dst_image, false );
|
|
|
|
|
|
|
|
|
|
if ( $colorStyle == 'palette' ) {
|
|
|
|
|
// Don't resample for paletted GIF images.
|
|
|
|
|
// It may just uglify them, and completely breaks transparency.
|
|
|
|
|
imagecopyresized( $dst_image, $src_image,
|
|
|
|
|
0, 0, 0, 0,
|
2011-01-07 22:12:05 +00:00
|
|
|
$width, $height,
|
2010-10-30 19:11:30 +00:00
|
|
|
imagesx( $src_image ), imagesy( $src_image ) );
|
|
|
|
|
} else {
|
|
|
|
|
imagecopyresampled( $dst_image, $src_image,
|
|
|
|
|
0, 0, 0, 0,
|
2011-01-07 22:12:05 +00:00
|
|
|
$width, $height,
|
2010-10-30 19:11:30 +00:00
|
|
|
imagesx( $src_image ), imagesy( $src_image ) );
|
|
|
|
|
}
|
2011-04-28 20:18:32 +00:00
|
|
|
|
2011-01-07 22:12:05 +00:00
|
|
|
if ( $rotation % 360 != 0 && $rotation % 90 == 0 ) {
|
|
|
|
|
$rot_image = imagerotate( $dst_image, $rotation, 0 );
|
|
|
|
|
imagedestroy( $dst_image );
|
|
|
|
|
$dst_image = $rot_image;
|
|
|
|
|
}
|
2011-04-28 20:18:32 +00:00
|
|
|
|
2010-10-30 19:11:30 +00:00
|
|
|
imagesavealpha( $dst_image, true );
|
2007-08-22 00:30:16 +00:00
|
|
|
|
2014-05-09 05:51:59 +00:00
|
|
|
$funcParams = array( $dst_image, $params['dstPath'] );
|
|
|
|
|
if ( $useQuality && isset( $params['quality'] ) ) {
|
|
|
|
|
$funcParams[] = $params['quality'];
|
|
|
|
|
}
|
|
|
|
|
call_user_func_array( $saveType, $funcParams );
|
|
|
|
|
|
2010-10-30 19:11:30 +00:00
|
|
|
imagedestroy( $dst_image );
|
|
|
|
|
imagedestroy( $src_image );
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-10-30 19:11:30 +00:00
|
|
|
return false; # No error
|
2007-04-20 12:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
2010-04-23 16:24:54 +00:00
|
|
|
/**
|
|
|
|
|
* Escape a string for ImageMagick's property input (e.g. -set -comment)
|
|
|
|
|
* See InterpretImageProperties() in magick/property.c
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param string $s
|
|
|
|
|
* @return string
|
2010-04-23 16:24:54 +00:00
|
|
|
*/
|
|
|
|
|
function escapeMagickProperty( $s ) {
|
|
|
|
|
// Double the backslashes
|
|
|
|
|
$s = str_replace( '\\', '\\\\', $s );
|
|
|
|
|
// Double the percents
|
|
|
|
|
$s = str_replace( '%', '%%', $s );
|
|
|
|
|
// Escape initial - or @
|
|
|
|
|
if ( strlen( $s ) > 0 && ( $s[0] === '-' || $s[0] === '@' ) ) {
|
|
|
|
|
$s = '\\' . $s;
|
|
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2010-04-23 16:24:54 +00:00
|
|
|
return $s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-10-30 19:11:30 +00:00
|
|
|
* Escape a string for ImageMagick's input filenames. See ExpandFilenames()
|
2010-04-23 16:24:54 +00:00
|
|
|
* and GetPathComponent() in magick/utility.c.
|
|
|
|
|
*
|
|
|
|
|
* This won't work with an initial ~ or @, so input files should be prefixed
|
2010-10-30 19:11:30 +00:00
|
|
|
* with the directory name.
|
2010-04-23 16:24:54 +00:00
|
|
|
*
|
|
|
|
|
* Glob character unescaping is broken in ImageMagick before 6.6.1-5, but
|
2010-10-30 19:11:30 +00:00
|
|
|
* it's broken in a way that doesn't involve trying to convert every file
|
2010-04-23 16:24:54 +00:00
|
|
|
* in a directory, so we're better off escaping and waiting for the bugfix
|
|
|
|
|
* to filter down to users.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $path The file path
|
2012-10-07 23:35:26 +00:00
|
|
|
* @param bool|string $scene The scene specification, or false if there is none
|
|
|
|
|
* @throws MWException
|
2012-02-09 21:35:05 +00:00
|
|
|
* @return string
|
2010-04-23 16:24:54 +00:00
|
|
|
*/
|
|
|
|
|
function escapeMagickInput( $path, $scene = false ) {
|
|
|
|
|
# Die on initial metacharacters (caller should prepend path)
|
|
|
|
|
$firstChar = substr( $path, 0, 1 );
|
|
|
|
|
if ( $firstChar === '~' || $firstChar === '@' ) {
|
2010-10-30 19:11:30 +00:00
|
|
|
throw new MWException( __METHOD__ . ': cannot escape this path name' );
|
2010-04-23 16:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Escape glob chars
|
|
|
|
|
$path = preg_replace( '/[*?\[\]{}]/', '\\\\\0', $path );
|
|
|
|
|
|
2013-03-08 12:07:32 +00:00
|
|
|
return $this->escapeMagickPath( $path, $scene );
|
2010-04-23 16:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-10-30 19:11:30 +00:00
|
|
|
* Escape a string for ImageMagick's output filename. See
|
2010-04-23 16:24:54 +00:00
|
|
|
* InterpretImageFilename() in magick/image.c.
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param string $path The file path
|
|
|
|
|
* @param bool|string $scene The scene specification, or false if there is none
|
2012-02-09 21:35:05 +00:00
|
|
|
* @return string
|
2010-04-23 16:24:54 +00:00
|
|
|
*/
|
|
|
|
|
function escapeMagickOutput( $path, $scene = false ) {
|
|
|
|
|
$path = str_replace( '%', '%%', $path );
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2013-03-08 12:07:32 +00:00
|
|
|
return $this->escapeMagickPath( $path, $scene );
|
2010-04-23 16:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-10-30 19:11:30 +00:00
|
|
|
* Armour a string against ImageMagick's GetPathComponent(). This is a
|
2010-04-23 16:24:54 +00:00
|
|
|
* helper function for escapeMagickInput() and escapeMagickOutput().
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $path The file path
|
2012-10-07 23:35:26 +00:00
|
|
|
* @param bool|string $scene The scene specification, or false if there is none
|
|
|
|
|
* @throws MWException
|
2012-02-09 21:35:05 +00:00
|
|
|
* @return string
|
2010-04-23 16:24:54 +00:00
|
|
|
*/
|
|
|
|
|
protected function escapeMagickPath( $path, $scene = false ) {
|
|
|
|
|
# Die on format specifiers (other than drive letters). The regex is
|
|
|
|
|
# meant to match all the formats you get from "convert -list format"
|
|
|
|
|
if ( preg_match( '/^([a-zA-Z0-9-]+):/', $path, $m ) ) {
|
|
|
|
|
if ( wfIsWindows() && is_dir( $m[0] ) ) {
|
|
|
|
|
// OK, it's a drive letter
|
|
|
|
|
// ImageMagick has a similar exception, see IsMagickConflict()
|
|
|
|
|
} else {
|
2010-10-30 19:11:30 +00:00
|
|
|
throw new MWException( __METHOD__ . ': unexpected colon character in path name' );
|
2010-04-23 16:24:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-30 19:11:30 +00:00
|
|
|
# If there are square brackets, add a do-nothing scene specification
|
2010-04-23 16:24:54 +00:00
|
|
|
# to force a literal interpretation
|
|
|
|
|
if ( $scene === false ) {
|
|
|
|
|
if ( strpos( $path, '[' ) !== false ) {
|
|
|
|
|
$path .= '[0--1]';
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$path .= "[$scene]";
|
|
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2010-04-23 16:24:54 +00:00
|
|
|
return $path;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-20 13:22:02 +00:00
|
|
|
/**
|
|
|
|
|
* Retrieve the version of the installed ImageMagick
|
|
|
|
|
* You can use PHPs version_compare() to use this value
|
|
|
|
|
* Value is cached for one hour.
|
2013-12-05 19:27:27 +00:00
|
|
|
* @return string Representing the IM version.
|
2010-08-20 13:22:02 +00:00
|
|
|
*/
|
|
|
|
|
protected function getMagickVersion() {
|
|
|
|
|
global $wgMemc;
|
|
|
|
|
|
|
|
|
|
$cache = $wgMemc->get( "imagemagick-version" );
|
2010-10-30 19:11:30 +00:00
|
|
|
if ( !$cache ) {
|
2010-08-20 13:22:02 +00:00
|
|
|
global $wgImageMagickConvertCommand;
|
|
|
|
|
$cmd = wfEscapeShellArg( $wgImageMagickConvertCommand ) . ' -version';
|
2010-10-30 19:11:30 +00:00
|
|
|
wfDebug( __METHOD__ . ": Running convert -version\n" );
|
2010-09-02 22:15:20 +00:00
|
|
|
$retval = '';
|
2010-09-03 16:00:58 +00:00
|
|
|
$return = wfShellExec( $cmd, $retval );
|
2010-10-30 19:11:30 +00:00
|
|
|
$x = preg_match( '/Version: ImageMagick ([0-9]*\.[0-9]*\.[0-9]*)/', $return, $matches );
|
|
|
|
|
if ( $x != 1 ) {
|
|
|
|
|
wfDebug( __METHOD__ . ": ImageMagick version check failed\n" );
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2010-08-20 13:22:02 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$wgMemc->set( "imagemagick-version", $matches[1], 3600 );
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2010-08-20 13:22:02 +00:00
|
|
|
return $matches[1];
|
|
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2010-08-20 13:22:02 +00:00
|
|
|
return $cache;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-09 05:51:59 +00:00
|
|
|
// FIXME: transformImageMagick() & transformImageMagickExt() uses JPEG quality 80, here it's 95?
|
|
|
|
|
static function imageJpegWrapper( $dst_image, $thumbPath, $quality = 95 ) {
|
2007-04-20 12:31:36 +00:00
|
|
|
imageinterlace( $dst_image );
|
2014-05-09 05:51:59 +00:00
|
|
|
imagejpeg( $dst_image, $thumbPath, $quality );
|
2007-04-20 12:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
2011-01-08 11:49:09 +00:00
|
|
|
/**
|
|
|
|
|
* Returns whether the current scaler supports rotation (im and gd do)
|
2011-04-28 20:18:32 +00:00
|
|
|
*
|
2011-01-08 11:49:09 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2011-01-22 22:34:36 +00:00
|
|
|
public static function canRotate() {
|
|
|
|
|
$scaler = self::getScalerType( null, false );
|
2011-03-02 19:42:30 +00:00
|
|
|
switch ( $scaler ) {
|
|
|
|
|
case 'im':
|
|
|
|
|
# ImageMagick supports autorotation
|
|
|
|
|
return true;
|
2011-03-12 19:32:39 +00:00
|
|
|
case 'imext':
|
|
|
|
|
# Imagick::rotateImage
|
|
|
|
|
return true;
|
2011-03-02 19:42:30 +00:00
|
|
|
case 'gd':
|
|
|
|
|
# GD's imagerotate function is used to rotate images, but not
|
|
|
|
|
# all precompiled PHP versions have that function
|
|
|
|
|
return function_exists( 'imagerotate' );
|
|
|
|
|
default:
|
|
|
|
|
# Other scalers don't support rotation
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-01-07 22:12:05 +00:00
|
|
|
}
|
2011-04-28 20:18:32 +00:00
|
|
|
|
2013-12-22 03:08:28 +00:00
|
|
|
/**
|
|
|
|
|
* @see $wgEnableAutoRotation
|
|
|
|
|
* @return bool Whether auto rotation is enabled
|
|
|
|
|
*/
|
|
|
|
|
public static function autoRotateEnabled() {
|
|
|
|
|
global $wgEnableAutoRotation;
|
|
|
|
|
|
|
|
|
|
if ( $wgEnableAutoRotation === null ) {
|
|
|
|
|
// Only enable auto-rotation when the bitmap handler can rotate
|
|
|
|
|
$wgEnableAutoRotation = BitmapHandler::canRotate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $wgEnableAutoRotation;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-08 11:35:55 +00:00
|
|
|
/**
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param File $file
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params Rotate parameters.
|
2013-12-05 19:27:27 +00:00
|
|
|
* 'rotation' clockwise rotation in degrees, allowed are multiples of 90
|
2013-01-08 11:35:55 +00:00
|
|
|
* @since 1.21
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2013-03-08 12:07:32 +00:00
|
|
|
public function rotate( $file, $params ) {
|
2013-01-08 11:35:55 +00:00
|
|
|
global $wgImageMagickConvertCommand;
|
|
|
|
|
|
2013-03-24 10:01:51 +00:00
|
|
|
$rotation = ( $params['rotation'] + $this->getRotation( $file ) ) % 360;
|
2013-01-08 11:35:55 +00:00
|
|
|
$scene = false;
|
|
|
|
|
|
|
|
|
|
$scaler = self::getScalerType( null, false );
|
|
|
|
|
switch ( $scaler ) {
|
|
|
|
|
case 'im':
|
|
|
|
|
$cmd = wfEscapeShellArg( $wgImageMagickConvertCommand ) . " " .
|
2013-03-24 10:01:51 +00:00
|
|
|
wfEscapeShellArg( $this->escapeMagickInput( $params['srcPath'], $scene ) ) .
|
2014-01-27 21:49:30 +00:00
|
|
|
" -rotate " . wfEscapeShellArg( "-$rotation" ) . " " .
|
2013-09-12 01:40:56 +00:00
|
|
|
wfEscapeShellArg( $this->escapeMagickOutput( $params['dstPath'] ) );
|
2013-01-08 11:35:55 +00:00
|
|
|
wfDebug( __METHOD__ . ": running ImageMagick: $cmd\n" );
|
|
|
|
|
wfProfileIn( 'convert' );
|
|
|
|
|
$retval = 0;
|
2014-02-20 03:47:25 +00:00
|
|
|
$err = wfShellExecWithStderr( $cmd, $retval );
|
2013-01-08 11:35:55 +00:00
|
|
|
wfProfileOut( 'convert' );
|
|
|
|
|
if ( $retval !== 0 ) {
|
2013-03-08 12:07:32 +00:00
|
|
|
$this->logErrorForExternalProcess( $retval, $err, $cmd );
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2013-01-08 11:35:55 +00:00
|
|
|
return new MediaTransformError( 'thumbnail_error', 0, 0, $err );
|
|
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2013-01-08 11:35:55 +00:00
|
|
|
return false;
|
|
|
|
|
case 'imext':
|
|
|
|
|
$im = new Imagick();
|
|
|
|
|
$im->readImage( $params['srcPath'] );
|
|
|
|
|
if ( !$im->rotateImage( new ImagickPixel( 'white' ), 360 - $rotation ) ) {
|
|
|
|
|
return new MediaTransformError( 'thumbnail_error', 0, 0,
|
|
|
|
|
"Error rotating $rotation degrees" );
|
|
|
|
|
}
|
|
|
|
|
$result = $im->writeImage( $params['dstPath'] );
|
|
|
|
|
if ( !$result ) {
|
|
|
|
|
return new MediaTransformError( 'thumbnail_error', 0, 0,
|
|
|
|
|
"Unable to write image to {$params['dstPath']}" );
|
|
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2013-01-08 11:35:55 +00:00
|
|
|
return false;
|
|
|
|
|
default:
|
|
|
|
|
return new MediaTransformError( 'thumbnail_error', 0, 0,
|
|
|
|
|
"$scaler rotation not implemented" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-08 11:49:09 +00:00
|
|
|
/**
|
2014-06-03 16:09:30 +00:00
|
|
|
* Returns whether the file needs to be rendered. Returns true if the
|
2011-01-08 11:49:09 +00:00
|
|
|
* file requires rotation and we are able to rotate it.
|
2011-04-28 20:18:32 +00:00
|
|
|
*
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param File $file
|
2011-01-08 11:49:09 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2011-01-07 22:12:05 +00:00
|
|
|
public function mustRender( $file ) {
|
2011-01-22 22:34:36 +00:00
|
|
|
return self::canRotate() && $this->getRotation( $file ) != 0;
|
2011-01-07 22:12:05 +00:00
|
|
|
}
|
2007-04-20 12:31:36 +00:00
|
|
|
}
|