Skip to content
Draft
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
24 changes: 5 additions & 19 deletions src/Image/ExtractedImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Exception;

use function BernardLedit\Image\decode;
use function BernardLedit\Common\formatFromExtension;
use function sprintf;
use function strlen;

Expand Down Expand Up @@ -52,8 +53,10 @@ public function __construct(public string $buffer, public string $filename, prot
public function writeToFile(string $outputPath, ?string $format = null, ?int $quality = null): void
{
$imagePath = $outputPath . DIRECTORY_SEPARATOR . $this->filename;
$encodedFormat = self::getEncodedImageFormat($format ?? $this->saveFormat);
if (null === $quality && $encodedFormat === self::getEncodedImageFormat($this->saveFormat)) {
$extension = pathinfo($this->filename, PATHINFO_EXTENSION);
$formatToEncode = $format ?? ('' !== $extension ? $extension : $this->saveFormat);
$encodedFormat = formatFromExtension($formatToEncode);
if (null === $quality && $encodedFormat === formatFromExtension($this->saveFormat)) {
self::writeBytes($imagePath, $this->buffer);
return;
}
Expand All @@ -71,23 +74,6 @@ public function asInputSource(): BytesInput
return new BytesInput($this->buffer, $this->filename);
}

/**
* Get the encoded image format, as understood by bernard_ledit.
*
* @param string $saveFormat Format to save the file as.
* @return string Encoded image format.
*/
public static function getEncodedImageFormat(string $saveFormat): string
{
return match (strtolower($saveFormat)) {
'png' => 'png',
'bmp', => 'bmp',
'gif' => 'gif',
'webp' => 'webp',
default => 'jpeg',
};
}

/**
* Writes bytes to a file, and fails if they weren't all written.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Image/ImageCompressor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Mindee\Error\MindeeImageException;
use Mindee\Error\MindeeUnhandledException;

use function BernardLedit\Image\compress as compressImage;
use function BernardLedit\Image\compressToJpeg as compressImage;

/**
* Image compressor class to handle image compression.
Expand All @@ -36,7 +36,7 @@ public static function compress(
): string {
DependencyChecker::requireBernardLedit();
try {
[$bytes] = compressImage($inputImage, $quality, $maxWidth, $maxHeight);
[$bytes] = compressImage($inputImage, $quality, null, $maxWidth, $maxHeight);
return $bytes;
} catch (Exception $e) {
throw new MindeeImageException("Image compression failed.", ErrorCode::FILE_OPERATION_ABORTED, $e);
Expand Down
26 changes: 20 additions & 6 deletions src/Image/ImageExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

use BernardLedit\Image\Image;
use BernardLedit\Image\ImageException;
use BernardLedit\Image\ImageExtractException;
use BernardLedit\Pdf\PdfExtractException;
use BernardLedit\Pdf\PdfDocument;
use BernardLedit\Pdf\PdfiumException;
use InvalidArgumentException;
use Mindee\Dependency\DependencyChecker;
Expand All @@ -17,10 +16,11 @@
use Mindee\Geometry\Point;
use Mindee\Geometry\Polygon;
use Mindee\Input\LocalInputSource;
use Mindee\Pdf\PdfUtils;
use ValueError;

use function BernardLedit\Common\formatFromExtension;
use function BernardLedit\Image\guessFormat;
use function BernardLedit\Image\extractFromPolygons;
use function count;
use function sprintf;
use function is_array;
Expand Down Expand Up @@ -204,8 +204,22 @@ protected function cropPolygons(array $polygons, int $pageIndex): array
{
$pairs = array_map(static::polygonToPairs(...), $polygons);
try {
return extractFromPolygons($this->sourceData, $pageIndex, $pairs, $this->pdfDpi);
} catch (ImageException|ImageExtractException|PdfExtractException|PdfiumException|ValueError $e) {
if ($this->inputSource->isPdf()) {
$pdf = new PdfDocument($this->sourceData);
try {
$sourceImage = $pdf->getPage($pageIndex)->render($this->pdfDpi / 72.0);
} finally {
PdfUtils::closeQuietly($pdf);
}
} else {
$sourceImage = new Image($this->sourceData);
}

return array_map(
static fn(array $polygon): Image => $sourceImage->cropPolygon($polygon),
$pairs
);
} catch (ImageException|PdfiumException|ValueError $e) {
throw new MindeeImageException($e->getMessage(), $e->getCode(), $e);
}
}
Expand All @@ -223,7 +237,7 @@ protected function encodeCrop(
string $format
): ExtractedImage {
try {
$data = $crop->encode(ExtractedImage::getEncodedImageFormat($format), self::CROP_QUALITY);
$data = $crop->encode(formatFromExtension($format), self::CROP_QUALITY);
} catch (ImageException|ValueError $e) {
throw new MindeeImageException($e->getMessage(), $e->getCode(), $e);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Input/LocalInputSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
use function BernardLedit\Pdf\applyPageOptions as nativeApplyPageOptions;
use function BernardLedit\Pdf\fixPdf as nativeFixPdf;
use function in_array;
use function BernardLedit\Image\compress as compressImage;
use function BernardLedit\Pdf\compress as compressPdf;
use function BernardLedit\Image\compressToJpeg as compressImage;
use function BernardLedit\Pdf\compressToJpeg as compressPdf;

/**
* List of allowed mime types for document parsing.
Expand Down Expand Up @@ -284,7 +284,7 @@ public function compress(
if ($this->isPdf()) {
$compressed = compressPdf($bytes, $quality, $optimize, $dpi);
} else {
[$compressed] = compressImage($bytes, $quality, $maxWidth, $maxHeight, $optimize);
[$compressed] = compressImage($bytes, $quality, $optimize, $maxWidth, $maxHeight);
if ($compressed !== $bytes) {
$this->fileMimetype = 'image/jpeg';
$extension = strtolower(pathinfo($this->fileName, PATHINFO_EXTENSION));
Expand Down Expand Up @@ -353,7 +353,7 @@ public function applyPageOptions(?PageOptions $pageOptions): void
return;
}
try {
$documentBytes = $document->asBytes();
$documentBytes = $document->toBytes();
$this->saveBytesAsFile($documentBytes);
$this->pageCount = $document->getPageCount();
} catch (Exception $e) {
Expand Down
3 changes: 2 additions & 1 deletion src/Pdf/ExtractedPdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Mindee\Error\MindeePdfException;
use Mindee\Error\MindeeUnhandledException;
use Mindee\Input\BytesInput;
use function BernardLedit\Common\formatFromExtension;

use const DIRECTORY_SEPARATOR;

Expand Down Expand Up @@ -71,7 +72,7 @@ public function writeToFile(string $outputPath): void
$pdfPath = $outputPath;
}
}
if (!str_ends_with(strtolower($pdfPath), 'pdf')) {
if ('PDF' !== formatFromExtension(pathinfo($pdfPath, PATHINFO_EXTENSION))) {
$pdfPath .= '.pdf';
}
file_put_contents($pdfPath, $this->pdfBytes);
Expand Down
2 changes: 1 addition & 1 deletion src/Pdf/PdfExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private static function buildSubDocument(PdfDocument $source, array $pageIndexes
try {
$subPdf = PdfDocument::create();
$subPdf->importPages($source, array_values($pageIndexes));
return $subPdf->asBytes();
return $subPdf->toBytes();
} catch (Exception $e) {
throw new MindeePdfException(
"PDF file couldn't be processed during extraction.",
Expand Down
Loading
Loading