msgframework / image
This library help to manipulate with images: crop, resize, change quality and watermarking.
Requires
- php: >=7.4
- msgframework/file: ^v0.1
Requires (Dev)
- phpstan/phpstan: ^1.3
- phpunit/phpunit: ^9.5
Suggests
- ext-gd: To use GD library based image processing.
- ext-imagick: To use Imagick based image processing.
README
About
This library help to manipulate with images: crop, resize, change quality and watermarking.
Usage
Load ImageFactory
use Msgframework\Lib\Image\ImageFactory; use Msgframework\Lib\Image\Adapter\ImageAdapter; ... // Create factory $factory = new ImageFactory();
Create Image with ImageFactory
... $factory = new ImageFactory(); // Create ImageAdapter from $path with auto selected Adapter $image = $factory->getImage($path); // Create ImagickAdapter from $path $image = $factory->getImage($path, 'Imagick'); // Create GDAdapter from $path $image = $factory->getImage($path, 'GD');
Create Image without ImageFactory
... // Create ImagickAdapter from $path $image = new \Msgframework\Lib\Image\Adapter\ImagickAdapter($path); // Create GDAdapter from $path $image = new \Msgframework\Lib\Image\Adapter\GDAdapter($path);
Get Image dimensions
... $image = $factory->getImage($path); $image->getWidth(); // Return width in px $image->getHeight(); // Return height in px
Resize Image
... $image = $factory->getImage($path); $image->resize(300, ImageAdapter::SIDE_WIDTH); // Set image width 300px ... $image->resize(500, ImageAdapter::SIDE_HEIGHT); // Set image width 500px $image->resize(800, ImageAdapter::SIDE_AUTO); // Set image width/height 800px by longest side
Scale Image
Used to scale to a given value on a specified side
... $image = $factory->getImage($path); $image->scale(300, ImageAdapter::SIDE_WIDTH); // Set image width 300px and scaled height
Crop Image
Allows you to crop the image to a specified size without the appearance of voids
... $image = $factory->getImage($path); $image->crop(300, 500); // If a similar image had dimensions of 400x400px, then the output will be an image of 300x400px
Change Image opacity
Allows you to change the transparency of the image as a percentage from 0 to one hundred
... $image = $factory->getImage($path); $image->opacity(.5); // Accepts float values from 0 to 1
Change Image quality
Allows you to specify the image quality in percentage
... $image = $factory->getImage($path); $image->quality(80); // Accepts int values from 0 to 100
Save changed Image
... $image = $factory->getImage($path); ... $image->save($image->getPathName()); // Owerwrite Image $image->save($newpath); // Save new Image
Show Image
If you need to display modified images by link without storing them, for example, implement a preview
... $image = $factory->getImage($path); ... $image->resize(600, ImageAdapter::SIDE_WIDTH); $image->show();
Watermarking Image
... $image = $factory->getImage($pathImage); $watermark = $factory->getImage($pathWatermark); ... $image->watermark($watermark, ImageAdapter::WATERMARK_CENTER_CENTER, 20, 70, 70); $image->save();
Watermark position codes
To position the watermark, you can use both ImageAdapter constants and codes from the table
Destroy Image
If you have finished manipulating the image but still want to work on the file
... $image = $factory->getImage($path); ... $image->save()->destroy(); ... $dirPath = $image->getPath(); // Get path to image directory
Installation
You can install this package easily with Composer.
Just require the package with the following command:
$ composer require msgframework/image
Asset
All images used for PHPUnit tests were downloaded from the site unsplash.com