This library help to manipulate with images: crop, resize, change quality and watermarking.
use Msgframework\Lib\Image\ImageFactory;
use Msgframework\Lib\Image\Adapter\ImageAdapter;
...
// Create factory
$factory = new 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 ImagickAdapter from $path
$image = new \Msgframework\Lib\Image\Adapter\ImagickAdapter($path);
// Create GDAdapter from $path
$image = new \Msgframework\Lib\Image\Adapter\GDAdapter($path);
...
$image = $factory->getImage($path);
$image->getWidth(); // Return width in px
$image->getHeight(); // Return height in px
...
$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
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
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
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
Allows you to specify the image quality in percentage
...
$image = $factory->getImage($path);
$image->quality(80);
// Accepts int values from 0 to 100
...
$image = $factory->getImage($path);
...
$image->save($image->getPathName()); // Owerwrite Image
$image->save($newpath); // Save new 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();
...
$image = $factory->getImage($pathImage);
$watermark = $factory->getImage($pathWatermark);
...
$image->watermark($watermark, ImageAdapter::WATERMARK_CENTER_CENTER, 20, 70, 70);
$image->save();
To position the watermark, you can use both ImageAdapter constants and codes from the table
Left | Center | Right | |
---|---|---|---|
Top | 1 | 2 | 3 |
Center | 4 | 5 | 6 |
Bottom | 7 | 8 | 9 |
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
You can install this package easily with Composer.
Just require the package with the following command:
$ composer require msgframework/image
All images used for PHPUnit tests were downloaded from the site unsplash.com