Skip to content
Merged
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
42 changes: 32 additions & 10 deletions app/Conversion/Drivers/Image/Concerns/RendersSingleImagePdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,26 @@
use App\Support\Conversions\DTO\ConversionResult;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\ImageManager;
use RuntimeException;

trait RendersSingleImagePdf
{
private function renderPdf(ConversionContext $context, string $imageMimeType): ConversionResult
{
$sourceContent = Storage::disk('local')->get($context->sourceFile->stored_path);
$storedPath = $context->sourceFile->stored_path;

if (! Storage::disk('local')->exists($storedPath)) {
throw new RuntimeException(
"RendersSingleImagePdf: source file not found at [{$storedPath}]."
);
}

$sourceContent = Storage::disk('local')->get($storedPath);
$imageDataUri = "data:{$imageMimeType};base64,".base64_encode($sourceContent);

$pageSize = $context->options['page_size'] ?? 'a4';
$pageSize = $this->resolvePageSize($context->options['page_size'] ?? 'a4');
$orientation = $this->resolveOrientation($context->options['orientation'] ?? 'auto', $sourceContent);
$margin = $this->resolveMargin($context->options['margin'] ?? 'small');
$fitMode = $context->options['fit_mode'] ?? 'contain';
Expand All @@ -37,22 +48,33 @@ private function renderPdf(ConversionContext $context, string $imageMimeType): C
);
}

private function resolvePageSize(string $pageSize): string
{
if ($pageSize === 'auto' || $pageSize === '') {
return 'a4';
}

return $pageSize;
}

private function resolveOrientation(string $orientation, string $imageContent): string
{
if ($orientation !== 'auto') {
return $orientation;
}

$image = imagecreatefromstring($imageContent);
if ($image === false) {
return 'portrait';
}
$manager = new ImageManager(new Driver);

$width = imagesx($image);
$height = imagesy($image);
imagedestroy($image);
try {
$image = $manager->decodeBinary($imageContent);
} catch (\Throwable $e) {
throw new RuntimeException(
"RendersSingleImagePdf: could not decode image binary to determine orientation. {$e->getMessage()}",
previous: $e,
);
}

return $width > $height ? 'landscape' : 'portrait';
return $image->width() > $image->height() ? 'landscape' : 'portrait';
}

private function resolveMargin(string $margin): string
Expand Down
19 changes: 19 additions & 0 deletions app/Conversion/Drivers/Image/Concerns/ResolvesImageQuality.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace App\Conversion\Drivers\Image\Concerns;

trait ResolvesImageQuality
{
private function resolveQuality(mixed $quality): int
{
return match ($quality) {
'low' => 60,
'medium' => 75,
'high' => 90,
'max' => 100,
default => 85,
};
}
}
13 changes: 10 additions & 3 deletions app/Conversion/Drivers/Image/JpgToPngDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\ImageManager;
use RuntimeException;

final class JpgToPngDriver implements ConverterDriver
{
Expand All @@ -20,10 +21,16 @@ public function key(): string

public function convert(ConversionContext $context): ConversionResult
{
$manager = new ImageManager(new Driver);
$storedPath = $context->sourceFile->stored_path;

if (! Storage::disk('local')->exists($storedPath)) {
throw new RuntimeException(
"JpgToPngDriver: source file not found at [{$storedPath}]."
);
}

$sourcePath = Storage::disk('local')->path($context->sourceFile->stored_path);
$image = $manager->decodePath($sourcePath);
$manager = new ImageManager(new Driver);
$image = $manager->decodePath(Storage::disk('local')->path($storedPath));

$encoded = $image->encodeUsingFileExtension('png');

Expand Down
27 changes: 13 additions & 14 deletions app/Conversion/Drivers/Image/JpgToWebpDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,36 @@

namespace App\Conversion\Drivers\Image;

use App\Conversion\Drivers\Image\Concerns\ResolvesImageQuality;
use App\Support\Conversions\Contracts\ConverterDriver;
use App\Support\Conversions\DTO\ConversionContext;
use App\Support\Conversions\DTO\ConversionResult;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\ImageManager;
use RuntimeException;

final class JpgToWebpDriver implements ConverterDriver
{
use ResolvesImageQuality;

public function key(): string
{
return 'jpg_to_webp';
}

public function convert(ConversionContext $context): ConversionResult
{
$manager = new ImageManager(new Driver);
$storedPath = $context->sourceFile->stored_path;

$sourcePath = Storage::disk('local')->path($context->sourceFile->stored_path);
$image = $manager->decodePath($sourcePath);
if (! Storage::disk('local')->exists($storedPath)) {
throw new RuntimeException(
"JpgToWebpDriver: source file not found at [{$storedPath}]."
);
}

$manager = new ImageManager(new Driver);
$image = $manager->decodePath(Storage::disk('local')->path($storedPath));

$quality = $this->resolveQuality($context->options['quality'] ?? 'high');

Expand All @@ -40,15 +50,4 @@ public function convert(ConversionContext $context): ConversionResult
sizeBytes: Storage::disk('local')->size($outputPath),
);
}

private function resolveQuality(mixed $quality): int
{
return match ($quality) {
'low' => 60,
'medium' => 75,
'high' => 90,
'max' => 100,
default => 85,
};
}
}
27 changes: 13 additions & 14 deletions app/Conversion/Drivers/Image/PngToJpgDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,36 @@

namespace App\Conversion\Drivers\Image;

use App\Conversion\Drivers\Image\Concerns\ResolvesImageQuality;
use App\Support\Conversions\Contracts\ConverterDriver;
use App\Support\Conversions\DTO\ConversionContext;
use App\Support\Conversions\DTO\ConversionResult;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\ImageManager;
use RuntimeException;

final class PngToJpgDriver implements ConverterDriver
{
use ResolvesImageQuality;

public function key(): string
{
return 'png_to_jpg';
}

public function convert(ConversionContext $context): ConversionResult
{
$manager = new ImageManager(new Driver);
$storedPath = $context->sourceFile->stored_path;

$sourcePath = Storage::disk('local')->path($context->sourceFile->stored_path);
$image = $manager->decodePath($sourcePath);
if (! Storage::disk('local')->exists($storedPath)) {
throw new RuntimeException(
"PngToJpgDriver: source file not found at [{$storedPath}]."
);
}

$manager = new ImageManager(new Driver);
$image = $manager->decodePath(Storage::disk('local')->path($storedPath));

$background = $context->options['background'] ?? '#ffffff';
$quality = $this->resolveQuality($context->options['quality'] ?? 'high');
Expand All @@ -43,15 +53,4 @@ public function convert(ConversionContext $context): ConversionResult
sizeBytes: Storage::disk('local')->size($outputPath),
);
}

private function resolveQuality(mixed $quality): int
{
return match ($quality) {
'low' => 60,
'medium' => 75,
'high' => 90,
'max' => 100,
default => 85,
};
}
}
27 changes: 13 additions & 14 deletions app/Conversion/Drivers/Image/PngToWebpDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,36 @@

namespace App\Conversion\Drivers\Image;

use App\Conversion\Drivers\Image\Concerns\ResolvesImageQuality;
use App\Support\Conversions\Contracts\ConverterDriver;
use App\Support\Conversions\DTO\ConversionContext;
use App\Support\Conversions\DTO\ConversionResult;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\ImageManager;
use RuntimeException;

final class PngToWebpDriver implements ConverterDriver
{
use ResolvesImageQuality;

public function key(): string
{
return 'png_to_webp';
}

public function convert(ConversionContext $context): ConversionResult
{
$manager = new ImageManager(new Driver);
$storedPath = $context->sourceFile->stored_path;

$sourcePath = Storage::disk('local')->path($context->sourceFile->stored_path);
$image = $manager->decodePath($sourcePath);
if (! Storage::disk('local')->exists($storedPath)) {
throw new RuntimeException(
"PngToWebpDriver: source file not found at [{$storedPath}]."
);
}

$manager = new ImageManager(new Driver);
$image = $manager->decodePath(Storage::disk('local')->path($storedPath));

$quality = $this->resolveQuality($context->options['quality'] ?? 'high');

Expand All @@ -40,15 +50,4 @@ public function convert(ConversionContext $context): ConversionResult
sizeBytes: Storage::disk('local')->size($outputPath),
);
}

private function resolveQuality(mixed $quality): int
{
return match ($quality) {
'low' => 60,
'medium' => 75,
'high' => 90,
'max' => 100,
default => 85,
};
}
}
41 changes: 15 additions & 26 deletions resources/views/pdf/single-image.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,24 @@
<head>
<meta charset="UTF-8">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
* { margin: 0; padding: 0; }
body { margin: {{ $margin }}; }
.image-container {
width: 100%;
height: 100%;
@if ($fitMode === 'contain')
display: flex;
align-items: center;
justify-content: center;
@endif
}
img {
@if ($fitMode === 'contain')
max-width: 100%;
max-height: 100%;
object-fit: contain;
@elseif ($fitMode === 'fill')
width: 100%;
height: 100%;
object-fit: fill;
@else
display: block;
@endif
}
@if ($fitMode === 'contain')
table { width: 100%; height: 100%; border-collapse: collapse; }
td { text-align: center; vertical-align: middle; }
img { max-width: 100%; max-height: 100%; }
@elseif ($fitMode === 'cover')
img { width: 100%; height: 100%; }
@else
img { display: block; }
@endif
</style>
</head>
<body>
<div class="image-container">
<img src="{{ $imageDataUri }}" alt="">
</div>
@if ($fitMode === 'contain')
<table><tr><td><img src="{{ $imageDataUri }}" alt=""></td></tr></table>
@else
<img src="{{ $imageDataUri }}" alt="">
@endif
</body>
</html>
1 change: 0 additions & 1 deletion tests/Feature/Conversion/Drivers/JpgToPdfDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
$sourcePath = ImageFixture::jpg('source.jpg', width: 600, height: 400);
$context = ConversionContextFactory::forSourcePath(
sourcePath: $sourcePath,
outputExtension: 'pdf',
options: [
'page_size' => 'a4',
'orientation' => 'auto',
Expand Down
1 change: 0 additions & 1 deletion tests/Feature/Conversion/Drivers/JpgToPngDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
$sourcePath = ImageFixture::jpg('source.jpg', width: 600, height: 400);
$context = ConversionContextFactory::forSourcePath(
sourcePath: $sourcePath,
outputExtension: 'png',
options: [
'resize' => 'original',
'remove_metadata' => true,
Expand Down
1 change: 0 additions & 1 deletion tests/Feature/Conversion/Drivers/JpgToWebpDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
$sourcePath = ImageFixture::jpg('source.jpg', width: 600, height: 400);
$context = ConversionContextFactory::forSourcePath(
sourcePath: $sourcePath,
outputExtension: 'webp',
options: [
'quality' => 'high',
'resize' => 'original',
Expand Down
1 change: 0 additions & 1 deletion tests/Feature/Conversion/Drivers/PngToJpgDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
$sourcePath = ImageFixture::png('source.png', width: 600, height: 400);
$context = ConversionContextFactory::forSourcePath(
sourcePath: $sourcePath,
outputExtension: 'jpg',
options: [
'quality' => 'high',
'background' => '#ffffff',
Expand Down
1 change: 0 additions & 1 deletion tests/Feature/Conversion/Drivers/PngToPdfDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
$sourcePath = ImageFixture::png('source.png', width: 600, height: 400);
$context = ConversionContextFactory::forSourcePath(
sourcePath: $sourcePath,
outputExtension: 'pdf',
options: [
'page_size' => 'a4',
'orientation' => 'auto',
Expand Down
1 change: 0 additions & 1 deletion tests/Feature/Conversion/Drivers/PngToWebpDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
$sourcePath = ImageFixture::png('source.png', width: 600, height: 400);
$context = ConversionContextFactory::forSourcePath(
sourcePath: $sourcePath,
outputExtension: 'webp',
options: [
'quality' => 'high',
'resize' => 'original',
Expand Down
1 change: 0 additions & 1 deletion tests/Support/ConversionContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ final class ConversionContextFactory
*/
public static function forSourcePath(
string $sourcePath,
string $outputExtension,
array $options = [],
): ConversionContext {
$sourceFile = new FileRecord;
Expand Down