Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
82.35% covered (warning)
82.35%
14 / 17
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
BmpHandler
82.35% covered (warning)
82.35%
14 / 17
66.67% covered (warning)
66.67%
2 / 3
5.14
0.00% covered (danger)
0.00%
0 / 1
 mustRender
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getThumbType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSizeAndMetadata
80.00% covered (warning)
80.00%
12 / 15
0.00% covered (danger)
0.00%
0 / 1
3.07
1<?php
2/**
3 * Handler for Microsoft's bitmap format.
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 MediaWiki\Libs\UnpackFailedException;
26
27/**
28 * Handler for Microsoft's bitmap format; getimagesize() doesn't
29 * support these files
30 *
31 * @ingroup Media
32 */
33class BmpHandler extends BitmapHandler {
34    /**
35     * @param File $file
36     * @return bool
37     */
38    public function mustRender( $file ) {
39        return true;
40    }
41
42    /**
43     * Render files as PNG
44     *
45     * @param string $ext
46     * @param string $mime
47     * @param array|null $params
48     * @return array
49     */
50    public function getThumbType( $ext, $mime, $params = null ) {
51        return [ 'png', 'image/png' ];
52    }
53
54    /**
55     * Get width and height from the bmp header.
56     *
57     * @param MediaHandlerState $state
58     * @param string $filename
59     * @return array
60     */
61    public function getSizeAndMetadata( $state, $filename ) {
62        $f = fopen( $filename, 'rb' );
63        if ( !$f ) {
64            return [];
65        }
66        $header = fread( $f, 54 );
67        fclose( $f );
68
69        // Extract binary form of width and height from the header
70        $w = substr( $header, 18, 4 );
71        $h = substr( $header, 22, 4 );
72
73        // Convert the unsigned long 32 bits (little endian):
74        try {
75            $w = StringUtils::unpack( 'V', $w, 4 );
76            $h = StringUtils::unpack( 'V', $h, 4 );
77        } catch ( UnpackFailedException $e ) {
78            return [];
79        }
80
81        return [
82            'width' => $w[1],
83            'height' => $h[1]
84        ];
85    }
86}