rkr / image
A simple image manipulation library using gd-lib
0.1.6
2024-01-15 15:37 UTC
Requires
- php: >= 7.4
- ext-gd: *
Requires (Dev)
- phpunit/phpunit: ^9.5
- saggre/phpdocumentor-markdown: 0.1.3
Suggests
- ext-exif: For automatic detection of image formats
README
A simple image manipulation library using gd-lib
Installation
composer require rkr/image
Usage
Find more information in the Documentation.
Resample proportionally
use Kir\Image\Image; $image = Image::loadFromFile('image.png'); $newImage = $image->getCopy(); $newImage->resizeProportional(500); $newImage->saveAsWebP('new-image-width-500.webp'); $newImage = $image->getCopy(); $newImage->resizeProportional(null, 500); $newImage->saveAsWebP('new-image-height-500.webp'); $newImage = $image->getCopy(); $newImage->resizeProportional(500, 500); $newImage->saveAsWebP('new-image-largest-side-to-500.webp');
Enlarge canvas
use Kir\Image\Image; $image = Image::loadFromFile('image.png'); $image->resizeProportional(500, 500); $image->resizeCanvasCentered(500, 500); $image->saveAsWebP('500x500.webp');
Auto crop image with optional border
The crop function is solved via a separate algorithm. If a border width is specified as a percentage, but too much of the original graphic was cut away during the actual cropping, then this image portion is retained in the border area.
use Kir\Image\Image; use Kir\Image\Color; $image = Image::loadFromFile('image.png'); $image->getCopy() ->crop(15 /* Threshold in 0..255 color scale */, 3 /* Border width in % */, Color::whiteOpaque()) ->resizeProportional(500, 500) ->resizeCanvasCentered(500, 500) ->saveAsWebP('500x500.webp');