GDImage
An image editing package using PHP's GD library., (*1)
, (*2)
Installation
Open a command console, enter your project directory and execute the following command to download the latest stable
version of this bundle:, (*3)
$ composer require sbolch/gd-image
This command requires you to have Composer installed globally, as explained in
the installation chapter
of the Composer documentation., (*4)
Usage
Image Converter
<?php
// ...
use sbolch\GDImage\Converter;
// ...
class Demo {
public function demo() {
$img = 'path/to/image.png';
$converter = new Converter();
$converter
->image($img)
->toJpg()
->target('path/to/converted-image.jpg')
->save();
}
}
Available functions
-
image ( string | GdImage $image ) : self
, (*5)
Set source image from file path or image resource, (*6)
-
target ( string $path ) : self
, (*7)
Set target path, (*8)
-
quality ( int quality ) : self
, (*9)
Set output quality - accepted value is a percentage, (*10)
-
background ( int red, int green, int blue ) : self
, (*11)
Set background color instead of transparency - accepted values are integers between 0 and 255, (*12)
-
toAvif() | toBmp() | toGif() | toJpg() | toJpeg() | toPng() | toWebp() : self
, (*13)
Set output format, (*14)
-
save() : string
, (*15)
Save generated image, (*16)
-
output() : void
, (*17)
Print image to PHP output, (*18)
Image Sizer
<?php
// ...
use sbolch\GDImage\Sizer;
// ...
class Demo {
public function demo() {
$img = 'path/to/image.jpg';
$sizer = new Sizer();
$sizer
->image($img)
->thumbnail(400, 300)
->save();
}
}
Available functions
See available functions at Converter, all of them are available here, too, (*19)
-
image ( string | GdImage $image ) : self
, (*20)
Set source image from file path or image resource, (*21)
-
instance(): GdImage
, (*22)
Return current image resource, (*23)
-
widen ( int $width ) : self
, (*24)
Set the image to the given width while preserving its ratio, (*25)
-
heighten ( int $height ) : self
, (*26)
Set the image to the given height while preserving its ratio, (*27)
-
maximize ( int $width, int $height ) : self
, (*28)
Maximize image's size by its longer dimension while preserving its ratio, (*29)
-
crop ( int $width, int $height [, int $x [, int $y]] ) : self
, (*30)
Crop picture to given dimensions starting at the given position, (*31)
-
thumbnail ( int $width, int $height ) : self
, (*32)
Make a thumbnail by cropping the image by its shorter dimension (centered crop), (*33)
Cached Image Sizer
Same as Image Sizer but it uses cache., (*34)
<?php
// ...
use sbolch\GDImage\CachedSizer;
// ...
class Demo {
public function demo() {
$img = 'path/to/image.jpg';
$sizer = new CachedSizer();
$sizer
->image($img)
->thumbnail(400, 300)
->save();
}
}
Twig integration
If you use Twig, you can include the extensions
- sbolch\GDImage\Twig\ConverterExtension([string $docroot])
- sbolch\GDImage\Twig\SizerExtension([string $docroot [, string $cacheDir]])
, (*35)
You can override the default PHP document root with the optional $docroot parameter for both extensions., (*36)
You can use the cache mechanism in SizerExtension by using the optional $cacheDir parameter with your desired cache folder., (*37)
Then you can use the below filters (question mark marks optional parameters), (*38)
{# Converting image and returning its new path via sbolch\GDImage\Twig\ConverterExtension #}
{{ 'path/to/image'|convert_to_avif(?targetPath, ?quality) }}
{{ 'path/to/image'|convert_to_bmp(?targetPath, ?quality) }}
{{ 'path/to/image'|convert_to_gif(?targetPath, ?quality) }}
{{ 'path/to/image'|convert_to_jpg(?targetPath, ?quality) }}
{{ 'path/to/image'|convert_to_jpeg(?targetPath, ?quality) }}
{{ 'path/to/image'|convert_to_png(?targetPath, ?quality) }}
{{ 'path/to/image'|convert_to_webp(?targetPath, ?quality) }}
{# Resizing image and returning its new path via sbolch\GDImage\Twig\SizerExtension #}
{{ 'path/to/image'|widen(width, ?outputFormat, ?targetPath) }}
{{ 'path/to/image'|heighten(height, ?outputFormat, ?targetPath) }}
{{ 'path/to/image'|maximize(width, height, ?outputFormat, ?targetPath) }}
{{ 'path/to/image'|thumbnail(width, height, ?outputFormat, ?targetPath) }}
Phar mode (only Converter yet)
You can download the phar file on the releases page and use it as below:, (*39)
php converter.phar -i /path/to/image -f jpg
- -i (--input) : Input file
- -o (--output) : Output file (optional - you must use -o or -f)
- -f (--format) : Output format (optional - you must use -o or -f)
- -q (--quality): Encoding quality as percentage (optional)