Skip to content

Commit 15bcb31

Browse files
fix naming
1 parent 21d8a6d commit 15bcb31

3 files changed

Lines changed: 36 additions & 13 deletions

File tree

src/Extraction/ImageExtractor.php

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/V2/FileOperations/Crop.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ public function extractCrops(array $crops): CropFiles
5555

5656
foreach ($cropsPerPage as $page => $pageCrops) {
5757
$polygons = array_map(fn ($c) => $c->location->polygon, $pageCrops);
58+
$filenamePrefix = sprintf('%s_page%d', $this->localInput->fileName, $page);
5859

5960
$images = $imageExtractor->extractPolygonsFromPage(
6061
$polygons,
61-
$page
62+
$page,
63+
$filenamePrefix
6264
);
6365
array_push($extractedImages, ...$images);
6466
}

tests/V2/FileOperations/CropFunctional.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ public function testExtractCropsFromImageCorrectly(): void
8080
$extractedImages->saveAllToDisk($this->outputDir, quality: 50);
8181

8282
$file1Info = filesize($this->outputDir . '/crop_001.jpg');
83-
$this->assertGreaterThanOrEqual(99000, $file1Info);
83+
$this->assertGreaterThanOrEqual(98000, $file1Info);
8484
$this->assertLessThanOrEqual(110000, $file1Info);
8585

8686
$file2Info = filesize($this->outputDir . '/crop_002.jpg');
87-
$this->assertGreaterThanOrEqual(99000, $file2Info);
87+
$this->assertGreaterThanOrEqual(98000, $file2Info);
8888
$this->assertLessThanOrEqual(110000, $file2Info);
8989
}
9090

0 commit comments

Comments
 (0)