-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathImageManager.php
More file actions
151 lines (131 loc) · 4.02 KB
/
Copy pathImageManager.php
File metadata and controls
151 lines (131 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
namespace Illuminate\Image;
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
use Illuminate\Contracts\Image\Driver;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Http\Client\Factory as HttpFactory;
use Illuminate\Http\UploadedFile;
use Illuminate\Image\Drivers\GdDriver;
use Illuminate\Image\Drivers\ImagickDriver;
use Illuminate\Support\Manager;
use InvalidArgumentException;
class ImageManager extends Manager
{
/**
* The registered transformation handlers.
*
* @var array<string, array<class-string<\Illuminate\Contracts\Image\Transformation>, callable>>
*/
protected array $transformationHandlers = [];
/**
* Create an image instance from raw bytes.
*/
public function fromBytes(string $contents): Image
{
return new Image($contents);
}
/**
* Create an image instance from a base64 encoded string.
*/
public function fromBase64(string $base64): Image
{
return new Image(
fn () => base64_decode($base64, true) ?: throw new ImageException('Invalid base64 image data.'),
);
}
/**
* Create an image instance from a file path.
*/
public function fromPath(string $path): Image
{
return new Image(
fn () => $this->container->make(Filesystem::class)->get($path),
);
}
/**
* Create an image instance from a storage disk path.
*/
public function fromStorage(string $path, ?string $disk = null): Image
{
return new Image(
fn () => $this->container->make(FilesystemFactory::class)->disk($disk)->get($path),
);
}
/**
* Create an image instance from an uploaded file.
*/
public function fromUpload(UploadedFile $file): Image
{
return new Image(fn () => $file->getContent(), $file);
}
/**
* Create an image instance from a URL.
*/
public function fromUrl(string $url): Image
{
return new Image(
fn () => $this->container->make(HttpFactory::class)->get($url)->body(),
);
}
/**
* Create a new driver instance.
*
* @throws InvalidArgumentException
*/
protected function createDriver($driver): Driver
{
try {
$instance = parent::createDriver($driver);
} catch (InvalidArgumentException $e) {
throw new InvalidArgumentException("Image driver [{$driver}] is not supported.", 0, $e);
}
if (method_exists($instance, 'ensureRequirementsAreMet')) {
$instance->ensureRequirementsAreMet();
}
$this->applyTransformationHandlers($driver, $instance);
return $instance;
}
/**
* Create the GD image driver.
*/
protected function createGdDriver(): GdDriver
{
return new GdDriver;
}
/**
* Create the Imagick image driver.
*/
protected function createImagickDriver(): ImagickDriver
{
return new ImagickDriver;
}
/**
* Register a transformation handler for the given driver.
*
* @param class-string<\Illuminate\Contracts\Image\Transformation> $transformation
*/
public function transformUsing(string $driver, string $transformation, callable $callback): static
{
$this->transformationHandlers[$driver][$transformation] = $callback;
if (isset($this->drivers[$driver])) {
$this->applyTransformationHandlers($driver, $this->drivers[$driver]);
}
return $this;
}
/**
* Apply registered transformation handlers to the given driver instance.
*/
protected function applyTransformationHandlers(string $driver, Driver $instance): void
{
foreach ($this->transformationHandlers[$driver] ?? [] as $transformation => $callback) {
$instance->transformUsing($transformation, $callback);
}
}
/**
* Get the default image driver name.
*/
public function getDefaultDriver(): string
{
return $this->config->get('image.default', 'gd');
}
}