Skip to content

Commit 71eed45

Browse files
committed
Added method to convert Uploaded images
1 parent 08e726d commit 71eed45

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

Diff for: src/Image/ImageEngine.php

+44
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace le0daniel\Laravel\ImageEngine\Image;
1010

11+
use Illuminate\Support\Str;
1112
use Intervention\Image\Constraint;
1213
use Intervention\Image\ImageManager;
1314
use le0daniel\Laravel\ImageEngine\Utility\Directories;
@@ -17,16 +18,20 @@
1718

1819
final class ImageEngine
1920
{
21+
private const IMAGE_RESIZE_DIMENSIONS_TO_FIT = 3000;
22+
2023
private array $sizes;
2124
private array $filters = [];
2225
private string $secret;
26+
private string $tmpPath;
2327
private ImageManager $imageManager;
2428

2529
public function __construct(ImageManager $imageManager)
2630
{
2731
$this->imageManager = $imageManager;
2832
$this->sizes = config('image-engine.sizes');
2933
$this->secret = config('image-engine.key');
34+
$this->tmpPath = rtrim(config('image-engine.tmp_directory'), '/');
3035
}
3136

3237
private function getBasePath(ImageRepresentation $imageRepresentation, bool $forceConfidential): string
@@ -93,6 +98,12 @@ private function getAbsoluteRenderPath(
9398
return "{$basePath}/{$folder}/{$path}.{$extension}";
9499
}
95100

101+
private function getTmpPath(string $extension)
102+
{
103+
$randomName = Str::random(20);
104+
return "{$this->tmpPath}/{$randomName}.{$extension}";
105+
}
106+
96107
public function serializeImage(ImageRepresentation $imageRepresentation): array
97108
{
98109
$signature = Signatures::sign($this->secret, $imageRepresentation->serialize());
@@ -169,4 +180,37 @@ static function (Constraint $constraint) {
169180
return $absoluteRenderPath;
170181
}
171182

183+
public function convertUploadedImage(
184+
string $absoluteImagePath,
185+
string $extension,
186+
\Closure $callback,
187+
int $imageDimensions = self::IMAGE_RESIZE_DIMENSIONS_TO_FIT
188+
): void {
189+
Images::verifyImageExtension($extension);
190+
try {
191+
$tmpFileName = $this->getTmpPath($extension);
192+
193+
$image = $this->imageManager->make($absoluteImagePath);
194+
$image->resize(
195+
$imageDimensions,
196+
$imageDimensions,
197+
static function ($constraint) {
198+
$constraint->aspectRatio();
199+
$constraint->upsize();
200+
}
201+
);
202+
$image->save($tmpFileName);
203+
unset($image);
204+
205+
$callback($tmpFileName);
206+
} catch (\Exception $exception) {
207+
if (file_exists($tmpFileName)) {
208+
unlink($tmpFileName);
209+
}
210+
throw $exception;
211+
}
212+
213+
unlink($tmpFileName);
214+
}
215+
172216
}

Diff for: src/config/image-engine.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,9 @@
6565
*/
6666
'original_cache_dir' => storage_path('app/original-images'),
6767

68-
];
68+
/**
69+
* Path used to create tmp files.
70+
*/
71+
'tmp_directory' => storage_path('tmp'),
72+
73+
];

0 commit comments

Comments
 (0)