• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPOffice / PhpSpreadsheet / 18997969069

01 Nov 2025 02:15PM UTC coverage: 95.947%. Remained the same
18997969069

Pull #4701

github

web-flow
Merge e88eaa6d9 into 4db18eadd
Pull Request #4701: Upgrade Phpstan

19 of 19 new or added lines in 4 files covered. (100.0%)

1 existing line in 1 file now uncovered.

45360 of 47276 relevant lines covered (95.95%)

372.31 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.5
/src/PhpSpreadsheet/Shared/Drawing.php
1
<?php
2

3
namespace PhpOffice\PhpSpreadsheet\Shared;
4

5
use SimpleXMLElement;
6

7
class Drawing
8
{
9
    /**
10
     * Convert pixels to EMU.
11
     *
12
     * @param int $pixelValue Value in pixels
13
     *
14
     * @return float|int Value in EMU
15
     */
16
    public static function pixelsToEMU(int $pixelValue): int|float
120✔
17
    {
18
        return $pixelValue * 9525;
120✔
19
    }
20

21
    /**
22
     * Convert EMU to pixels.
23
     *
24
     * @param int|SimpleXMLElement $emuValue Value in EMU
25
     *
26
     * @return int Value in pixels
27
     */
28
    public static function EMUToPixels($emuValue): int
107✔
29
    {
30
        $emuValue = (int) $emuValue;
107✔
31
        if ($emuValue != 0) {
107✔
32
            return (int) round($emuValue / 9525);
107✔
33
        }
34

35
        return 0;
91✔
36
    }
37

38
    /**
39
     * Convert pixels to column width. Exact algorithm not known.
40
     * By inspection of a real Excel file using Calibri 11, one finds 1000px ~ 142.85546875
41
     * This gives a conversion factor of 7. Also, we assume that pixels and font size are proportional.
42
     *
43
     * @param int $pixelValue Value in pixels
44
     *
45
     * @return float|int Value in cell dimension
46
     */
47
    public static function pixelsToCellDimension(int $pixelValue, \PhpOffice\PhpSpreadsheet\Style\Font $defaultFont): int|float
154✔
48
    {
49
        // Font name and size
50
        $name = $defaultFont->getName();
154✔
51
        $size = $defaultFont->getSize();
154✔
52
        $sizex = ($size !== null && $size == (int) $size) ? ((int) $size) : "$size";
154✔
53

54
        if (isset(Font::DEFAULT_COLUMN_WIDTHS[$name][$sizex])) {
154✔
55
            // Exact width can be determined
56
            return $pixelValue * Font::DEFAULT_COLUMN_WIDTHS[$name][$sizex]['width']
149✔
57
                / Font::DEFAULT_COLUMN_WIDTHS[$name][$sizex]['px'];
149✔
58
        }
59

60
        // We don't have data for this particular font and size, use approximation by
61
        // extrapolating from Calibri 11
62
        return $pixelValue * 11 * Font::DEFAULT_COLUMN_WIDTHS['Calibri'][11]['width']
5✔
63
            / Font::DEFAULT_COLUMN_WIDTHS['Calibri'][11]['px'] / $size;
5✔
64
    }
65

66
    /**
67
     * Convert column width from (intrinsic) Excel units to pixels.
68
     *
69
     * @param float $cellWidth Value in cell dimension
70
     * @param \PhpOffice\PhpSpreadsheet\Style\Font $defaultFont Default font of the workbook
71
     *
72
     * @return int Value in pixels
73
     */
74
    public static function cellDimensionToPixels(float $cellWidth, \PhpOffice\PhpSpreadsheet\Style\Font $defaultFont): int
88✔
75
    {
76
        // Font name and size
77
        $name = $defaultFont->getName();
88✔
78
        $size = $defaultFont->getSize();
88✔
79
        $sizex = ($size !== null && $size == (int) $size) ? ((int) $size) : "$size";
88✔
80

81
        if (isset(Font::DEFAULT_COLUMN_WIDTHS[$name][$sizex])) {
88✔
82
            // Exact width can be determined
83
            $colWidth = $cellWidth * Font::DEFAULT_COLUMN_WIDTHS[$name][$sizex]['px']
74✔
84
                / Font::DEFAULT_COLUMN_WIDTHS[$name][$sizex]['width'];
74✔
85
        } else {
86
            // We don't have data for this particular font and size, use approximation by
87
            // extrapolating from Calibri 11
88
            $colWidth = $cellWidth * $size * Font::DEFAULT_COLUMN_WIDTHS['Calibri'][11]['px']
14✔
89
                / Font::DEFAULT_COLUMN_WIDTHS['Calibri'][11]['width'] / 11;
14✔
90
        }
91

92
        // Round pixels to closest integer
93
        $colWidth = (int) round($colWidth);
88✔
94

95
        return $colWidth;
88✔
96
    }
97

98
    /**
99
     * Convert pixels to points.
100
     *
101
     * @param int $pixelValue Value in pixels
102
     *
103
     * @return float Value in points
104
     */
105
    public static function pixelsToPoints(int $pixelValue): float
42✔
106
    {
107
        return $pixelValue * 0.75;
42✔
108
    }
109

110
    /**
111
     * Convert points to pixels.
112
     *
113
     * @param float|int $pointValue Value in points
114
     *
115
     * @return int Value in pixels
116
     */
117
    public static function pointsToPixels($pointValue): int
4✔
118
    {
119
        if ($pointValue != 0) {
4✔
120
            return (int) ceil($pointValue / 0.75);
4✔
121
        }
122

UNCOV
123
        return 0;
×
124
    }
125

126
    /**
127
     * Convert degrees to angle.
128
     *
129
     * @param int $degrees Degrees
130
     *
131
     * @return int Angle
132
     */
133
    public static function degreesToAngle(int $degrees): int
53✔
134
    {
135
        return (int) round($degrees * 60000);
53✔
136
    }
137

138
    /**
139
     * Convert angle to degrees.
140
     *
141
     * @param int|SimpleXMLElement $angle Angle
142
     *
143
     * @return int Degrees
144
     */
145
    public static function angleToDegrees($angle): int
58✔
146
    {
147
        $angle = (int) $angle;
58✔
148
        if ($angle != 0) {
58✔
149
            return (int) round($angle / 60000);
4✔
150
        }
151

152
        return 0;
58✔
153
    }
154
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc