Skip to content

Commit d19be38

Browse files
✨ add support for V2 crop and split operations (#179)
1 parent 040d4cc commit d19be38

18 files changed

Lines changed: 850 additions & 142 deletions

.gitignore

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ composer.lock
2121
.env.local
2222
_test.php
2323
.php-cs-fixer.cache
24-
local_test/
25-
LocalTest.php
2624
.phpunit*
2725
*.log
2826
/build
2927
.phplint-cache
3028
.phpdoc
3129
docs/_build
3230
tests/LocalTestNotUnit.php
31+
32+
# For local testing
33+
/output/
34+
local_test/
35+
LocalTest.php

examples/MultiReceiptsAutoExtractionExample.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
use Mindee\Client;
4-
use Mindee\Extraction\ImageExtractor;
4+
use Mindee\V1\Image\ImageExtractor;
55
use Mindee\Input\PathInput;
66
use Mindee\Product\MultiReceiptsDetector\MultiReceiptsDetectorV1;
77
use Mindee\Product\Receipt\ReceiptV5;

src/Error/MindeeInputException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Mindee\Error;
4+
5+
/**
6+
* Input exceptions.
7+
*/
8+
class MindeeInputException extends MindeeException
9+
{
10+
}

src/Extraction/ExtractedImage.php

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,77 +12,100 @@
1212
class ExtractedImage
1313
{
1414
/**
15-
* Imagick wrapper for the image.
16-
*
17-
* @var \Imagick
15+
* @var \Imagick Wrapper for the image.
1816
*/
1917
public \Imagick $image;
2018

2119
/**
22-
* Name of the file.
23-
*
24-
* @var string
20+
* @var string Name of the file.
2521
*/
2622
public string $filename;
2723

2824
/**
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.
3236
*/
3337
protected string $saveFormat;
3438

3539
/**
3640
* Initializes a new instance of the ExtractedImage class.
3741
*
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+
*
4148
* @throws MindeeUnhandledException Throws if PDF operations aren't supported.
4249
*/
43-
public function __construct(mixed $image, string $filename, string $saveFormat)
50+
public function __construct(mixed $image, string $filename, string $saveFormat, int $pageIndex, int $index)
4451
{
4552
DependencyChecker::isImageMagickAvailable();
4653
DependencyChecker::isGhostscriptAvailable();
4754
$this->image = $image;
4855
$this->filename = $filename;
4956
$this->saveFormat = $saveFormat;
57+
$this->pageId = $pageIndex;
58+
$this->elementId = $index;
5059
}
5160

5261
/**
5362
* Writes the image to a file.
5463
* Uses the default image format and filename.
5564
*
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+
*
5769
* @return void
5870
* @throws \ImagickException Throws if the image can't be processed.
5971
*/
60-
public function writeToFile(string $outputPath): void
72+
public function writeToFile(string $outputPath, ?string $format = null, int $quality = 100): void
6173
{
6274
$imagePath = $outputPath . DIRECTORY_SEPARATOR . $this->filename;
63-
$format = $this->getEncodedImageFormat($this->saveFormat);
75+
$format = $this->getEncodedImageFormat($format ?? $this->saveFormat);
6476
$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);
6586
$this->image->writeImage($imagePath);
6687
}
6788

6889
/**
6990
* Returns the image in a format suitable for sending to a client for parsing.
7091
*
71-
* @throws \ImagickException Throws if the image can't be processed.
7292
* @return BytesInput Bytes input for the image.
93+
*
94+
* @throws \ImagickException Throws if the image can't be processed.
7395
*/
7496
public function asInputSource(): BytesInput
7597
{
7698
$format = $this->getEncodedImageFormat($this->saveFormat);
7799
$this->image->setImageFormat($format);
100+
78101
return new BytesInput($this->image->getImageBlob(), $this->filename);
79102
}
80103

81104
/**
82105
* Get the encoded image format.
83106
*
84107
* @param string $saveFormat Format to save the file as.
85-
* @return string
108+
* @return string Encoded image format.
86109
*/
87110
private function getEncodedImageFormat(string $saveFormat): string
88111
{

src/Extraction/ExtractedPdf.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,21 @@
1616
class ExtractedPdf
1717
{
1818
/**
19-
* File object for an ExtractedPdf.
20-
*
21-
* @var string
19+
* @var string name of the original file
2220
*/
23-
protected string $pdfBytes;
21+
public string $filename;
2422

2523
/**
26-
* Name of the original file.
27-
*
28-
* @var string
24+
* @var string File object for an ExtractedPdf.
2925
*/
30-
protected string $filename;
26+
protected string $pdfBytes;
3127

3228
/**
3329
* Initializes a new instance of the ExtractedPdf class.
3430
*
3531
* @param string $pdfBytes A binary string representation of the PDF.
3632
* @param string $filename Name of the original file.
33+
*
3734
* @throws MindeeUnhandledException Throws if PDF operations aren't supported.
3835
*/
3936
public function __construct(string $pdfBytes, string $filename)
@@ -47,16 +44,18 @@ public function __construct(string $pdfBytes, string $filename)
4744
/**
4845
* Wrapper for pdf GetPageCount().
4946
*
50-
* @return integer The number of pages in the file.
47+
* @return integer the number of pages in the file
48+
*
5149
* @throws MindeePDFException Throws if FPDI is unable to process the file.
5250
*/
5351
public function getPageCount(): int
5452
{
5553
try {
56-
$pdfHandle = new FPDI();
54+
$pdfHandle = new Fpdi();
5755

5856
$tempFilename = tempnam(sys_get_temp_dir(), 'extracted_pdf_');
5957
file_put_contents($tempFilename, $this->pdfBytes);
58+
6059
return $pdfHandle->setSourceFile($tempFilename);
6160
} catch (PdfParserException $e) {
6261
throw new MindeePDFException(
@@ -76,32 +75,37 @@ public function getPageCount(): int
7675
public function writeToFile(string $outputPath): void
7776
{
7877
$pdfPath = $outputPath . DIRECTORY_SEPARATOR . $this->filename;
79-
if (basename($outputPath) !== '') {
80-
$pdfPath = realpath($outputPath);
78+
if ('' !== basename($outputPath)) {
79+
if (!($pdfPath = realpath($outputPath))) {
80+
$pdfPath = $outputPath;
81+
}
82+
}
83+
if (!str_ends_with(strtolower($pdfPath), 'pdf')) {
84+
$pdfPath .= '.pdf';
8185
}
8286
file_put_contents($pdfPath, $this->pdfBytes);
8387
}
8488

8589
/**
8690
* Return the file in a format suitable for sending to MindeeClient for parsing.
8791
*
88-
* @return BytesInput Bytes input for the image.
92+
* @return BytesInput bytes input for the image
8993
*/
9094
public function asInputSource(): BytesInput
9195
{
9296
return new BytesInput($this->pdfBytes, $this->filename);
9397
}
9498

9599
/**
96-
* @return string The pdf bytes.
100+
* @return string the pdf bytes
97101
*/
98102
public function getPdfBytes(): string
99103
{
100104
return $this->pdfBytes;
101105
}
102106

103107
/**
104-
* @return string The name of the file.
108+
* @return string the name of the file
105109
*/
106110
public function getFilename(): string
107111
{

0 commit comments

Comments
 (0)