-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCrop.php
More file actions
66 lines (55 loc) · 1.7 KB
/
Copy pathCrop.php
File metadata and controls
66 lines (55 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
declare(strict_types=1);
namespace Mindee\V2\FileOperations;
use Mindee\Image\ExtractedImage;
use Mindee\Image\ImageExtractor;
use Mindee\Input\LocalInputSource;
use Mindee\V2\Product\Crop\CropItem;
use function sprintf;
/**
* V2 Crop operation.
*/
class Crop
{
/**
* @param LocalInputSource $localInput LocalInputSource object.
*/
public function __construct(private readonly LocalInputSource $localInput) {}
/**
* Extracts a crop zone from a file.
*
* @param CropItem $crop Crop to extract.
*
* @return ExtractedImage extracted image
*/
public function extractSingleCrop(CropItem $crop): ExtractedImage
{
return $this->extractMultipleCrops([$crop])[0];
}
/**
* Extracts multiple crop zones from a file.
*
* @param CropItem[] $crops List of crops to extract.
* @return CropFiles list of extracted files
*/
public function extractMultipleCrops(array $crops): CropFiles
{
$imageExtractor = new ImageExtractor($this->localInput);
$extractedImages = [];
$cropsPerPage = [];
foreach ($crops as $crop) {
$cropsPerPage[$crop->location->page][] = $crop;
}
foreach ($cropsPerPage as $page => $pageCrops) {
$polygons = array_map(static fn($c) => $c->location->polygon, $pageCrops);
$filenamePrefix = sprintf('%s_page%d', $this->localInput->fileName, $page);
$images = $imageExtractor->extractPolygonsFromPage(
$polygons,
$page,
$filenamePrefix
);
array_push($extractedImages, ...$images);
}
return new CropFiles(...$extractedImages);
}
}