-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExtractedImage.php
More file actions
97 lines (88 loc) · 2.68 KB
/
Copy pathExtractedImage.php
File metadata and controls
97 lines (88 loc) · 2.68 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
namespace Mindee\Extraction;
use Mindee\Error\MindeeUnhandledException;
use Mindee\Input\BytesInput;
use Mindee\Parsing\DependencyChecker;
/**
* An extracted sub-image.
*/
class ExtractedImage
{
/**
* Imagick wrapper for the image.
*
* @var \Imagick
*/
public \Imagick $image;
/**
* Name of the file.
*
* @var string
*/
public string $filename;
/**
* String representation of the save format.
*
* @var string
*/
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.
* @throws MindeeUnhandledException Throws if PDF operations aren't supported.
*/
public function __construct(mixed $image, string $filename, string $saveFormat)
{
DependencyChecker::isImageMagickAvailable();
DependencyChecker::isGhostscriptAvailable();
$this->image = $image;
$this->filename = $filename;
$this->saveFormat = $saveFormat;
}
/**
* Writes the image to a file.
* Uses the default image format and filename.
*
* @param string $outputPath The output directory (must exist).
* @return void
* @throws \ImagickException Throws if the image can't be processed.
*/
public function writeToFile(string $outputPath): void
{
$imagePath = $outputPath . DIRECTORY_SEPARATOR . $this->filename;
$format = $this->getEncodedImageFormat($this->saveFormat);
$this->image->setImageFormat($format);
$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.
*/
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
*/
private function getEncodedImageFormat(string $saveFormat): string
{
return match (strtolower($saveFormat)) {
'png' => 'png',
'bmp', => 'bmp',
'gif' => 'gif',
'webp' => 'webp',
default => 'jpeg',
};
}
}