@@ -137,19 +137,37 @@ public function extractImagesFromPage(array $fields, int $pageIndex, ?string $ou
137137 /**
138138 * Extracts images from a page.
139139 *
140- * @param array $polygons List of polygons to extract.
141- * @param integer $pageIndex The page index to extract, begins at 0.
142- * @param null|string $format Save format for extracted images. Defaults to the original format.
140+ * @param array $polygons List of polygons to extract.
141+ * @param integer $pageIndex The page index to extract, begins at 0.
142+ * @param null|string $filenamePrefix Output filename prefix.
143+ * @param null|string $format Save format for extracted images. Defaults to the original format.
143144 *
144145 * @return array an array of created images
146+ * @throws MindeeImageException Throws if the image can't be processed.
145147 */
146- public function extractPolygonsFromPage (array $ polygons , int $ pageIndex , ?string $ format = null ): array
147- {
148+ public function extractPolygonsFromPage (
149+ array $ polygons ,
150+ int $ pageIndex ,
151+ ?string $ filenamePrefix = null ,
152+ ?string $ format = null
153+ ): array {
148154 $ saveFormat = $ format ?? $ this ->saveFormat ;
149155 $ extractedImages = [];
150156
151- foreach ($ polygons as $ i => $ polygon ) {
152- $ extractedImages [] = $ this ->extractPolygonFromPage ($ polygon , $ pageIndex , $ i , null , $ format );
157+ try {
158+ foreach ($ polygons as $ i => $ polygon ) {
159+ $ filenamePrefix ??= $ this ->filename ;
160+ $ outputFilename = sprintf ('%s-%d.%s ' , $ filenamePrefix , $ i , $ saveFormat );
161+ $ extractedImages [] = $ this ->extractPolygonFromPage (
162+ $ polygon ,
163+ $ pageIndex ,
164+ $ i ,
165+ $ outputFilename ,
166+ $ saveFormat
167+ );
168+ }
169+ } catch (\ImagickException $ e ) {
170+ throw new MindeeImageException ($ e ->getMessage (), $ e ->getCode (), $ e );
153171 }
154172
155173 return $ extractedImages ;
@@ -165,7 +183,7 @@ public function extractPolygonsFromPage(array $polygons, int $pageIndex, ?string
165183 * @param null|string $format Output format.
166184 *
167185 * @return ExtractedImage Extracted image data.
168- * @throws \ImagickException Throws if the image can't be processed.
186+ * @throws MindeeImageException Throws if the image can't be processed.
169187 */
170188 public function extractPolygonFromPage (
171189 Polygon $ polygon ,
@@ -175,11 +193,14 @@ public function extractPolygonFromPage(
175193 ?string $ format = null
176194 ): ExtractedImage {
177195 $ bbox = BBoxUtils::generateBBoxFromPolygon ($ polygon );
178- $ extractedImageData = $ this ->extractImageFromBbox ($ bbox , $ pageIndex );
196+ try {
197+ $ extractedImageData = $ this ->extractImageFromBbox ($ bbox , $ pageIndex );
198+ } catch (\ImagickException $ e ) {
199+ throw new MindeeImageException ($ e ->getMessage (), $ e ->getCode (), $ e );
200+ }
179201 $ filename ??= $ this ->filename ;
180202 $ format ??= $ this ->saveFormat ;
181203 $ filename ??= sprintf ('%s.%s_page%d-%d.%s ' , $ filename , $ format , $ pageIndex , $ index , $ format );
182-
183204 return new ExtractedImage ($ extractedImageData , $ filename , $ format , $ pageIndex , $ index );
184205 }
185206
0 commit comments