| 1 |
<?php |
| 2 |
|
| 3 |
namespace PhpOffice\PhpSpreadsheet\Shared; |
| 4 |
|
| 5 |
use GdImage; |
| 6 |
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException; |
| 7 |
use SimpleXMLElement; |
| 8 |
|
| 9 |
class Drawing |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Convert pixels to EMU. |
| 13 |
* |
| 14 |
* @param int $pixelValue Value in pixels |
| 15 |
* |
| 16 |
* @return int Value in EMU |
| 17 |
*/ |
| 18 |
public static function pixelsToEMU($pixelValue) |
| 19 |
{ |
| 20 |
return $pixelValue * 9525; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Convert EMU to pixels. |
| 25 |
* |
| 26 |
* @param int|SimpleXMLElement $emuValue Value in EMU |
| 27 |
* |
| 28 |
* @return int Value in pixels |
| 29 |
*/ |
| 30 |
public static function EMUToPixels($emuValue) |
| 31 |
{ |
| 32 |
$emuValue = (int) $emuValue; |
| 33 |
if ($emuValue != 0) { |
| 34 |
return (int) round($emuValue / 9525); |
| 35 |
} |
| 36 |
|
| 37 |
return 0; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Convert pixels to column width. Exact algorithm not known. |
| 42 |
* By inspection of a real Excel file using Calibri 11, one finds 1000px ~ 142.85546875 |
| 43 |
* This gives a conversion factor of 7. Also, we assume that pixels and font size are proportional. |
| 44 |
* |
| 45 |
* @param int $pixelValue Value in pixels |
| 46 |
* |
| 47 |
* @return float|int Value in cell dimension |
| 48 |
*/ |
| 49 |
public static function pixelsToCellDimension($pixelValue, \PhpOffice\PhpSpreadsheet\Style\Font $defaultFont) |
| 50 |
{ |
| 51 |
// Font name and size |
| 52 |
$name = $defaultFont->getName(); |
| 53 |
$size = $defaultFont->getSize(); |
| 54 |
|
| 55 |
if (isset(Font::$defaultColumnWidths[$name][$size])) { |
| 56 |
// Exact width can be determined |
| 57 |
return $pixelValue * Font::$defaultColumnWidths[$name][$size]['width'] |
| 58 |
/ Font::$defaultColumnWidths[$name][$size]['px']; |
| 59 |
} |
| 60 |
|
| 61 |
// We don't have data for this particular font and size, use approximation by |
| 62 |
// extrapolating from Calibri 11 |
| 63 |
return $pixelValue * 11 * Font::$defaultColumnWidths['Calibri'][11]['width'] |
| 64 |
/ Font::$defaultColumnWidths['Calibri'][11]['px'] / $size; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Convert column width from (intrinsic) Excel units to pixels. |
| 69 |
* |
| 70 |
* @param float $cellWidth Value in cell dimension |
| 71 |
* @param \PhpOffice\PhpSpreadsheet\Style\Font $defaultFont Default font of the workbook |
| 72 |
* |
| 73 |
* @return int Value in pixels |
| 74 |
*/ |
| 75 |
public static function cellDimensionToPixels($cellWidth, \PhpOffice\PhpSpreadsheet\Style\Font $defaultFont) |
| 76 |
{ |
| 77 |
// Font name and size |
| 78 |
$name = $defaultFont->getName(); |
| 79 |
$size = $defaultFont->getSize(); |
| 80 |
|
| 81 |
if (isset(Font::$defaultColumnWidths[$name][$size])) { |
| 82 |
// Exact width can be determined |
| 83 |
$colWidth = $cellWidth * Font::$defaultColumnWidths[$name][$size]['px'] |
| 84 |
/ Font::$defaultColumnWidths[$name][$size]['width']; |
| 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::$defaultColumnWidths['Calibri'][11]['px'] |
| 89 |
/ Font::$defaultColumnWidths['Calibri'][11]['width'] / 11; |
| 90 |
} |
| 91 |
|
| 92 |
// Round pixels to closest integer |
| 93 |
$colWidth = (int) round($colWidth); |
| 94 |
|
| 95 |
return $colWidth; |
| 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($pixelValue) |
| 106 |
{ |
| 107 |
return $pixelValue * 0.75; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Convert points to pixels. |
| 112 |
* |
| 113 |
* @param int $pointValue Value in points |
| 114 |
* |
| 115 |
* @return int Value in pixels |
| 116 |
*/ |
| 117 |
public static function pointsToPixels($pointValue) |
| 118 |
{ |
| 119 |
if ($pointValue != 0) { |
| 120 |
return (int) ceil($pointValue / 0.75); |
| 121 |
} |
| 122 |
|
| 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($degrees) |
| 134 |
{ |
| 135 |
return (int) round($degrees * 60000); |
| 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) |
| 146 |
{ |
| 147 |
$angle = (int) $angle; |
| 148 |
if ($angle != 0) { |
| 149 |
return (int) round($angle / 60000); |
| 150 |
} |
| 151 |
|
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Create a new image from file. By alexander at alexauto dot nl. |
| 157 |
* |
| 158 |
* @see http://www.php.net/manual/en/function.imagecreatefromwbmp.php#86214 |
| 159 |
* |
| 160 |
* @param string $bmpFilename Path to Windows DIB (BMP) image |
| 161 |
* |
| 162 |
* @return GdImage|resource |
| 163 |
* |
| 164 |
* @deprecated 1.26 use Php function imagecreatefrombmp instead |
| 165 |
* |
| 166 |
* @codeCoverageIgnore |
| 167 |
*/ |
| 168 |
public static function imagecreatefrombmp($bmpFilename) |
| 169 |
{ |
| 170 |
$retVal = @imagecreatefrombmp($bmpFilename); |
| 171 |
if ($retVal === false) { |
| 172 |
throw new ReaderException("Unable to create image from $bmpFilename"); |
| 173 |
} |
| 174 |
|
| 175 |
return $retVal; |
| 176 |
} |
| 177 |
} |
| 178 |
|