Initial commit

This commit is contained in:
Ahrom
2025-11-16 12:43:07 +03:30
commit 4bbe56b83f
16778 changed files with 1914371 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2024-present Oliver Vogel
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,168 @@
# Intervention Image Laravel
## Laravel Integration for Intervention Image
[![Latest Version](https://img.shields.io/packagist/v/intervention/image-laravel.svg)](https://packagist.org/packages/intervention/image-laravel)
[![Tests](https://github.com/Intervention/image-laravel/actions/workflows/build.yml/badge.svg)](https://github.com/Intervention/image-laravel/actions/workflows/build.yml)
[![Monthly Downloads](https://img.shields.io/packagist/dm/intervention/image-laravel.svg)](https://packagist.org/packages/intervention/image-laravel/stats)
[![Support me on Ko-fi](https://raw.githubusercontent.com/Intervention/image-laravel/main/.github/images/support.svg)](https://ko-fi.com/interventionphp)
This package provides an integration to setup [Intervention
Image](https://image.intervention.io) easily to your Laravel application.
Included are a Laravel service provider, facade and a publishable configuration
file.
## Requirements
- Laravel >= 8
## Installation
In your existing Laravel application you can install this package using [Composer](https://getcomposer.org).
```bash
composer require intervention/image-laravel
```
## Features
Although Intervention Image can be used with Laravel without this extension,
this intergration package includes the following features that make image
interaction with the framework much easier.
### Application-wide configuration
The extension comes with a global configuration file that is recognized by
Laravel. It is therefore possible to store the settings for Intervention Image
once centrally and not have to define them individually each time you call the
image manager.
The configuration file can be copied to the application with the following command.
```bash
php artisan vendor:publish --provider="Intervention\Image\Laravel\ServiceProvider"
```
This command will publish the configuration file `config/image.php`. Here you
can set the desired driver and its configuration options for Intervention
Image. By default the library is configured to use GD library for image
processing.
The configuration files looks like this.
```php
return [
/*
|--------------------------------------------------------------------------
| Image Driver
|--------------------------------------------------------------------------
|
| Intervention Image supports “GD Library” and “Imagick” to process images
| internally. Depending on your PHP setup, you can choose one of them.
|
| Included options:
| - \Intervention\Image\Drivers\Gd\Driver::class
| - \Intervention\Image\Drivers\Imagick\Driver::class
|
*/
'driver' => \Intervention\Image\Drivers\Gd\Driver::class,
/*
|--------------------------------------------------------------------------
| Configuration Options
|--------------------------------------------------------------------------
|
| These options control the behavior of Intervention Image.
|
| - "autoOrientation" controls whether an imported image should be
| automatically rotated according to any existing Exif data.
|
| - "decodeAnimation" decides whether a possibly animated image is
| decoded as such or whether the animation is discarded.
|
| - "blendingColor" Defines the default blending color.
|
| - "strip" controls if meta data like exif tags should be removed when
| encoding images.
*/
'options' => [
'autoOrientation' => true,
'decodeAnimation' => true,
'blendingColor' => 'ffffff',
'strip' => false,
]
];
```
You can read more about the different options for
[driver selection](https://image.intervention.io/v3/basics/image-manager#driver-selection), setting options for
[auto orientation](https://image.intervention.io/v3/modifying/effects#image-orientation-according-to-exif-data),
[decoding animations](https://image.intervention.io/v3/modifying/animations) and
[blending color](https://image.intervention.io/v3/basics/colors#transparency).
### Static Facade Interface
This package also integrates access to Intervention Image's central entry
point, the `ImageManager::class`, via a static [facade](https://laravel.com/docs/11.x/facades). The call provides access to the
centrally configured [image manager](https://image.intervention.io/v3/basics/instantiation) via singleton pattern.
The following code example shows how to read an image from an upload request
the image facade in a Laravel route and save it on disk with a random file
name.
```php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\Laravel\Facades\Image;
Route::get('/', function (Request $request) {
$upload = $request->file('image');
$image = Image::read($upload)
->resize(300, 200);
Storage::put(
Str::random() . '.' . $upload->getClientOriginalExtension(),
$image->encodeByExtension($upload->getClientOriginalExtension(), quality: 70)
);
});
```
### Image Response Macro
Furthermore, the package includes a response macro that can be used to
elegantly encode an image resource and convert it to an HTTP response in a
single step.
The following code example shows how to read an image from disk apply
modifications and use the image response macro to encode it and send the image
back to the user in one call. Only the first parameter is required.
```php
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Format;
use Intervention\Image\Laravel\Facades\Image;
Route::get('/', function () {
$image = Image::read(Storage::get('example.jpg'))
->scale(300, 200);
return response()->image($image, Format::WEBP, quality: 65);
});
```
You can read more about intervention image in general in the [official documentation of Intervention Image](https://image.intervention.io).
## Authors
This library is developed and maintained by [Oliver Vogel](https://intervention.io)
Thanks to the community of [contributors](https://github.com/Intervention/image-laravel/graphs/contributors) who have helped to improve this project.
## License
Intervention Image Laravel is licensed under the [MIT License](LICENSE).

View File

@@ -0,0 +1,54 @@
{
"name": "intervention/image-laravel",
"description": "Laravel Integration of Intervention Image",
"homepage": "https://image.intervention.io/",
"keywords": [
"image",
"gd",
"imagick",
"watermark",
"thumbnail",
"resize",
"laravel"
],
"license": "MIT",
"authors": [
{
"name": "Oliver Vogel",
"email": "oliver@intervention.io",
"homepage": "https://intervention.io/"
}
],
"require": {
"php": "^8.1",
"illuminate/support": "^8|^9|^10|^11|^12",
"illuminate/http": "^8|^9|^10|^11|^12",
"illuminate/routing": "^8|^9|^10|^11|^12",
"intervention/image": "^3.11"
},
"require-dev": {
"ext-fileinfo": "*",
"phpunit/phpunit": "^10.0 || ^11.0 || ^12.0",
"orchestra/testbench": "^8.18 || ^9.9"
},
"autoload": {
"psr-4": {
"Intervention\\Image\\Laravel\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Intervention\\Image\\Laravel\\Tests\\": "tests"
}
},
"extra": {
"laravel": {
"providers": [
"Intervention\\Image\\Laravel\\ServiceProvider"
],
"aliases": {
"Image": "Intervention\\Image\\Laravel\\Facades\\Image"
}
}
}
}

View File

@@ -0,0 +1,46 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Image Driver
|--------------------------------------------------------------------------
|
| Intervention Image supports “GD Library” and “Imagick” to process images
| internally. Depending on your PHP setup, you can choose one of them.
|
| Included options:
| - \Intervention\Image\Drivers\Gd\Driver::class
| - \Intervention\Image\Drivers\Imagick\Driver::class
|
*/
'driver' => \Intervention\Image\Drivers\Gd\Driver::class,
/*
|--------------------------------------------------------------------------
| Configuration Options
|--------------------------------------------------------------------------
|
| These options control the behavior of Intervention Image.
|
| - "autoOrientation" controls whether an imported image should be
| automatically rotated according to any existing Exif data.
|
| - "decodeAnimation" decides whether a possibly animated image is
| decoded as such or whether the animation is discarded.
|
| - "blendingColor" Defines the default blending color.
|
| - "strip" controls if meta data like exif tags should be removed when
| encoding images.
*/
'options' => [
'autoOrientation' => true,
'decodeAnimation' => true,
'blendingColor' => 'ffffff',
'strip' => false,
]
];

View File

@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Laravel\Facades;
use Illuminate\Support\Facades\Facade;
/**
* @method static \Intervention\Image\Interfaces\ImageInterface read(mixed $input, string|array|\Intervention\Image\Interfaces\DecoderInterface $decoders = [])
* @method static \Intervention\Image\Interfaces\ImageInterface create(int $width, int $height)
* @method static \Intervention\Image\Interfaces\ImageInterface animate(callable $callback)
*/
class Image extends Facade
{
/**
* Binding name of the service container
*/
public const BINDING = 'image';
protected static function getFacadeAccessor()
{
return self::BINDING;
}
}

View File

@@ -0,0 +1,131 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Laravel;
use Illuminate\Http\Response;
use Intervention\Image\Exceptions\DriverException;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Exceptions\RuntimeException;
use Intervention\Image\FileExtension;
use Intervention\Image\Format;
use Intervention\Image\Image;
use Intervention\Image\MediaType;
class ImageResponseFactory
{
/**
* Image encoder options
*
* @var array<string, mixed>
*/
protected array $options = [];
/**
* Create new ImageResponseFactory instance
*
* @param Image $image
* @param null|string|Format|MediaType|FileExtension $format
* @param mixed ...$options
* @return void
*/
public function __construct(
protected Image $image,
protected null|string|Format|MediaType|FileExtension $format = null,
mixed ...$options
) {
$this->options = $options;
}
/**
* Static factory method to create HTTP response directly
*
* @param Image $image
* @param null|string|Format|MediaType|FileExtension $format
* @param mixed ...$options
* @throws NotSupportedException
* @throws DriverException
* @throws RuntimeException
* @return Response
*/
public static function make(
Image $image,
null|string|Format|MediaType|FileExtension $format = null,
mixed ...$options,
): Response {
return (new self($image, $format, ...$options))->response();
}
/**
* Create HTTP response
*
* @throws NotSupportedException
* @throws DriverException
* @throws RuntimeException
* @return Response
*/
public function response(): Response
{
return new Response(
content: $this->content(),
headers: $this->headers()
);
}
/**
* Read image contents
*
* @throws NotSupportedException
* @throws DriverException
* @throws RuntimeException
* @return string
*/
private function content(): string
{
return (string) $this->image->encodeByMediaType(
$this->format()->mediaType(),
...$this->options
);
}
/**
* Return HTTP response headers to be attached in the image response
*
* @return array
*/
private function headers(): array
{
return [
'Content-Type' => $this->format()->mediaType()->value
];
}
/**
* Determine the target format of the image in the HTTP response
*
* @return Format
*/
private function format(): Format
{
if ($this->format instanceof Format) {
return $this->format;
}
if (($this->format instanceof MediaType)) {
return $this->format->format();
}
if ($this->format instanceof FileExtension) {
return $this->format->format();
}
if (is_string($this->format)) {
return Format::create($this->format);
}
// target format is undefined (null) at this point:
// try to extract the original image format or use jpeg by default.
return Format::tryCreate($this->image->origin()->mediaType()) ?? Format::JPEG;
}
}

View File

@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Laravel;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Support\Facades\Response as ResponseFacade;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use Intervention\Image\ImageManager;
use Intervention\Image\Image;
use Illuminate\Http\Response;
use Intervention\Image\FileExtension;
use Intervention\Image\Format;
use Intervention\Image\MediaType;
class ServiceProvider extends BaseServiceProvider
{
/**
* Bootstrap application events
*
* @return void
*/
public function boot()
{
// define config files for publishing
$this->publishes([
__DIR__ . '/../config/image.php' => config_path(Facades\Image::BINDING . '.php')
]);
$this->app->afterResolving(ResponseFactory::class, function (): void {
// register response macro "image"
if ($this->shouldCreateResponseMacro()) {
ResponseFacade::macro(Facades\Image::BINDING, function (
Image $image,
null|string|Format|MediaType|FileExtension $format = null,
mixed ...$options,
): Response {
return ImageResponseFactory::make($image, $format, ...$options);
});
}
});
}
/**
* Register the image service
*
* @return void
*/
public function register()
{
$this->mergeConfigFrom(
__DIR__ . '/../config/image.php',
Facades\Image::BINDING
);
$this->app->singleton(Facades\Image::BINDING, function () {
return new ImageManager(
driver: config('image.driver'),
autoOrientation: config('image.options.autoOrientation', true),
decodeAnimation: config('image.options.decodeAnimation', true),
blendingColor: config('image.options.blendingColor', 'ffffff'),
strip: config('image.options.strip', false)
);
});
}
/**
* Determine if response macro should be created
*/
private function shouldCreateResponseMacro(): bool
{
if (!$this->app->runningUnitTests() && $this->app->runningInConsole()) {
return false;
}
return !ResponseFacade::hasMacro(Facades\Image::BINDING);
}
}

View File

@@ -0,0 +1,2 @@
providers:
- Intervention\Image\Laravel\ServiceProvider