Skip to content

Commit b1cb72e

Browse files
committed
Add new Configuration entity.
1 parent 3342f3e commit b1cb72e

File tree

3 files changed

+94
-34
lines changed

3 files changed

+94
-34
lines changed

README.md

+19-3
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,29 @@ How to use
2222
----------
2323

2424
```php
25-
$pdfPath = __DIR__ . '/example.pdf';
26-
$imagePath = __DIR__ . '/example.jpg';
25+
$configuration = new Configuration(
26+
pdfPath: __DIR__ . '/example.pdf',
27+
savePath: __DIR__ . '/example.jpg',
28+
format: 'jpg'
29+
);
2730

2831
// Render PDF to image and save to disk.
29-
\Baraja\PdfToImage\Convertor::convert($pdfPath, $imagePath, 'jpg');
32+
\Baraja\PdfToImage\Convertor::convert($configuration);
3033
```
3134

35+
Supported configuration options
36+
-------------------------------
37+
38+
| Name | Type | Default value |
39+
|------------|------------|---------|
40+
| `pdfPath` | `string` | |
41+
| `savePath` | `string` | |
42+
| `format` | `string` | `'jpg'` |
43+
| `trim` | `bool` | `false` |
44+
| `cols` | `int|null` | `null` |
45+
| `rows` | `int|null` | `null` |
46+
| `bestfit` | `bool` | `false` |
47+
3248
📄 License
3349
-----------
3450

src/Configuration.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Baraja\PdfToImage;
6+
7+
8+
final class Configuration
9+
{
10+
public function __construct(
11+
public string $pdfPath,
12+
public string $savePath,
13+
public string $format = 'jpg',
14+
public bool $trim = false,
15+
public ?int $cols = null,
16+
public ?int $rows = null,
17+
public bool $bestfit = false
18+
) {
19+
$this->format = strtolower($format);
20+
}
21+
22+
23+
public static function from(
24+
string $pdfPath,
25+
?string $savePath = null,
26+
string $format = 'jpg',
27+
bool $trim = false,
28+
): self {
29+
return new self(
30+
pdfPath: $pdfPath,
31+
savePath: $savePath,
32+
format: $format,
33+
trim: $trim,
34+
);
35+
}
36+
}

src/Convertor.php

+39-31
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77

88
final class Convertor
99
{
10-
public const FORMAT_JPG = 'jpg';
10+
public const
11+
FormatJpg = 'jpg',
12+
FormatPng = 'png',
13+
FormatGif = 'gif';
1114

12-
public const FORMAT_PNG = 'png';
13-
14-
public const FORMAT_GIF = 'gif';
15-
16-
public const SUPPORTED_FORMATS = [self::FORMAT_JPG, self::FORMAT_PNG, self::FORMAT_GIF];
15+
public const SupportedFormats = [self::FormatJpg, self::FormatPng, self::FormatGif];
1716

1817

1918
/** @throws \Error */
@@ -28,24 +27,28 @@ public function __construct()
2827
*
2928
* @throws ConvertorException
3029
*/
31-
public static function convert(string $pdfPath, string $savePath, string $format = 'jpg', bool $trim = false): void
32-
{
33-
if (\in_array($format = strtolower($format), self::SUPPORTED_FORMATS, true) === false) {
34-
throw new \InvalidArgumentException(
35-
'Format "' . $format . '" is not supported. '
36-
. 'Did you mean "' . implode('", "', self::SUPPORTED_FORMATS) . '"?',
37-
);
30+
public static function convert(
31+
string|Configuration $pdfPath,
32+
?string $savePath = null,
33+
string $format = 'jpg',
34+
bool $trim = false
35+
): void {
36+
$configuration = is_string($pdfPath)
37+
? Configuration::from($pdfPath, $savePath, $format, $trim)
38+
: $pdfPath;
39+
40+
if (in_array($configuration->format, self::SupportedFormats, true) === false) {
41+
throw new \InvalidArgumentException(sprintf(
42+
'Format "%s" is not supported. Did you mean "%s"?',
43+
$configuration->format,
44+
implode('", "', self::SupportedFormats),
45+
));
3846
}
39-
if (\is_file($pdfPath) === false) {
40-
throw new ConvertorException('File "' . $pdfPath . '" does not exist.');
47+
if (is_file($configuration->pdfPath) === false) {
48+
throw new ConvertorException(sprintf('File "%s" does not exist.', $configuration->pdfPath));
4149
}
4250
try {
43-
$im = self::process($pdfPath, $savePath);
44-
if ($trim === true) {
45-
$im->setImageBorderColor('rgb(255,255,255)');
46-
$im->trimImage(1);
47-
self::write($savePath, (string) $im);
48-
}
51+
self::process($configuration);
4952
} catch (\ImagickException $e) {
5053
throw new ConvertorException($e->getMessage(), $e->getCode(), $e);
5154
}
@@ -55,17 +58,22 @@ public static function convert(string $pdfPath, string $savePath, string $format
5558
/**
5659
* @throws \ImagickException
5760
*/
58-
private static function process(string $pdfPath, string $savePath): \Imagick
61+
private static function process(Configuration $configuration): void
5962
{
6063
if (class_exists('\Imagick') === false) {
6164
throw new \RuntimeException('Imagick is not installed.');
6265
}
6366

64-
$im = new \Imagick($pdfPath);
65-
$im->setImageFormat('jpg');
66-
self::write($savePath, (string) $im);
67-
68-
return $im;
67+
$im = new \Imagick($configuration->pdfPath);
68+
$im->setImageFormat($configuration->format);
69+
if ($configuration->cols !== null && $configuration->rows !== null) {
70+
$im->scaleImage($configuration->cols, $configuration->rows, $configuration->bestfit);
71+
}
72+
if ($configuration->trim) {
73+
$im->setImageBorderColor('rgb(255,255,255)');
74+
$im->trimImage(1);
75+
}
76+
self::write($configuration->savePath, (string) $im);
6977
}
7078

7179

@@ -76,12 +84,12 @@ private static function process(string $pdfPath, string $savePath): \Imagick
7684
*/
7785
private static function write(string $file, string $content, ?int $mode = 0_666): void
7886
{
79-
static::createDir(dirname($file));
87+
self::createDir(dirname($file));
8088
if (@file_put_contents($file, $content) === false) { // @ is escalated to exception
81-
throw new ConvertorException('Unable to write file "' . $file . '": ' . self::getLastError());
89+
throw new ConvertorException(sprintf('Unable to write file "%s": %s', $file, self::getLastError()));
8290
}
8391
if ($mode !== null && !@chmod($file, $mode)) { // @ is escalated to exception
84-
throw new ConvertorException('Unable to chmod file "' . $file . '": ' . self::getLastError());
92+
throw new ConvertorException(sprintf('Unable to chmod file "%s": %s', $file, self::getLastError()));
8593
}
8694
}
8795

@@ -94,7 +102,7 @@ private static function write(string $file, string $content, ?int $mode = 0_666)
94102
private static function createDir(string $dir, int $mode = 0_777): void
95103
{
96104
if (!is_dir($dir) && !@mkdir($dir, $mode, true) && !is_dir($dir)) { // @ - dir may already exist
97-
throw new ConvertorException('Unable to create directory "' . $dir . '": ' . self::getLastError());
105+
throw new ConvertorException(sprintf('Unable to create directory "%s": %s', $dir, self::getLastError()));
98106
}
99107
}
100108

0 commit comments

Comments
 (0)