greg-md / php-imagix
Save images in real-time in different formats using Intervention Image.
Requires
- php: ^7.3
- greg-md/php-support: dev-master
- intervention/image: ^2
Requires (Dev)
- phpunit/phpunit: ^8
This package is auto-updated.
Last update: 2024-12-19 23:36:39 UTC
README
Save images in real-time in different formats using Intervention Image.
You don't care anymore about generating new images from their sources when your app UI was changed. Only thing you should do is to add new formats or change existent and the application will take care of them by itself.
Table of Contents:
Requirements
- PHP Version
^5.6 || ^7.0
How It Works
First of all, you have to initialize the Imagix.
Optionally you can create an URL decorator for it. It helps to rewrite image URLs to/from HTTP Server(Apache/Nginx).
class ImagixDecorator implements ImageDecoratorStrategy { // Will use /imagix public path for generated images. private $uri = '/imagix'; public function output($url) { return $this->nginxUri . $url; } public function input($url) { return \Greg\Support\Str::shift($url, $this->uri); } } $sourcePath = __DIR__ . '/img'; $destinationPath = __DIR__ . '/imagix'; $decorator = new ImagixDecorator(); $intervention = new Intervention\Image\ImageManager(); $imagix = new \Greg\Imagix\Imagix($intervention, $sourcePath, $destinationPath, $decorator);
Next, create image formats.
$imagix->format('square', function (\Intervention\Image\Image $image) { $image->resize(600, 600, function (\Intervention\Image\Constraint $constraint) { $constraint->aspectRatio(); $constraint->upsize(); }); });
Now, it's time to use it in your app.
It will generate an URL in format <image_path>/<image_name>@<format>@<last_modified>.<image_extension>
.
// result: /imagix/pictures/picture@square@129648839.jpg $imageUrl = $imagix->url('/pictures/picture.jpg', 'square'); echo '<img src="' . $imageUrl . '" width="600" height="600" alt="Picture" />';
To see the results, you have to config your http server
.
Nginx
# Imagix
location ~* ^/imagix/.+ {
# If images doesn't exists, send to PHP to create it.
if (!-f $document_root$uri) {
rewrite .+ /imagix.php last;
}
expires max;
add_header Pragma public;
add_header Cache-Control "public";
add_header Vary "Accept-Encoding";
}
In imagix.php you will dispatch new files that was not generated yet in /imagix
path.
$imagix->send($_SERVER['REQUEST_URI']);
Methods
Magic methods:
__construct
Initialize the manager.
__construct(ImageManager $manager, string $sourcePath, string $destinationPath, ImageDecoratorStrategy $decorator = null)
$manager
- Intervention image manager;
$sourcePath
- Source path;
$destinationPath
- Destination path;
$decorator
- Decorator.
Example:
class ImagixDecorator implements ImageDecoratorStrategy { // Will use /imagix public path for generated images. private $uri = '/imagix'; public function output($url) { return $this->nginxUri . $url; } public function input($url) { return \Greg\Support\Str::shift($url, $this->uri); } } $sourcePath = __DIR__ . '/img'; $destinationPath = __DIR__ . '/imagix'; $decorator = new ImagixDecorator(); $intervention = new Intervention\Image\ImageManager(); $imagix = new \Greg\Imagix\ImagixManager($intervention, $sourcePath, $destinationPath, $decorator);
Supported methods:
- format - Register an image format;
- url - Get formatted image URL;
- source - Get source URL of a formatted image URL;
- effective - Get effective URL of a formatted image URL;
- compile - Compile a formatted image URL;
- send - Send image of a formatted image URL;
- unlink - Remove formatted versions of an image;
- remove - Remove formatted images.
format
Register an image format.
format(string $name, callable(\Intervention\Image\Image $image): void $callable): $this
$name
- Format name;
$callable
- A callable to format an image.
$image
- The image.
Example:
$imagix->format('square', function (\Intervention\Image\Image $image) { $image->resize(600, 600, function (\Intervention\Image\Constraint $constraint) { $constraint->aspectRatio(); $constraint->upsize(); }); });
url
Get formatted image URL.
url(string $source, string $format): string
$source
- Original image URL;
$format
- Format name.
Example:
$imagix->url('/pictures/picture.jpg', 'square'); // result: /pictures/picture@square@129648839.jpg
source
Get source URL of a formatted image URL.
source(string $destination): string
$destination
- Formatted image URL.
Example:
$imagix->source('/pictures/picture@square@129648839.jpg'); // result: /pictures/picture.jpg
effective
Get effective URL of a formatted image URL.
Every time a image changes, it's effective URL also changes. So, if you have an old URL, you will get the new one.
This is useful when you store somewhere formatted urls.
By default,
send
method will return a 301 redirect to the new URL.
effective(string $destination): string
$destination
- Formatted image URL.
Example:
$imagix->effective('/pictures/picture@square@129642346.jpg'); // result: /pictures/picture@square@129648839.jpg
compile
Compile a formatted image URL. Will return the real path of the image.
compile(string $destination): string
$destination
- Formatted image URL.
Example:
$imagix->compile('/pictures/picture@square@129648839.jpg'); // result: /path/to/pictures/picture@square@129648839.jpg
send
Send image of a formatted image URL.
send(string $destination): $this
$destination
- Formatted image URL.
Example:
$imagix->send('/pictures/picture@square@129648839.jpg');
unlink
Remove formatted versions of an image.
unlink(string $source, string $format = null, int $lifetime = 0): $this
$source
- Formatted image URL;
$format
- Format name;
$lifetime
- If set, will remove only expired files.
Example:
$imagix->unlink('/pictures/picture.jpg');
remove
Remove formatted images.
remove(string $format = null, int $lifetime = 0): $this
$format
- Format name;
$lifetime
- If set, will remove only expired files.
Example:
$imagix->remove(); // Will remove all formatted images. $imagix->remove('square'); // Will remove only square images.
License
MIT © Grigorii Duca