|
9 | 9 |
|
10 | 10 | use InvalidArgumentException; |
11 | 11 |
|
| 12 | +/** |
| 13 | + * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) |
| 14 | + */ |
12 | 15 | final readonly class FilesystemPdfImageEmbedder implements PdfImageEmbedderInterface |
13 | 16 | { |
14 | 17 | public function embed(string $source): EmbeddedPdfImage |
@@ -284,7 +287,7 @@ private function createImageDictionary(int $width, int $height, string $colorSpa |
284 | 287 | */ |
285 | 288 | private function unfilterPngScanlines(string $idat, int $height, int $rowLength, int $bytesPerPixel): array |
286 | 289 | { |
287 | | - $inflated = @gzuncompress($idat); |
| 290 | + $inflated = $this->runWithoutWarnings(static fn (): string|false => gzuncompress($idat)); |
288 | 291 | if (!is_string($inflated)) { |
289 | 292 | throw new InvalidArgumentException('PNG image data could not be decompressed.'); |
290 | 293 | } |
@@ -322,13 +325,13 @@ private function unfilterPngRow( |
322 | 325 | ): string { |
323 | 326 | $row = ''; |
324 | 327 | $rowLength = strlen($filteredRow); |
325 | | - $previousRowWithPadding = str_repeat("\x00", $bytesPerPixel) . $previousRow; |
| 328 | + $paddedPreviousRow = str_repeat("\x00", $bytesPerPixel) . $previousRow; |
326 | 329 |
|
327 | 330 | for ($index = 0; $index < $rowLength; $index++) { |
328 | 331 | $rawByte = ord($filteredRow[$index]); |
329 | 332 | $left = $index >= $bytesPerPixel ? ord($row[$index - $bytesPerPixel]) : 0; |
330 | 333 | $above = ord($previousRow[$index]); |
331 | | - $upperLeft = ord($previousRowWithPadding[$index]); |
| 334 | + $upperLeft = ord($paddedPreviousRow[$index]); |
332 | 335 |
|
333 | 336 | $decodedByte = match ($filterType) { |
334 | 337 | 0 => $rawByte, |
@@ -408,7 +411,7 @@ private function assertReadableSource(string $source): void |
408 | 411 |
|
409 | 412 | private function readSourceContents(string $source): string |
410 | 413 | { |
411 | | - $contents = @file_get_contents($source); |
| 414 | + $contents = $this->runWithoutWarnings(static fn (): string|false => file_get_contents($source)); |
412 | 415 | if (!is_string($contents)) { |
413 | 416 | throw new InvalidArgumentException(sprintf('Failed to read image source "%s".', $source)); |
414 | 417 | } |
@@ -460,4 +463,15 @@ private function resolveJpegColorSpace(mixed $channels): string |
460 | 463 | default => '/DeviceRGB', |
461 | 464 | }; |
462 | 465 | } |
| 466 | + |
| 467 | + private function runWithoutWarnings(callable $operation): mixed |
| 468 | + { |
| 469 | + set_error_handler(static fn (): bool => true); |
| 470 | + |
| 471 | + try { |
| 472 | + return $operation(); |
| 473 | + } finally { |
| 474 | + restore_error_handler(); |
| 475 | + } |
| 476 | + } |
463 | 477 | } |
0 commit comments