@@ -8,13 +8,10 @@ Explore how to integrate Intervention Image with Laravel and Symfony frameworks
8
8
9
9
Intervention Image can be easily integrated into a Laravel application with the
10
10
[ official integration package] ( https://github.com/Intervention/image-laravel ) . This package
11
- provides a Laravel service provider, facade and a publishable configuration
12
- file.
11
+ provides a Laravel service provider, facade, a publishable configuration
12
+ file and more .
13
13
14
- Although this integration is not mandatory, it has the advantage of integrating
15
- the configuration centrally in the application.
16
-
17
- ### Integration
14
+ ### Installation
18
15
19
16
Instead of installing the Intervention Image directly, it is only necessary to integrate
20
17
the ` intervention/image-laravel ` package. The corresponding base libraries are automatically
@@ -24,15 +21,23 @@ installed as well.
24
21
composer require intervention/image-laravel
25
22
```
26
23
27
- Next, add the configuration files to your application using the ` vendor:publish ` command:
24
+ ### Application-wide Configuration
25
+
26
+ The extension comes with a global configuration file that is recognized by
27
+ Laravel. It is therefore possible to store the settings for Intervention Image
28
+ once centrally and not have to define them individually each time you call the
29
+ image manager.
30
+
31
+ The configuration file can be copied to the application with the following command.
28
32
29
33
``` bash
30
34
php artisan vendor:publish --provider=" Intervention\Image\Laravel\ServiceProvider"
31
35
```
32
36
33
- This command will publish the configuration file ` image.php ` to your ` app/config `
34
- directory. In this file you can set the desired driver for Intervention Image.
35
- By default the library is configured to use GD library for image processing.
37
+ This command will publish the configuration file ` config/image.php ` . Here you
38
+ can set the desired driver and its configuration options for Intervention
39
+ Image. By default the library is configured to use GD library for image
40
+ processing.
36
41
37
42
The configuration files looks like this.
38
43
@@ -72,7 +77,6 @@ return [
72
77
|
73
78
| - "strip" controls if meta data like exif tags should be removed when
74
79
| encoding images.
75
-
76
80
*/
77
81
78
82
'options' => [
@@ -90,56 +94,56 @@ You can read more about the different options for
90
94
[ decoding animations] ( /v3/modifying/animations ) and
91
95
[ blending color] ( /v3/basics/colors#transparency ) .
92
96
93
- The integration is now complete and it is possible to access the
94
- [ ImageManager] ( /v3/basics/instantiation ) via Laravel's facade.
95
-
96
- ### Laravel Code Examples
97
+ ### Static Facade Interface
97
98
98
- #### Reading images from filesystem
99
+ This package also integrates access to Intervention Image's central entry
100
+ point, the ` ImageManager::class ` , via a static [ facade] ( https://laravel.com/docs/11.x/facades ) . The call provides access to the
101
+ centrally configured [ image manager] ( /v3/basics/instantiation ) via singleton pattern.
99
102
100
- The following example shows how to read an image file from the file system,
101
- encode it in Jpeg format and return it as an HTTP response.
103
+ The following code example shows how to read an image from an upload request
104
+ the image facade in a Laravel route and save it on disk with a random file
105
+ name.
102
106
103
107
``` php
108
+ use Illuminate\Http\Request;
104
109
use Illuminate\Support\Facades\Route;
105
110
use Illuminate\Support\Facades\Storage;
111
+ use Illuminate\Support\Str;
106
112
use Intervention\Image\Laravel\Facades\Image;
107
113
108
- Route::get('/', function () {
109
- $image = Image::read(Storage::get('example.jpg'))
110
- ->cover(400, 300 )
111
- ->toJpeg(quality: 65 );
114
+ Route::get('/', function (Request $request ) {
115
+ $upload = $request->file('image');
116
+ $image = Image::read($upload )
117
+ ->resize(300, 200 );
112
118
113
- return response((string) $image)
114
- ->header('Content-Type', $image->mediaType());
119
+ Storage::put(
120
+ Str::random() . '.' . $upload->getClientOriginalExtension(),
121
+ $image->encodeByExtension($upload->getClientOriginalExtension(), quality: 70)
122
+ );
115
123
});
116
124
```
117
125
118
- #### Reading image file uploads
126
+ ### Image Response Macro
119
127
120
- This example shows how to read an image as file upload, apply crop
121
- modifications by reading values of the HTTP request. Finally the image is
122
- encoded and stored with a random file name on disk .
128
+ Furthermore, the package includes a response macro that can be used to
129
+ elegantly encode an image resource and convert it to an HTTP response in a
130
+ single step .
123
131
124
- ** The example is for educational purposes only. Any external user input should be considered unsafe and must be validated.**
132
+ The following code example shows how to read an image from disk apply
133
+ modifications and use the image response macro to encode it and send the image
134
+ back to the user in one call. Only the first parameter is required.
125
135
126
136
``` php
127
- use Illuminate\Http\Request;
128
137
use Illuminate\Support\Facades\Route;
129
138
use Illuminate\Support\Facades\Storage;
130
- use Illuminate\Support\Str ;
139
+ use Intervention\Image\Format ;
131
140
use Intervention\Image\Laravel\Facades\Image;
132
141
133
- Route::post('/upload', function (Request $request) {
134
- $upload = $request->file('image');
135
-
136
- $image = Image::read($upload)
137
- ->crop(...$request->only('width', 'height', 'offset_x', 'offset_y'));
142
+ Route::get('/', function () {
143
+ $image = Image::read(Storage::get('example.jpg'))
144
+ ->scale(300, 200);
138
145
139
- Storage::put(
140
- Str::random() . '.' . $upload->getClientOriginalExtension(),
141
- $image->encodeByExtension($upload->getClientOriginalExtension(), quality: 70)
142
- );
146
+ return response()->image($image, Format::WEBP, quality: 65);
143
147
});
144
148
```
145
149
0 commit comments