meriksk / php-image
PHP class to re-size and scale images
Requires
- php: >=5.6.0
Requires (Dev)
- phpunit/phpunit: ^9.5
Suggests
- ext-gd: to use GD library based image processing.
- ext-imagick: to use Imagick based image processing.
This package is auto-updated.
Last update: 2024-11-19 15:17:28 UTC
README
The Image class offers a bunch of image processing features using GD or IMagick.
Requirements
- PHP >=5.6
Supported Image Libraries
- GD Library (>=2.0)
- Imagick PHP extension (>=6.5.7)
Setup
The preferred way to install this extension is through Composer. If you do not have Composer, you may install it by following the instructions at getcomposer.org.
Either run
composer require meriksk/php-image
or add
"meriksk/php-image": "~1.0"
to your composer.json and run composer update
Usage
Open image
Because this class uses namespacing, when instantiating the object, you need to either use the fully qualified namespace:
$image = new \meriksk\PhpImage\Image($filename);
or alias it:
use \meriksk\PhpImage\Image; $image = new Image($filename);
This class can use Imagick or GD extension - whichever is available. Imagick extension is preferred if is available. You can force the extension for manipulating images as follows:
$image = new Image($filename, Image::DRIVER_IMAGICK);
Save image
Library supports three formats of image: 'jpeg', 'png' and 'gif'. By default they quality is set to 75. When saving to disk or outputting into the browser, the script assumes the same output type and quality as input.
$image->save($filename);
Save in a different type to the source:
$image->save($filename, 60, 'png');
Output image
To render the image directly into the browser, you can call:
$image->toScreen(60, 'png');
Resize
resize
$image = $image->resize($width, $height, $allow_enlarge);
resize to width
$image = $image->resizeToWidth($width, $allow_enlarge);
resize to height
$image = $image->resizeToHeight($height, $allow_enlarge);
resize to best fit
$image = $image->resizeToBestFit($max_width, $max_height, $allow_enlarge);
resize to long side
$image = $image->resizeToLongSide($max, $allow_enlarge);
resize to short side
$image = $image->resizeToShortSide($max, $allow_enlarge);
Crop
** manual crop **
$image->crop($x, $y, $width, $height, $allow_enlarge);
** automatic crop **
$image->autoCrop($width, $height, $position);
Thumbnail
$image->thumbnail($width, $height, $fill, $allow_enlarge);
Rotate image
Rotating is counter clockwise;
Rotate on 90 degrees:
$image->rotate(90);
Rotate on 45 degrees, and fill empty field with white color:
$image->rotate(45, '#FFFFFF');
Flip image
Flip in vertical direction:
$image->flip();
Flip in horisontal direction:
$image->flip(Image::FLIP_HORIZONTAL);
Flip in both directions:
$image->flip(Image::FLIP_BOTH);
Filters
todo