1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
<?php
/**
* @file
* @ingroup Media
*/
/**
* @ingroup Media
*/
class BitmapHandler extends ImageHandler {
function normaliseParams( $image, &$params ) {
global $wgMaxImageArea;
if ( !parent::normaliseParams( $image, $params ) ) {
return false;
}
$mimeType = $image->getMimeType();
$srcWidth = $image->getWidth( $params['page'] );
$srcHeight = $image->getHeight( $params['page'] );
# Don't thumbnail an image so big that it will fill hard drives and send servers into swap
# JPEG has the handy property of allowing thumbnailing without full decompression, so we make
# an exception for it.
if ( $mimeType !== 'image/jpeg' &&
$this->getImageArea( $image, $srcWidth, $srcHeight ) > $wgMaxImageArea )
{
return false;
}
# Don't make an image bigger than the source
$params['physicalWidth'] = $params['width'];
$params['physicalHeight'] = $params['height'];
if ( $params['physicalWidth'] >= $srcWidth ) {
$params['physicalWidth'] = $srcWidth;
$params['physicalHeight'] = $srcHeight;
return true;
}
return true;
}
// Function that returns the number of pixels to be thumbnailed.
// Intended for animated GIFs to multiply by the number of frames.
function getImageArea( $image, $width, $height ) {
return $width * $height;
}
function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
global $wgUseImageMagick, $wgImageMagickConvertCommand, $wgImageMagickTempDir;
global $wgCustomConvertCommand, $wgUseImageResize;
global $wgSharpenParameter, $wgSharpenReductionThreshold;
global $wgMaxAnimatedGifArea;
if ( !$this->normaliseParams( $image, $params ) ) {
return new TransformParameterError( $params );
}
$physicalWidth = $params['physicalWidth'];
$physicalHeight = $params['physicalHeight'];
$clientWidth = $params['width'];
$clientHeight = $params['height'];
$comment = isset( $params['descriptionUrl'] ) ? "File source: ". $params['descriptionUrl'] : '';
$srcWidth = $image->getWidth();
$srcHeight = $image->getHeight();
$mimeType = $image->getMimeType();
$srcPath = $image->getPath();
$retval = 0;
wfDebug( __METHOD__.": creating {$physicalWidth}x{$physicalHeight} thumbnail at $dstPath\n" );
if ( !$image->mustRender() && $physicalWidth == $srcWidth && $physicalHeight == $srcHeight ) {
# normaliseParams (or the user) wants us to return the unscaled image
wfDebug( __METHOD__.": returning unscaled image\n" );
return new ThumbnailImage( $image, $image->getURL(), $clientWidth, $clientHeight, $srcPath );
}
if ( !$dstPath ) {
// No output path available, client side scaling only
$scaler = 'client';
} elseif( !$wgUseImageResize ) {
$scaler = 'client';
} elseif ( $wgUseImageMagick ) {
$scaler = 'im';
} elseif ( $wgCustomConvertCommand ) {
$scaler = 'custom';
} elseif ( function_exists( 'imagecreatetruecolor' ) ) {
$scaler = 'gd';
} else {
$scaler = 'client';
}
wfDebug( __METHOD__.": scaler $scaler\n" );
if ( $scaler == 'client' ) {
# Client-side image scaling, use the source URL
# Using the destination URL in a TRANSFORM_LATER request would be incorrect
return new ThumbnailImage( $image, $image->getURL(), $clientWidth, $clientHeight, $srcPath );
}
if ( $flags & self::TRANSFORM_LATER ) {
wfDebug( __METHOD__.": Transforming later per flags.\n" );
return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
}
if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
wfDebug( __METHOD__.": Unable to create thumbnail destination directory, falling back to client scaling\n" );
return new ThumbnailImage( $image, $image->getURL(), $clientWidth, $clientHeight, $srcPath );
}
if ( $scaler == 'im' ) {
# use ImageMagick
$quality = '';
$sharpen = '';
$scene = false;
$animation = '';
if ( $mimeType == 'image/jpeg' ) {
$quality = "-quality 80"; // 80%
# Sharpening, see bug 6193
if ( ( $physicalWidth + $physicalHeight ) / ( $srcWidth + $srcHeight ) < $wgSharpenReductionThreshold ) {
$sharpen = "-sharpen " . wfEscapeShellArg( $wgSharpenParameter );
}
} elseif ( $mimeType == 'image/png' ) {
$quality = "-quality 95"; // zlib 9, adaptive filtering
} elseif( $mimeType == 'image/gif' ) {
if( $srcWidth * $srcHeight > $wgMaxAnimatedGifArea ) {
// Extract initial frame only; we're so big it'll
// be a total drag. :P
$scene = 0;
} else {
// Coalesce is needed to scale animated GIFs properly (bug 1017).
$animation = ' -coalesce ';
}
}
if ( strval( $wgImageMagickTempDir ) !== '' ) {
$tempEnv = 'MAGICK_TMPDIR=' . wfEscapeShellArg( $wgImageMagickTempDir ) . ' ';
} else {
$tempEnv = '';
}
# Specify white background color, will be used for transparent images
# in Internet Explorer/Windows instead of default black.
# Note, we specify "-size {$physicalWidth}" and NOT "-size {$physicalWidth}x{$physicalHeight}".
# It seems that ImageMagick has a bug wherein it produces thumbnails of
# the wrong size in the second case.
$cmd =
$tempEnv .
wfEscapeShellArg( $wgImageMagickConvertCommand ) .
" {$quality} -background white -size {$physicalWidth} ".
wfEscapeShellArg( $this->escapeMagickInput( $srcPath, $scene ) ) .
$animation .
// For the -resize option a "!" is needed to force exact size,
// or ImageMagick may decide your ratio is wrong and slice off
// a pixel.
" -thumbnail " . wfEscapeShellArg( "{$physicalWidth}x{$physicalHeight}!" ) .
// Add the source url as a comment to the thumb.
" -set comment " . wfEscapeShellArg( $this->escapeMagickProperty( $comment ) ) .
" -depth 8 $sharpen " .
wfEscapeShellArg( $this->escapeMagickOutput( $dstPath ) ) . " 2>&1";
wfDebug( __METHOD__.": running ImageMagick: $cmd\n" );
wfProfileIn( 'convert' );
$err = wfShellExec( $cmd, $retval );
wfProfileOut( 'convert' );
} elseif( $scaler == 'custom' ) {
# Use a custom convert command
# Variables: %s %d %w %h
$src = wfEscapeShellArg( $srcPath );
$dst = wfEscapeShellArg( $dstPath );
$cmd = $wgCustomConvertCommand;
$cmd = str_replace( '%s', $src, str_replace( '%d', $dst, $cmd ) ); # Filenames
$cmd = str_replace( '%h', $physicalHeight, str_replace( '%w', $physicalWidth, $cmd ) ); # Size
wfDebug( __METHOD__.": Running custom convert command $cmd\n" );
wfProfileIn( 'convert' );
$err = wfShellExec( $cmd, $retval );
wfProfileOut( 'convert' );
} else /* $scaler == 'gd' */ {
# 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(
'image/gif' => array( 'imagecreatefromgif', 'palette', 'imagegif' ),
'image/jpeg' => array( 'imagecreatefromjpeg', 'truecolor', array( __CLASS__, 'imageJpegWrapper' ) ),
'image/png' => array( 'imagecreatefrompng', 'bits', 'imagepng' ),
'image/vnd.wap.wbmp' => array( 'imagecreatefromwbmp', 'palette', 'imagewbmp' ),
'image/xbm' => array( 'imagecreatefromxbm', 'palette', 'imagexbm' ),
);
if( !isset( $typemap[$mimeType] ) ) {
$err = 'Image type not supported';
wfDebug( "$err\n" );
$errMsg = wfMsg ( 'thumbnail_image-type' );
return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $errMsg );
}
list( $loader, $colorStyle, $saveType ) = $typemap[$mimeType];
if( !function_exists( $loader ) ) {
$err = "Incomplete GD library configuration: missing function $loader";
wfDebug( "$err\n" );
$errMsg = wfMsg ( 'thumbnail_gd-library', $loader );
return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $errMsg );
}
if ( !file_exists( $srcPath ) ) {
$err = "File seems to be missing: $srcPath";
wfDebug( "$err\n" );
$errMsg = wfMsg ( 'thumbnail_image-missing', $srcPath );
return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $errMsg );
}
$src_image = call_user_func( $loader, $srcPath );
$dst_image = imagecreatetruecolor( $physicalWidth, $physicalHeight );
// 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,
$physicalWidth, $physicalHeight, imagesx( $src_image ), imagesy( $src_image ) );
} else {
imagecopyresampled( $dst_image, $src_image,
0,0,0,0,
$physicalWidth, $physicalHeight, imagesx( $src_image ), imagesy( $src_image ) );
}
imagesavealpha( $dst_image, true );
call_user_func( $saveType, $dst_image, $dstPath );
imagedestroy( $dst_image );
imagedestroy( $src_image );
$retval = 0;
}
$removed = $this->removeBadFile( $dstPath, $retval );
if ( $retval != 0 || $removed ) {
wfDebugLog( 'thumbnail',
sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
wfHostname(), $retval, trim($err), $cmd ) );
return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
} else {
return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
}
}
/**
* Escape a string for ImageMagick's property input (e.g. -set -comment)
* See InterpretImageProperties() in magick/property.c
*/
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;
}
return $s;
}
/**
* Escape a string for ImageMagick's input filenames. See ExpandFilenames()
* and GetPathComponent() in magick/utility.c.
*
* This won't work with an initial ~ or @, so input files should be prefixed
* with the directory name.
*
* Glob character unescaping is broken in ImageMagick before 6.6.1-5, but
* it's broken in a way that doesn't involve trying to convert every file
* in a directory, so we're better off escaping and waiting for the bugfix
* to filter down to users.
*
* @param $path string The file path
* @param $scene string The scene specification, or false if there is none
*/
function escapeMagickInput( $path, $scene = false ) {
# Die on initial metacharacters (caller should prepend path)
$firstChar = substr( $path, 0, 1 );
if ( $firstChar === '~' || $firstChar === '@' ) {
throw new MWException( __METHOD__.': cannot escape this path name' );
}
# Escape glob chars
$path = preg_replace( '/[*?\[\]{}]/', '\\\\\0', $path );
return $this->escapeMagickPath( $path, $scene );
}
/**
* Escape a string for ImageMagick's output filename. See
* InterpretImageFilename() in magick/image.c.
*/
function escapeMagickOutput( $path, $scene = false ) {
$path = str_replace( '%', '%%', $path );
return $this->escapeMagickPath( $path, $scene );
}
/**
* Armour a string against ImageMagick's GetPathComponent(). This is a
* helper function for escapeMagickInput() and escapeMagickOutput().
*
* @param $path string The file path
* @param $scene string The scene specification, or false if there is none
*/
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 {
throw new MWException( __METHOD__.': unexpected colon character in path name' );
}
}
# If there are square brackets, add a do-nothing scene specification
# to force a literal interpretation
if ( $scene === false ) {
if ( strpos( $path, '[' ) !== false ) {
$path .= '[0--1]';
}
} else {
$path .= "[$scene]";
}
return $path;
}
static function imageJpegWrapper( $dst_image, $thumbPath ) {
imageinterlace( $dst_image );
imagejpeg( $dst_image, $thumbPath, 95 );
}
function getMetadata( $image, $filename ) {
global $wgShowEXIF;
if( $wgShowEXIF && file_exists( $filename ) ) {
$exif = new Exif( $filename );
$data = $exif->getFilteredData();
if ( $data ) {
$data['MEDIAWIKI_EXIF_VERSION'] = Exif::version();
return serialize( $data );
} else {
return '0';
}
} else {
return '';
}
}
function getMetadataType( $image ) {
return 'exif';
}
function isMetadataValid( $image, $metadata ) {
global $wgShowEXIF;
if ( !$wgShowEXIF ) {
# Metadata disabled and so an empty field is expected
return true;
}
if ( $metadata === '0' ) {
# Special value indicating that there is no EXIF data in the file
return true;
}
$exif = @unserialize( $metadata );
if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) ||
$exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version() )
{
# Wrong version
wfDebug( __METHOD__.": wrong version\n" );
return false;
}
return true;
}
/**
* Get a list of EXIF metadata items which should be displayed when
* the metadata table is collapsed.
*
* @return array of strings
* @access private
*/
function visibleMetadataFields() {
$fields = array();
$lines = explode( "\n", wfMsgForContent( 'metadata-fields' ) );
foreach( $lines as $line ) {
$matches = array();
if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
$fields[] = $matches[1];
}
}
$fields = array_map( 'strtolower', $fields );
return $fields;
}
function formatMetadata( $image ) {
$result = array(
'visible' => array(),
'collapsed' => array()
);
$metadata = $image->getMetadata();
if ( !$metadata ) {
return false;
}
$exif = unserialize( $metadata );
if ( !$exif ) {
return false;
}
unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
$format = new FormatExif( $exif );
$formatted = $format->getFormattedData();
// Sort fields into visible and collapsed
$visibleFields = $this->visibleMetadataFields();
foreach ( $formatted as $name => $value ) {
$tag = strtolower( $name );
self::addMeta( $result,
in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed',
'exif',
$tag,
$value
);
}
return $result;
}
}
|