Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
77.78% covered (warning)
77.78%
14 / 18
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 * @license GPL-2.0-or-later
6 * @file
7 * @ingroup Media
8 */
9
10namespace MediaWiki\Media;
11
12use MediaWiki\FileRepo\File\File;
13use Wikimedia\StringUtils\StringUtils;
14use Wikimedia\UnpackFailedException;
15
16/**
17 * Handler for Microsoft's bitmap format; getimagesize() doesn't
18 * support these files
19 *
20 * @ingroup Media
21 */
22class BmpHandler extends BitmapHandler {
23    /**
24     * @param File $file
25     * @return bool
26     */
27    public function mustRender( $file ) {
28        return true;
29    }
30
31    /**
32     * Render files as PNG
33     *
34     * @param string $ext
35     * @param string $mime
36     * @param array|null $params
37     * @return array
38     */
39    public function getThumbType( $ext, $mime, $params = null ) {
40        return [ 'png', 'image/png' ];
41    }
42
43    /**
44     * Get width and height from the bmp header.
45     *
46     * @param MediaHandlerState $state
47     * @param string $filename
48     * @return array
49     */
50    public function getSizeAndMetadata( $state, $filename ) {
51        $f = fopen( $filename, 'rb' );
52        if ( !$f ) {
53            return [];
54        }
55        $header = fread( $f, 54 );
56        fclose( $f );
57
58        // Extract binary form of width and height from the header
59        $w = substr( $header, 18, 4 );
60        $h = substr( $header, 22, 4 );
61
62        // Convert the unsigned long 32 bits (little endian):
63        try {
64            $w = StringUtils::unpack( 'V', $w, 4 );
65            $h = StringUtils::unpack( 'V', $h, 4 );
66        } catch ( UnpackFailedException ) {
67            return [];
68        }
69
70        return [
71            'width' => $w[1],
72            'height' => $h[1]
73        ];
74    }
75}
76
77/** @deprecated class alias since 1.46 */
78class_alias( BmpHandler::class, 'BmpHandler' );