Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
65 / 65 |
|
100.00% |
18 / 18 |
CRAP | |
100.00% |
1 / 1 |
| Image | |
100.00% |
65 / 65 |
|
100.00% |
18 / 18 |
36 | |
100.00% |
1 / 1 |
| createImageFromFile | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| createImageFromStream | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| loadImageFromFile | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
7 | |||
| loadImageFromStream | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
6 | |||
| resizeToWidth | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| resizeToHeight | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| resize | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| scale | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| isFile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| isStream | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| getImage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getStream | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getWidth | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getHeight | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getResizedWidth | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
3 | |||
| getResizedHeight | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
3 | |||
| getResizeDimensions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isPreserveResolution | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Pop PHP Framework (https://www.popphp.org/) |
| 4 | * |
| 5 | * @link https://github.com/popphp/popphp-framework |
| 6 | * @author Nick Sagona, III <dev@noladev.com> |
| 7 | * @copyright Copyright (c) 2009-2026 NOLA Interactive, LLC. |
| 8 | * @license https://www.popphp.org/license New BSD License |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * @namespace |
| 13 | */ |
| 14 | namespace Pop\Pdf\Document\Page; |
| 15 | |
| 16 | /** |
| 17 | * Pdf page image class |
| 18 | * |
| 19 | * @category Pop |
| 20 | * @package Pop\Pdf |
| 21 | * @author Nick Sagona, III <dev@noladev.com> |
| 22 | * @copyright Copyright (c) 2009-2026 NOLA Interactive, LLC. |
| 23 | * @license https://www.popphp.org/license New BSD License |
| 24 | * @version 5.2.7 |
| 25 | */ |
| 26 | class Image |
| 27 | { |
| 28 | |
| 29 | /** |
| 30 | * Image file name |
| 31 | * @var ?string |
| 32 | */ |
| 33 | protected ?string $image = null; |
| 34 | |
| 35 | /** |
| 36 | * Image stream |
| 37 | * @var string |
| 38 | */ |
| 39 | protected ?string $stream = null; |
| 40 | |
| 41 | /** |
| 42 | * Image width |
| 43 | * @var ?int |
| 44 | */ |
| 45 | protected ?int $width = null; |
| 46 | |
| 47 | /** |
| 48 | * Image height |
| 49 | * @var ?int |
| 50 | */ |
| 51 | protected ?int $height = null; |
| 52 | |
| 53 | /** |
| 54 | * Image resize value |
| 55 | * @var ?array |
| 56 | */ |
| 57 | protected ?array $resize = null; |
| 58 | |
| 59 | /** |
| 60 | * Flag to preserve image resolution |
| 61 | * @var bool |
| 62 | */ |
| 63 | protected bool $preserveResolution = false; |
| 64 | |
| 65 | /** |
| 66 | * Create PDF image object from file |
| 67 | * |
| 68 | * @param string $file |
| 69 | * @throws Exception |
| 70 | * @return Image |
| 71 | */ |
| 72 | public static function createImageFromFile(string $file): Image |
| 73 | { |
| 74 | $image = new self(); |
| 75 | $image->loadImageFromFile($file); |
| 76 | return $image; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Create PDF image object from data stream |
| 81 | * |
| 82 | * @param string $stream |
| 83 | * @throws Exception |
| 84 | * @return Image |
| 85 | */ |
| 86 | public static function createImageFromStream(string $stream): Image |
| 87 | { |
| 88 | $image = new self(); |
| 89 | $image->loadImageFromStream($stream); |
| 90 | return $image; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Load image from file |
| 95 | * |
| 96 | * @param string $file |
| 97 | * @throws Exception |
| 98 | * @return Image |
| 99 | */ |
| 100 | public function loadImageFromFile(string $file): Image |
| 101 | { |
| 102 | if (!file_exists($file)) { |
| 103 | throw new Exception('Error: That image file does not exist.'); |
| 104 | } |
| 105 | |
| 106 | $imgSize = getimagesize($file); |
| 107 | |
| 108 | if (!isset($imgSize['mime']) || |
| 109 | (isset($imgSize['mime']) && ($imgSize['mime'] != 'image/jpeg') && |
| 110 | ($imgSize['mime'] != 'image/gif') && ($imgSize['mime'] != 'image/png'))) { |
| 111 | throw new Exception( |
| 112 | 'Error: That image type is not supported. Only GIF, JPG and PNG image types are supported.' |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | // Set image properties. |
| 117 | $this->image = $file; |
| 118 | $this->width = $imgSize[0]; |
| 119 | $this->height = $imgSize[1]; |
| 120 | |
| 121 | return $this; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Load image from stream |
| 126 | * |
| 127 | * @param string $stream |
| 128 | * @throws Exception |
| 129 | * @return Image |
| 130 | */ |
| 131 | public function loadImageFromStream(string $stream): Image |
| 132 | { |
| 133 | $imgSize = getimagesizefromstring($stream); |
| 134 | |
| 135 | if (!isset($imgSize['mime']) || |
| 136 | (isset($imgSize['mime']) && ($imgSize['mime'] != 'image/jpeg') && |
| 137 | ($imgSize['mime'] != 'image/gif') && ($imgSize['mime'] != 'image/png'))) { |
| 138 | throw new Exception( |
| 139 | 'Error: That image type is not supported. Only GIF, JPG and PNG image types are supported.' |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | $this->stream = $stream; |
| 144 | $this->width = $imgSize[0]; |
| 145 | $this->height = $imgSize[1]; |
| 146 | |
| 147 | return $this; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Resize image to width |
| 152 | * |
| 153 | * @param int $width |
| 154 | * @param bool $preserveResolution |
| 155 | * @return Image |
| 156 | */ |
| 157 | public function resizeToWidth(int $width, bool $preserveResolution = false): Image |
| 158 | { |
| 159 | $this->resize = [ |
| 160 | 'width' => $width, |
| 161 | 'height' => round($this->height * ($width / $this->width)) |
| 162 | ]; |
| 163 | |
| 164 | $this->preserveResolution = $preserveResolution; |
| 165 | return $this; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Resize image to height |
| 170 | * |
| 171 | * @param int $height |
| 172 | * @param bool $preserveResolution |
| 173 | * @return Image |
| 174 | */ |
| 175 | public function resizeToHeight(int $height, bool $preserveResolution = false): Image |
| 176 | { |
| 177 | $this->resize = [ |
| 178 | 'width' => round($this->width * ($height / $this->height)), |
| 179 | 'height' => $height |
| 180 | ]; |
| 181 | |
| 182 | $this->preserveResolution = $preserveResolution; |
| 183 | return $this; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Resize image on whichever dimension is the greatest |
| 188 | * |
| 189 | * @param int $pixel |
| 190 | * @param bool $preserveResolution |
| 191 | * @return Image |
| 192 | */ |
| 193 | public function resize(int $pixel, bool $preserveResolution = false): Image |
| 194 | { |
| 195 | $scale = ($this->width > $this->height) ? ($pixel / $this->width) : ($pixel / $this->height); |
| 196 | $this->resize = [ |
| 197 | 'width' => round($this->width * $scale), |
| 198 | 'height' => round($this->height * $scale) |
| 199 | ]; |
| 200 | |
| 201 | $this->preserveResolution = $preserveResolution; |
| 202 | return $this; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Scale image |
| 207 | * |
| 208 | * @param float $scale |
| 209 | * @param bool $preserveResolution |
| 210 | * @return Image |
| 211 | */ |
| 212 | public function scale(float $scale, bool $preserveResolution = false): Image |
| 213 | { |
| 214 | $this->resize = [ |
| 215 | 'width' => round($this->width * $scale), |
| 216 | 'height' => round($this->height * $scale) |
| 217 | ]; |
| 218 | $this->preserveResolution = $preserveResolution; |
| 219 | return $this; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Is image file |
| 224 | * |
| 225 | * @return bool |
| 226 | */ |
| 227 | public function isFile(): bool |
| 228 | { |
| 229 | return (($this->image !== null) && ($this->stream === null)); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Is image stream |
| 234 | * |
| 235 | * @return bool |
| 236 | */ |
| 237 | public function isStream(): bool |
| 238 | { |
| 239 | return (($this->stream !== null) && ($this->image === null)); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Get the image file |
| 244 | * |
| 245 | * @return ?string |
| 246 | */ |
| 247 | public function getImage(): ?string |
| 248 | { |
| 249 | return $this->image; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Get the image stream |
| 254 | * |
| 255 | * @return ?string |
| 256 | */ |
| 257 | public function getStream(): ?string |
| 258 | { |
| 259 | return $this->stream; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Get the image width |
| 264 | * |
| 265 | * @return ?int |
| 266 | */ |
| 267 | public function getWidth(): ?int |
| 268 | { |
| 269 | return $this->width; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Get the image height |
| 274 | * |
| 275 | * @return ?int |
| 276 | */ |
| 277 | public function getHeight(): ?int |
| 278 | { |
| 279 | return $this->height; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Get the image resized width |
| 284 | * |
| 285 | * @return ?int |
| 286 | */ |
| 287 | public function getResizedWidth(): ?int |
| 288 | { |
| 289 | return (($this->resize !== null) && isset($this->resize['width'])) ? $this->resize['width'] : null; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Get the image resized height |
| 294 | * |
| 295 | * @return ?int |
| 296 | */ |
| 297 | public function getResizedHeight(): ?int |
| 298 | { |
| 299 | return (($this->resize !== null) && isset($this->resize['height'])) ? $this->resize['height'] : null; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Get the image resize dimensions |
| 304 | * |
| 305 | * @return ?array |
| 306 | */ |
| 307 | public function getResizeDimensions(): ?array |
| 308 | { |
| 309 | return $this->resize; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Get the image preserve resolution flag |
| 314 | * |
| 315 | * @return bool |
| 316 | */ |
| 317 | public function isPreserveResolution(): bool |
| 318 | { |
| 319 | return $this->preserveResolution; |
| 320 | } |
| 321 | |
| 322 | } |