1<?php
2/**
3 * Fake handler for images.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Media
22 */
23
24/**
25 * Mock handler for images.
26 *
27 * This is really intended for unit testing.
28 *
29 * @ingroup Media
30 */
31class MockImageHandler {
32
33	/**
34	 * Override BitmapHandler::doTransform() making sure we do not generate
35	 * a thumbnail at all. That is merely returning a ThumbnailImage that
36	 * will be consumed by the unit test.  There is no need to create a real
37	 * thumbnail on the filesystem.
38	 * @param ImageHandler $that
39	 * @param File $image
40	 * @param string $dstPath
41	 * @param string $dstUrl
42	 * @param array $params
43	 * @param int $flags
44	 * @return ThumbnailImage
45	 */
46	public static function doFakeTransform( $that, $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
47		# Example of what we receive:
48		# $image: LocalFile
49		# $dstPath: /tmp/transform_7d0a7a2f1a09-1.jpg
50		# $dstUrl : http://example.com/images/thumb/0/09/Bad.jpg/320px-Bad.jpg
51		# $params:  width: 320,  descriptionUrl http://trunk.dev/wiki/File:Bad.jpg
52
53		$that->normaliseParams( $image, $params );
54
55		$scalerParams = [
56			# The size to which the image will be resized
57			'physicalWidth' => $params['physicalWidth'],
58			'physicalHeight' => $params['physicalHeight'],
59			'physicalDimensions' => "{$params['physicalWidth']}x{$params['physicalHeight']}",
60			# The size of the image on the page
61			'clientWidth' => $params['width'],
62			'clientHeight' => $params['height'],
63			# Comment as will be added to the EXIF of the thumbnail
64			'comment' => isset( $params['descriptionUrl'] ) ?
65					"File source: {$params['descriptionUrl']}" : '',
66			# Properties of the original image
67			'srcWidth' => $image->getWidth(),
68			'srcHeight' => $image->getHeight(),
69			'mimeType' => $image->getMimeType(),
70			'dstPath' => $dstPath,
71			'dstUrl' => $dstUrl,
72		];
73
74		# In some cases, we do not bother generating a thumbnail.
75		if ( !$image->mustRender() &&
76			$scalerParams['physicalWidth'] == $scalerParams['srcWidth']
77			&& $scalerParams['physicalHeight'] == $scalerParams['srcHeight']
78		) {
79			wfDebug( __METHOD__ . ": returning unscaled image" );
80			// getClientScalingThumbnailImage is protected
81			return $that->doClientImage( $image, $scalerParams );
82		}
83
84		return new ThumbnailImage( $image, $dstUrl, false, $params );
85	}
86}
87