Skip to content

Commit 09d782d

Browse files
authored
Feature/laravel response macro (#55)
* Add documentation for Laravel image response macro * Improve documentation on Laravel integration * Edit texts * Edit title
1 parent 29ff505 commit 09d782d

File tree

1 file changed

+44
-40
lines changed

1 file changed

+44
-40
lines changed

image/v3/introduction/frameworks.md

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ Explore how to integrate Intervention Image with Laravel and Symfony frameworks
88

99
Intervention Image can be easily integrated into a Laravel application with the
1010
[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.
1313

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
1815

1916
Instead of installing the Intervention Image directly, it is only necessary to integrate
2017
the `intervention/image-laravel` package. The corresponding base libraries are automatically
@@ -24,15 +21,23 @@ installed as well.
2421
composer require intervention/image-laravel
2522
```
2623

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.
2832

2933
```bash
3034
php artisan vendor:publish --provider="Intervention\Image\Laravel\ServiceProvider"
3135
```
3236

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.
3641

3742
The configuration files looks like this.
3843

@@ -72,7 +77,6 @@ return [
7277
|
7378
| - "strip" controls if meta data like exif tags should be removed when
7479
| encoding images.
75-
7680
*/
7781

7882
'options' => [
@@ -90,56 +94,56 @@ You can read more about the different options for
9094
[decoding animations](/v3/modifying/animations) and
9195
[blending color](/v3/basics/colors#transparency).
9296

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
9798

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.
99102

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.
102106

103107
```php
108+
use Illuminate\Http\Request;
104109
use Illuminate\Support\Facades\Route;
105110
use Illuminate\Support\Facades\Storage;
111+
use Illuminate\Support\Str;
106112
use Intervention\Image\Laravel\Facades\Image;
107113

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);
112118

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+
);
115123
});
116124
```
117125

118-
#### Reading image file uploads
126+
### Image Response Macro
119127

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.
123131

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.
125135

126136
```php
127-
use Illuminate\Http\Request;
128137
use Illuminate\Support\Facades\Route;
129138
use Illuminate\Support\Facades\Storage;
130-
use Illuminate\Support\Str;
139+
use Intervention\Image\Format;
131140
use Intervention\Image\Laravel\Facades\Image;
132141

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);
138145

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);
143147
});
144148
```
145149

0 commit comments

Comments
 (0)