Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
1.04% covered (danger)
1.04%
1 / 96
6.25% covered (danger)
6.25%
1 / 16
CRAP
0.00% covered (danger)
0.00%
0 / 1
ImageHandler
1.04% covered (danger)
1.04%
1 / 96
6.25% covered (danger)
6.25%
1 / 16
1920.13
0.00% covered (danger)
0.00%
0 / 1
 canRender
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 getParamMap
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 validateParam
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 makeParamString
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 parseParamString
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getScriptParams
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 normaliseParams
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
182
 validateThumbParams
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
 getScriptedTransform
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 getImageSize
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getSizeAndMetadata
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 getImageArea
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getShortDesc
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getLongDesc
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 getDimensionsString
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 sanitizeParamsForBucketing
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Media-handling base classes and generic functionality.
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
24use MediaWiki\FileRepo\File\File;
25use Wikimedia\AtEase\AtEase;
26
27/**
28 * Media handler abstract base class for images
29 *
30 * @stable to extend
31 *
32 * @ingroup Media
33 */
34abstract class ImageHandler extends MediaHandler {
35    /**
36     * @inheritDoc
37     * @stable to override
38     * @param File $file
39     * @return bool
40     */
41    public function canRender( $file ) {
42        return ( $file->getWidth() && $file->getHeight() );
43    }
44
45    /**
46     * @inheritDoc
47     * @stable to override
48     * @return string[]
49     */
50    public function getParamMap() {
51        return [ 'img_width' => 'width' ];
52    }
53
54    /**
55     * @inheritDoc
56     * @stable to override
57     */
58    public function validateParam( $name, $value ) {
59        return in_array( $name, [ 'width', 'height' ] ) && $value > 0;
60    }
61
62    /**
63     * @inheritDoc
64     * @stable to override
65     * @throws MediaTransformInvalidParametersException
66     */
67    public function makeParamString( $params ) {
68        if ( isset( $params['physicalWidth'] ) ) {
69            $width = $params['physicalWidth'];
70        } elseif ( isset( $params['width'] ) ) {
71            $width = $params['width'];
72        } else {
73            throw new MediaTransformInvalidParametersException( 'No width specified to ' . __METHOD__ );
74        }
75
76        # Removed for ProofreadPage
77        # $width = intval( $width );
78        return "{$width}px";
79    }
80
81    /**
82     * @inheritDoc
83     * @stable to override
84     */
85    public function parseParamString( $str ) {
86        $m = false;
87        if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
88            return [ 'width' => $m[1] ];
89        }
90        return false;
91    }
92
93    /**
94     * @stable to override
95     * @param array $params
96     * @return array
97     */
98    protected function getScriptParams( $params ) {
99        return [ 'width' => $params['width'] ];
100    }
101
102    /**
103     * @inheritDoc
104     * @stable to override
105     * @param File $image
106     * @param array &$params @phan-ignore-reference
107     * @return bool
108     * @phan-assert array{width:int,physicalWidth:int,height:int,physicalHeight:int,page:int} $params
109     */
110    public function normaliseParams( $image, &$params ) {
111        if ( !isset( $params['width'] ) ) {
112            return false;
113        }
114
115        if ( !isset( $params['page'] ) ) {
116            $params['page'] = 1;
117        } else {
118            $params['page'] = (int)$params['page'];
119            if ( $params['page'] > $image->pageCount() ) {
120                $params['page'] = $image->pageCount();
121            }
122
123            if ( $params['page'] < 1 ) {
124                $params['page'] = 1;
125            }
126        }
127
128        $srcWidth = $image->getWidth( $params['page'] );
129        $srcHeight = $image->getHeight( $params['page'] );
130
131        if ( isset( $params['height'] ) && $params['height'] !== -1 ) {
132            # Height & width were both set
133            if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
134                # Height is the relative smaller dimension, so scale width accordingly
135                $params['width'] = self::fitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
136
137                if ( $params['width'] == 0 ) {
138                    # Very small image, so we need to rely on client side scaling :(
139                    $params['width'] = 1;
140                }
141
142                $params['physicalWidth'] = $params['width'];
143            } else {
144                # Height was crap, unset it so that it will be calculated later
145                unset( $params['height'] );
146            }
147        }
148
149        if ( !isset( $params['physicalWidth'] ) ) {
150            # Passed all validations, so set the physicalWidth
151            // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive, checked above
152            $params['physicalWidth'] = $params['width'];
153        }
154
155        # Because thumbs are only referred to by width, the height always needs
156        # to be scaled by the width to keep the thumbnail sizes consistent,
157        # even if it was set inside the if block above
158        $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight,
159            $params['physicalWidth'] );
160
161        # Set the height if it was not validated in the if block higher up
162        if ( !isset( $params['height'] ) || $params['height'] === -1 ) {
163            $params['height'] = $params['physicalHeight'];
164        }
165
166        if ( !$this->validateThumbParams( $params['physicalWidth'],
167            $params['physicalHeight'], $srcWidth, $srcHeight )
168        ) {
169            return false;
170        }
171
172        return true;
173    }
174
175    /**
176     * Validate thumbnail parameters and fill in the correct height
177     *
178     * @param int &$width Specified width (input/output)
179     * @param int &$height Height (output only)
180     * @param int $srcWidth Width of the source image
181     * @param int $srcHeight Height of the source image
182     * @return bool False to indicate that an error should be returned to the user.
183     */
184    private function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight ) {
185        $width = (int)$width;
186
187        if ( $width <= 0 ) {
188            wfDebug( __METHOD__ . ": Invalid destination width: $width" );
189
190            return false;
191        }
192        if ( $srcWidth <= 0 ) {
193            wfDebug( __METHOD__ . ": Invalid source width: $srcWidth" );
194
195            return false;
196        }
197
198        $height = File::scaleHeight( $srcWidth, $srcHeight, $width );
199        if ( $height == 0 ) {
200            # Force height to be at least 1 pixel
201            $height = 1;
202        }
203
204        return true;
205    }
206
207    /**
208     * @inheritDoc
209     * @stable to override
210     * @param File $image
211     * @param string $script
212     * @param array $params
213     * @return MediaTransformOutput|false
214     */
215    public function getScriptedTransform( $image, $script, $params ) {
216        if ( !$this->normaliseParams( $image, $params ) ) {
217            return false;
218        }
219        $url = wfAppendQuery( $script, $this->getScriptParams( $params ) );
220
221        if ( $image->mustRender() || $params['width'] < $image->getWidth() ) {
222            return new ThumbnailImage( $image, $url, false, $params );
223        }
224    }
225
226    public function getImageSize( $image, $path ) {
227        AtEase::suppressWarnings();
228        $gis = getimagesize( $path );
229        AtEase::restoreWarnings();
230
231        return $gis;
232    }
233
234    public function getSizeAndMetadata( $state, $path ) {
235        // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
236        $gis = @getimagesize( $path );
237        if ( $gis ) {
238            $info = [
239                'width' => $gis[0],
240                'height' => $gis[1],
241            ];
242            if ( isset( $gis['bits'] ) ) {
243                $info['bits'] = $gis['bits'];
244            }
245        } else {
246            $info = [];
247        }
248        return $info;
249    }
250
251    /**
252     * Function that returns the number of pixels to be thumbnailed.
253     * Intended for animated GIFs to multiply by the number of frames.
254     *
255     * If the file doesn't support a notion of "area" return 0.
256     * @stable to override
257     *
258     * @param File $image
259     * @return int
260     */
261    public function getImageArea( $image ) {
262        return $image->getWidth() * $image->getHeight();
263    }
264
265    /**
266     * @inheritDoc
267     * @stable to override
268     * @param File $file
269     * @return string
270     */
271    public function getShortDesc( $file ) {
272        global $wgLang;
273        $nbytes = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
274        $widthheight = wfMessage( 'widthheight' )
275            ->numParams( $file->getWidth(), $file->getHeight() )->escaped();
276
277        return "$widthheight ($nbytes)";
278    }
279
280    /**
281     * @inheritDoc
282     * @stable to override
283     * @param File $file
284     * @return string
285     */
286    public function getLongDesc( $file ) {
287        $pages = $file->pageCount();
288        if ( $pages === false || $pages <= 1 ) {
289            $msg = wfMessage( 'file-info-size' )->numParams( $file->getWidth(),
290                $file->getHeight() )->sizeParams( $file->getSize() )->params(
291                    '<span class="mime-type">' . $file->getMimeType() . '</span>' )->parse();
292        } else {
293            $msg = wfMessage( 'file-info-size-pages' )->numParams( $file->getWidth(),
294                $file->getHeight() )->sizeParams( $file->getSize() )->params(
295                    '<span class="mime-type">' . $file->getMimeType() . '</span>' )->numParams( $pages )->parse();
296        }
297
298        return $msg;
299    }
300
301    /**
302     * @inheritDoc
303     * @stable to override
304     * @param File $file
305     * @return string
306     */
307    public function getDimensionsString( $file ) {
308        $pages = $file->pageCount();
309        if ( $pages > 1 ) {
310            return wfMessage( 'widthheightpage' )
311                ->numParams( $file->getWidth(), $file->getHeight(), $pages )->text();
312        }
313        return wfMessage( 'widthheight' )
314            ->numParams( $file->getWidth(), $file->getHeight() )->text();
315    }
316
317    /**
318     * @inheritDoc
319     * @stable to override
320     */
321    public function sanitizeParamsForBucketing( $params ) {
322        $params = parent::sanitizeParamsForBucketing( $params );
323
324        // We unset the height parameters in order to let normaliseParams recalculate them
325        // Otherwise there might be a height discrepancy
326        unset( $params['height'] );
327        unset( $params['physicalHeight'] );
328
329        return $params;
330    }
331}