-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExtractedImage.php
More file actions
127 lines (112 loc) · 3.76 KB
/
Copy pathExtractedImage.php
File metadata and controls
127 lines (112 loc) · 3.76 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
declare(strict_types=1);
namespace Mindee\Extraction;
use Mindee\Dependency\DependencyChecker;
use Mindee\Error\MindeeUnhandledException;
use Mindee\Input\BytesInput;
use Imagick;
use ImagickException;
use function in_array;
use const DIRECTORY_SEPARATOR;
/**
* An extracted sub-image.
*/
class ExtractedImage
{
/**
* @var Imagick Wrapper for the image.
*/
public Imagick $image;
/**
* @var string Name of the file.
*/
public string $filename;
/**
* @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 Imagick $image The extracted image.
* @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(Imagick $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 null|string $format The image format to use. Defaults to the save format if not provided.
* @param integer $quality Quality of the saved image.
*
* @throws ImagickException Throws if the image can't be processed.
*/
public function writeToFile(string $outputPath, ?string $format = null, int $quality = 100): void
{
$imagePath = $outputPath . DIRECTORY_SEPARATOR . $this->filename;
$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', (string) $finalQuality);
} elseif (in_array($format, ['jpg', 'jpeg'], true)) {
$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.
*
* @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 Encoded image format.
*/
private function getEncodedImageFormat(string $saveFormat): string
{
return match (strtolower($saveFormat)) {
'png' => 'png',
'bmp', => 'bmp',
'gif' => 'gif',
'webp' => 'webp',
default => 'jpeg',
};
}
}