Skip to content

Commit 04f3365

Browse files
committed
Added new Utilities
1 parent ae157d1 commit 04f3365

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

src/Utility/Directories.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace le0daniel\Laravel\ImageEngine\Utility;
4+
5+
final class Directories
6+
{
7+
8+
public static function makeRecursive(string $directory): void
9+
{
10+
if (file_exists($directory)) {
11+
return;
12+
}
13+
14+
if (!mkdir($directory, 0777, true) && !is_dir($directory)) {
15+
throw new \RuntimeException(sprintf('Directory "%s" was not created', $directory));
16+
}
17+
}
18+
19+
}

src/Utility/Images.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace le0daniel\Laravel\ImageEngine\Utility;
4+
5+
use Illuminate\Contracts\Filesystem\Filesystem;
6+
use Illuminate\Support\Facades\Storage;
7+
use le0daniel\Laravel\ImageEngine\Image\ImageException;
8+
use le0daniel\Laravel\ImageEngine\Image\ImageRepresentation;
9+
use League\Flysystem\Adapter\Local;
10+
11+
final class Images
12+
{
13+
public const ORIGINAL_SIZE = 'original';
14+
15+
static protected $imageExtensions = [
16+
'png',
17+
'jpg',
18+
'jpeg',
19+
];
20+
21+
public static function verifyImageExtension(string $requestedFormat)
22+
{
23+
if (!in_array($requestedFormat, self::$imageExtensions, true)) {
24+
throw ImageException::withHint(
25+
'Invalid image extension given.',
26+
"Available extensions: " . implode(', ', self::$imageExtensions)
27+
);
28+
}
29+
}
30+
31+
public static function isOriginalSize(string $size): bool
32+
{
33+
return self::ORIGINAL_SIZE === $size;
34+
}
35+
36+
private static function isLocalDisk(Filesystem $disk): bool
37+
{
38+
return $disk->getDriver()->getAdapter() instanceof Local;
39+
}
40+
41+
public static function realPath(ImageRepresentation $imageRepresentation): string
42+
{
43+
$disk = $imageRepresentation->disk();
44+
45+
// Return path from local disk
46+
if (self::isLocalDisk($disk)) {
47+
$localPathPrefix = rtrim($disk->getDriver()->getAdapter()->getPathPrefix(), '/');
48+
return realpath("{$localPathPrefix}/{$imageRepresentation->filePath}");
49+
}
50+
51+
$baseCachePath = config('image-engine.original_cache_dir');
52+
[$folder, $name] = Strings::splitAtIndex(md5($imageRepresentation->filePath), 6);
53+
$cacheFilePath = "{$baseCachePath}/{$folder}/{$name}.{$imageRepresentation->extension}";
54+
55+
Directories::makeRecursive(dirname($cacheFilePath));
56+
57+
// Download and cache the file
58+
if (!file_exists($cacheFilePath)) {
59+
Resources::auto(
60+
fn($resource) => $disk->writeStream($imageRepresentation->filePath, $resource),
61+
'w+',
62+
$cacheFilePath
63+
);
64+
}
65+
66+
return realpath($cacheFilePath);
67+
}
68+
69+
}

src/Utility/Resources.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace le0daniel\Laravel\ImageEngine\Utility;
6+
7+
final class Resources
8+
{
9+
10+
/**
11+
* @param string $path
12+
* @param string $mode
13+
* @return resource
14+
*/
15+
public static function open(string $path, string $mode)
16+
{
17+
$resource = fopen($path, $mode);
18+
if (!is_resource($resource)) {
19+
throw new \RuntimeException("Could not open resource {$path} in mode: {$mode}");
20+
}
21+
22+
return $resource;
23+
}
24+
25+
public static function close(...$resources): void
26+
{
27+
foreach ($resources as $resource) {
28+
if (is_resource($resource)) {
29+
fclose($resource);
30+
}
31+
}
32+
}
33+
34+
public static function auto(\Closure $closure, string $mode, string ...$paths)
35+
{
36+
$resources = array_map(fn(string $path) => self::open($path, $mode), $paths);
37+
$closure(...$resources);
38+
self::close(...$resources);
39+
}
40+
41+
}

src/Utility/Strings.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace le0daniel\Laravel\ImageEngine\Utility;
6+
7+
final class Strings
8+
{
9+
10+
/**
11+
* @param string $string
12+
* @param int $index
13+
* @return string[]
14+
*/
15+
public static function splitAtIndex(string $string, int $index): array
16+
{
17+
return [
18+
substr($string, 0, $index),
19+
$part2 = substr($string, $index),
20+
];
21+
}
22+
23+
}

0 commit comments

Comments
 (0)