Skip to content

Commit

Permalink
Add new Configuration entity.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Oct 24, 2022
1 parent 3342f3e commit b1cb72e
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 34 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,29 @@ How to use
----------

```php
$pdfPath = __DIR__ . '/example.pdf';
$imagePath = __DIR__ . '/example.jpg';
$configuration = new Configuration(
pdfPath: __DIR__ . '/example.pdf',
savePath: __DIR__ . '/example.jpg',
format: 'jpg'
);

// Render PDF to image and save to disk.
\Baraja\PdfToImage\Convertor::convert($pdfPath, $imagePath, 'jpg');
\Baraja\PdfToImage\Convertor::convert($configuration);
```

Supported configuration options
-------------------------------

| Name | Type | Default value |
|------------|------------|---------|
| `pdfPath` | `string` | |
| `savePath` | `string` | |
| `format` | `string` | `'jpg'` |
| `trim` | `bool` | `false` |
| `cols` | `int|null` | `null` |
| `rows` | `int|null` | `null` |
| `bestfit` | `bool` | `false` |

📄 License
-----------

Expand Down
36 changes: 36 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Baraja\PdfToImage;


final class Configuration
{
public function __construct(
public string $pdfPath,
public string $savePath,
public string $format = 'jpg',
public bool $trim = false,
public ?int $cols = null,
public ?int $rows = null,
public bool $bestfit = false
) {
$this->format = strtolower($format);
}


public static function from(
string $pdfPath,
?string $savePath = null,
string $format = 'jpg',
bool $trim = false,
): self {
return new self(
pdfPath: $pdfPath,
savePath: $savePath,
format: $format,
trim: $trim,
);
}
}
70 changes: 39 additions & 31 deletions src/Convertor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@

final class Convertor
{
public const FORMAT_JPG = 'jpg';
public const
FormatJpg = 'jpg',
FormatPng = 'png',
FormatGif = 'gif';

public const FORMAT_PNG = 'png';

public const FORMAT_GIF = 'gif';

public const SUPPORTED_FORMATS = [self::FORMAT_JPG, self::FORMAT_PNG, self::FORMAT_GIF];
public const SupportedFormats = [self::FormatJpg, self::FormatPng, self::FormatGif];


/** @throws \Error */
Expand All @@ -28,24 +27,28 @@ public function __construct()
*
* @throws ConvertorException
*/
public static function convert(string $pdfPath, string $savePath, string $format = 'jpg', bool $trim = false): void
{
if (\in_array($format = strtolower($format), self::SUPPORTED_FORMATS, true) === false) {
throw new \InvalidArgumentException(
'Format "' . $format . '" is not supported. '
. 'Did you mean "' . implode('", "', self::SUPPORTED_FORMATS) . '"?',
);
public static function convert(
string|Configuration $pdfPath,
?string $savePath = null,
string $format = 'jpg',
bool $trim = false
): void {
$configuration = is_string($pdfPath)
? Configuration::from($pdfPath, $savePath, $format, $trim)
: $pdfPath;

if (in_array($configuration->format, self::SupportedFormats, true) === false) {
throw new \InvalidArgumentException(sprintf(
'Format "%s" is not supported. Did you mean "%s"?',
$configuration->format,
implode('", "', self::SupportedFormats),
));
}
if (\is_file($pdfPath) === false) {
throw new ConvertorException('File "' . $pdfPath . '" does not exist.');
if (is_file($configuration->pdfPath) === false) {
throw new ConvertorException(sprintf('File "%s" does not exist.', $configuration->pdfPath));
}
try {
$im = self::process($pdfPath, $savePath);
if ($trim === true) {
$im->setImageBorderColor('rgb(255,255,255)');
$im->trimImage(1);
self::write($savePath, (string) $im);
}
self::process($configuration);
} catch (\ImagickException $e) {
throw new ConvertorException($e->getMessage(), $e->getCode(), $e);
}
Expand All @@ -55,17 +58,22 @@ public static function convert(string $pdfPath, string $savePath, string $format
/**
* @throws \ImagickException
*/
private static function process(string $pdfPath, string $savePath): \Imagick
private static function process(Configuration $configuration): void
{
if (class_exists('\Imagick') === false) {
throw new \RuntimeException('Imagick is not installed.');
}

$im = new \Imagick($pdfPath);
$im->setImageFormat('jpg');
self::write($savePath, (string) $im);

return $im;
$im = new \Imagick($configuration->pdfPath);
$im->setImageFormat($configuration->format);
if ($configuration->cols !== null && $configuration->rows !== null) {
$im->scaleImage($configuration->cols, $configuration->rows, $configuration->bestfit);
}
if ($configuration->trim) {
$im->setImageBorderColor('rgb(255,255,255)');
$im->trimImage(1);
}
self::write($configuration->savePath, (string) $im);
}


Expand All @@ -76,12 +84,12 @@ private static function process(string $pdfPath, string $savePath): \Imagick
*/
private static function write(string $file, string $content, ?int $mode = 0_666): void
{
static::createDir(dirname($file));
self::createDir(dirname($file));
if (@file_put_contents($file, $content) === false) { // @ is escalated to exception
throw new ConvertorException('Unable to write file "' . $file . '": ' . self::getLastError());
throw new ConvertorException(sprintf('Unable to write file "%s": %s', $file, self::getLastError()));
}
if ($mode !== null && !@chmod($file, $mode)) { // @ is escalated to exception
throw new ConvertorException('Unable to chmod file "' . $file . '": ' . self::getLastError());
throw new ConvertorException(sprintf('Unable to chmod file "%s": %s', $file, self::getLastError()));
}
}

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

Expand Down

0 comments on commit b1cb72e

Please sign in to comment.