|
12 | 12 | class ExtractedImage |
13 | 13 | { |
14 | 14 | /** |
15 | | - * Imagick wrapper for the image. |
16 | | - * |
17 | | - * @var \Imagick |
| 15 | + * @var \Imagick Wrapper for the image. |
18 | 16 | */ |
19 | 17 | public \Imagick $image; |
20 | 18 |
|
21 | 19 | /** |
22 | | - * Name of the file. |
23 | | - * |
24 | | - * @var string |
| 20 | + * @var string Name of the file. |
25 | 21 | */ |
26 | 22 | public string $filename; |
27 | 23 |
|
28 | 24 | /** |
29 | | - * String representation of the save format. |
30 | | - * |
31 | | - * @var string |
| 25 | + * @var integer Page ID of the image. |
| 26 | + */ |
| 27 | + public int $pageId; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var integer Element ID of the image. |
| 31 | + */ |
| 32 | + public int $elementId; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var string String representation of the save format. |
32 | 36 | */ |
33 | 37 | protected string $saveFormat; |
34 | 38 |
|
35 | 39 | /** |
36 | 40 | * Initializes a new instance of the ExtractedImage class. |
37 | 41 | * |
38 | | - * @param mixed $image The extracted image. Not explicitly typed as \Imagick to avoid errors. |
39 | | - * @param string $filename The filename for the image. |
40 | | - * @param string $saveFormat The format to save the image. |
| 42 | + * @param mixed $image The extracted image. Not explicitly typed as \Imagick to avoid errors. |
| 43 | + * @param string $filename The filename for the image. |
| 44 | + * @param string $saveFormat The format to save the image. |
| 45 | + * @param integer $pageIndex The page index of the image. |
| 46 | + * @param integer $index The element index of the image. |
| 47 | + * |
41 | 48 | * @throws MindeeUnhandledException Throws if PDF operations aren't supported. |
42 | 49 | */ |
43 | | - public function __construct(mixed $image, string $filename, string $saveFormat) |
| 50 | + public function __construct(mixed $image, string $filename, string $saveFormat, int $pageIndex, int $index) |
44 | 51 | { |
45 | 52 | DependencyChecker::isImageMagickAvailable(); |
46 | 53 | DependencyChecker::isGhostscriptAvailable(); |
47 | 54 | $this->image = $image; |
48 | 55 | $this->filename = $filename; |
49 | 56 | $this->saveFormat = $saveFormat; |
| 57 | + $this->pageId = $pageIndex; |
| 58 | + $this->elementId = $index; |
50 | 59 | } |
51 | 60 |
|
52 | 61 | /** |
53 | 62 | * Writes the image to a file. |
54 | 63 | * Uses the default image format and filename. |
55 | 64 | * |
56 | | - * @param string $outputPath The output directory (must exist). |
| 65 | + * @param string $outputPath The output directory (must exist). |
| 66 | + * @param null|string $format The image format to use. Defaults to the save format if not provided. |
| 67 | + * @param integer $quality Quality of the saved image. |
| 68 | + * |
57 | 69 | * @return void |
58 | 70 | * @throws \ImagickException Throws if the image can't be processed. |
59 | 71 | */ |
60 | | - public function writeToFile(string $outputPath): void |
| 72 | + public function writeToFile(string $outputPath, ?string $format = null, int $quality = 100): void |
61 | 73 | { |
62 | 74 | $imagePath = $outputPath . DIRECTORY_SEPARATOR . $this->filename; |
63 | | - $format = $this->getEncodedImageFormat($this->saveFormat); |
| 75 | + $format = $this->getEncodedImageFormat($format ?? $this->saveFormat); |
64 | 76 | $this->image->setImageFormat($format); |
| 77 | + $this->image->stripImage(); |
| 78 | + $quality = min(100, max(0, $quality)); |
| 79 | + if ('png' === $format) { |
| 80 | + $finalQuality = round($quality * 0.09); |
| 81 | + $this->image->setOption('png:compression-level', $finalQuality); |
| 82 | + } elseif (in_array($format, ['jpg', 'jpeg'])) { |
| 83 | + $this->image->setImageCompression(\Imagick::COMPRESSION_JPEG); |
| 84 | + } |
| 85 | + $this->image->setImageCompressionQuality($quality); |
65 | 86 | $this->image->writeImage($imagePath); |
66 | 87 | } |
67 | 88 |
|
68 | 89 | /** |
69 | 90 | * Returns the image in a format suitable for sending to a client for parsing. |
70 | 91 | * |
71 | | - * @throws \ImagickException Throws if the image can't be processed. |
72 | 92 | * @return BytesInput Bytes input for the image. |
| 93 | + * |
| 94 | + * @throws \ImagickException Throws if the image can't be processed. |
73 | 95 | */ |
74 | 96 | public function asInputSource(): BytesInput |
75 | 97 | { |
76 | 98 | $format = $this->getEncodedImageFormat($this->saveFormat); |
77 | 99 | $this->image->setImageFormat($format); |
| 100 | + |
78 | 101 | return new BytesInput($this->image->getImageBlob(), $this->filename); |
79 | 102 | } |
80 | 103 |
|
81 | 104 | /** |
82 | 105 | * Get the encoded image format. |
83 | 106 | * |
84 | 107 | * @param string $saveFormat Format to save the file as. |
85 | | - * @return string |
| 108 | + * @return string Encoded image format. |
86 | 109 | */ |
87 | 110 | private function getEncodedImageFormat(string $saveFormat): string |
88 | 111 | { |
|
0 commit comments