Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ composer.lock
.env.local
_test.php
.php-cs-fixer.cache
local_test/
LocalTest.php
.phpunit*
*.log
/build
.phplint-cache
.phpdoc
docs/_build
tests/LocalTestNotUnit.php

# For local testing
/output/
local_test/
LocalTest.php
2 changes: 1 addition & 1 deletion examples/MultiReceiptsAutoExtractionExample.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Mindee\Client;
use Mindee\Extraction\ImageExtractor;
use Mindee\V1\Image\ImageExtractor;
use Mindee\Input\PathInput;
use Mindee\Product\MultiReceiptsDetector\MultiReceiptsDetectorV1;
use Mindee\Product\Receipt\ReceiptV5;
Expand Down
10 changes: 10 additions & 0 deletions src/Error/MindeeInputException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Mindee\Error;

/**
* Input exceptions.
*/
class MindeeInputException extends MindeeException
{
}
59 changes: 41 additions & 18 deletions src/Extraction/ExtractedImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,77 +12,100 @@
class ExtractedImage
{
/**
* Imagick wrapper for the image.
*
* @var \Imagick
* @var \Imagick Wrapper for the image.
*/
public \Imagick $image;

/**
* Name of the file.
*
* @var string
* @var string Name of the file.
*/
public string $filename;

/**
* String representation of the save format.
*
* @var string
* @var integer Page ID of the image.
*/
public int $pageId;

/**
* @var integer Element ID of the image.
*/
public int $elementId;

/**
* @var string String representation of the save format.
*/
protected string $saveFormat;

/**
* Initializes a new instance of the ExtractedImage class.
*
* @param mixed $image The extracted image. Not explicitly typed as \Imagick to avoid errors.
* @param string $filename The filename for the image.
* @param string $saveFormat The format to save the image.
* @param mixed $image The extracted image. Not explicitly typed as \Imagick to avoid errors.
* @param string $filename The filename for the image.
* @param string $saveFormat The format to save the image.
* @param integer $pageIndex The page index of the image.
* @param integer $index The element index of the image.
*
* @throws MindeeUnhandledException Throws if PDF operations aren't supported.
*/
public function __construct(mixed $image, string $filename, string $saveFormat)
public function __construct(mixed $image, string $filename, string $saveFormat, int $pageIndex, int $index)
{
DependencyChecker::isImageMagickAvailable();
DependencyChecker::isGhostscriptAvailable();
$this->image = $image;
$this->filename = $filename;
$this->saveFormat = $saveFormat;
$this->pageId = $pageIndex;
$this->elementId = $index;
}

/**
* Writes the image to a file.
* Uses the default image format and filename.
*
* @param string $outputPath The output directory (must exist).
* @param string $outputPath The output directory (must exist).
* @param null|string $format The image format to use. Defaults to the save format if not provided.
* @param integer $quality Quality of the saved image.
*
* @return void
* @throws \ImagickException Throws if the image can't be processed.
*/
public function writeToFile(string $outputPath): void
public function writeToFile(string $outputPath, ?string $format = null, int $quality = 100): void
{
$imagePath = $outputPath . DIRECTORY_SEPARATOR . $this->filename;
$format = $this->getEncodedImageFormat($this->saveFormat);
$format = $this->getEncodedImageFormat($format ?? $this->saveFormat);
$this->image->setImageFormat($format);
$this->image->stripImage();
$quality = min(100, max(0, $quality));
if ('png' === $format) {
$finalQuality = round($quality * 0.09);
$this->image->setOption('png:compression-level', $finalQuality);
} elseif (in_array($format, ['jpg', 'jpeg'])) {
$this->image->setImageCompression(\Imagick::COMPRESSION_JPEG);
}
$this->image->setImageCompressionQuality($quality);
$this->image->writeImage($imagePath);
}

/**
* Returns the image in a format suitable for sending to a client for parsing.
*
* @throws \ImagickException Throws if the image can't be processed.
* @return BytesInput Bytes input for the image.
*
* @throws \ImagickException Throws if the image can't be processed.
*/
public function asInputSource(): BytesInput
{
$format = $this->getEncodedImageFormat($this->saveFormat);
$this->image->setImageFormat($format);

return new BytesInput($this->image->getImageBlob(), $this->filename);
}

/**
* Get the encoded image format.
*
* @param string $saveFormat Format to save the file as.
* @return string
* @return string Encoded image format.
*/
private function getEncodedImageFormat(string $saveFormat): string
{
Expand Down
34 changes: 19 additions & 15 deletions src/Extraction/ExtractedPdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,21 @@
class ExtractedPdf
{
/**
* File object for an ExtractedPdf.
*
* @var string
* @var string name of the original file
*/
protected string $pdfBytes;
public string $filename;

/**
* Name of the original file.
*
* @var string
* @var string File object for an ExtractedPdf.
*/
protected string $filename;
protected string $pdfBytes;

/**
* Initializes a new instance of the ExtractedPdf class.
*
* @param string $pdfBytes A binary string representation of the PDF.
* @param string $filename Name of the original file.
*
* @throws MindeeUnhandledException Throws if PDF operations aren't supported.
*/
public function __construct(string $pdfBytes, string $filename)
Expand All @@ -47,16 +44,18 @@ public function __construct(string $pdfBytes, string $filename)
/**
* Wrapper for pdf GetPageCount().
*
* @return integer The number of pages in the file.
* @return integer the number of pages in the file
*
* @throws MindeePDFException Throws if FPDI is unable to process the file.
*/
public function getPageCount(): int
{
try {
$pdfHandle = new FPDI();
$pdfHandle = new Fpdi();

$tempFilename = tempnam(sys_get_temp_dir(), 'extracted_pdf_');
file_put_contents($tempFilename, $this->pdfBytes);

return $pdfHandle->setSourceFile($tempFilename);
} catch (PdfParserException $e) {
throw new MindeePDFException(
Expand All @@ -76,32 +75,37 @@ public function getPageCount(): int
public function writeToFile(string $outputPath): void
{
$pdfPath = $outputPath . DIRECTORY_SEPARATOR . $this->filename;
if (basename($outputPath) !== '') {
$pdfPath = realpath($outputPath);
if ('' !== basename($outputPath)) {
if (!($pdfPath = realpath($outputPath))) {
$pdfPath = $outputPath;
}
}
if (!str_ends_with(strtolower($pdfPath), 'pdf')) {
$pdfPath .= '.pdf';
}
file_put_contents($pdfPath, $this->pdfBytes);
}

/**
* Return the file in a format suitable for sending to MindeeClient for parsing.
*
* @return BytesInput Bytes input for the image.
* @return BytesInput bytes input for the image
*/
public function asInputSource(): BytesInput
{
return new BytesInput($this->pdfBytes, $this->filename);
}

/**
* @return string The pdf bytes.
* @return string the pdf bytes
*/
public function getPdfBytes(): string
{
return $this->pdfBytes;
}

/**
* @return string The name of the file.
* @return string the name of the file
*/
public function getFilename(): string
{
Expand Down
Loading
Loading