ImageProcessor
A PHP library to resize / convert images, (*1)
Table of contents
, (*2)
Requirements
, (*3)
Installation
, (*4)
Using Composer
Run the following command:, (*5)
composer require widgets-nl/image-processor
, (*6)
Manual installation
- Clone or download the project
- Copy
ImageProcessor.php and ImageResource.php to your directory of choice.
- Make sure the two files are autoloaded / included e.g.:
require_once 'ImageProcessor.php';
require_once 'ImageResource.php';
, (*7)
Usage
, (*8)
Basic usage
setWidth(100)
->setHeight(100)
->save('output.jpg')
;
```
#### Specify image type
```php
setType(ImageProcessor::TYPE_PNG)
->save('output.png')
;
```
#### Output to string
```php
setWidth(100)
->setHeight(100)
;
$imageData = $image->getFileData();
header('Content-Type: image/jpeg');
echo $imageData;
```
#### Output to base64 encoded string / base64 data URI
```php
setWidth(100)
->setHeight(100)
;
// $imageData = $image->getFileData(ImageProcessor::DATA_ENCODING_BASE64);
$imageDataURI = $image->getFileData(ImageProcessor::DATA_ENCODING_BASE64_DATA_URI);
?>
<img src="<?php echo $imageDataURI; ?>">
br/, (*9)
, (*10)
Reference
, (*11)
class ImageProcessor
, (*12)
public function __construct(string $pathOrFileData = '') : ImageProcessor
Class constructor, (*13)
Parameters, (*14)
Example:, (*15)
$image = new ImageProcessor();
// Or
$image = new ImageProcessor('test.jpg');
// Or
$image = new ImageProcessor(file_get_contents('test.jpg'));
br/, (*16)
, (*17)
public function setResourceFromPath(string $path) : ImageProcessor
Loads image from specified path.
Creates an ImageResource instance internally., (*18)
For chainability, this method returns the ImageProcessor instance., (*19)
Parameters, (*20)
| Name |
Type |
Required |
Default value |
Description |
$path |
string |
Yes |
n/a |
Full path to an image file |
Example:, (*21)
$image = new ImageProcessor();
$image->setResourceFromPath('test.jpg');
br/, (*22)
, (*23)
public function setResourceFromFileData(string $fileData) : ImageProcessor
Loads image from raw image data.
Creates an ImageResource instance internally., (*24)
For chainability, this method returns the ImageProcessor instance., (*25)
Parameters, (*26)
| Name |
Type |
Required |
Default value |
Description |
$fileData |
string |
Yes |
n/a |
Raw image data |
Example:, (*27)
$image = new ImageProcessor();
$image->setResourceFromFileData(file_get_contents('test.jpg'));
br/, (*28)
, (*29)
public function getWidth() : int
Returns the current width setting., (*30)
br/, (*31)
, (*32)
public function setWidth(int $width) : ImageProcessor
Set the width of the output image., (*33)
For chainability, this method returns the ImageProcessor instance., (*34)
Parameters, (*35)
| Name |
Type |
Required |
Default value |
Description |
$width |
int |
Yes |
n/a, but if setWidth is never called, value is inherited from source image
|
Maximum output image width |
Example:, (*36)
$image = new ImageProcessor('test.png');
$image
->setWidth(100)
->save('test_copy.png')
;
br/, (*37)
, (*38)
public function getHeight() : int
Returns the current height setting., (*39)
br/, (*40)
, (*41)
public function setHeight(int $height) : ImageProcessor
Set the height of the output image., (*42)
For chainability, this method returns the ImageProcessor instance., (*43)
Parameters, (*44)
| Name |
Type |
Required |
Default value |
Description |
$height |
int |
Yes |
n/a, but if setHeight is never called, value is inherited from source image
|
Maximum output image height |
Example:, (*45)
$image = new ImageProcessor('test.png');
$image
->setHeight(100)
->save('test_copy.png')
;
br/, (*46)
, (*47)
Object fit
The object fit setting is inspired by the object-fit CSS property and works pretty much the same way., (*48)
There are 3 possible values:, (*49)
| Value |
Constant |
Description |
1 |
ImageProcessor::OBJECT_FIT_FILL |
The source image is stretched to match the target dimensions. For most images, you probably do not want to use this, as this messes up the aspect ratio. |
2 |
ImageProcessor::OBJECT_FIT_CONTAIN |
The source image is scaled to fit within the bounding box of the target dimensions, maintaining its aspect-ratio and without cropping. This is the default setting. |
3 |
ImageProcessor::OBJECT_FIT_COVER |
The source image is scaled to fill the full bounding box of the target dimensions. It will be cropped to fit. |
, (*50)
public function getObjectFit() : int
Returns the current object fit setting., (*51)
br/, (*52)
, (*53)
public function setObjectFit(int $objectFit = self::OBJECT_FIT_CONTAIN) : ImageProcessor
Set the object fit setting to be applied to the image upon output., (*54)
For chainability, this method returns the ImageProcessor instance., (*55)
Parameters, (*56)
| Name |
Type |
Required |
Default value |
Description |
$objectFit |
int |
Yes |
2 |
Object fit setting to use |
Example:, (*57)
$image = new ImageProcessor('test.jpg');
$image
->setWidth(100)
->setHeight(100)
->setObjectFit(ImageProcessor::OBJECT_FIT_COVER)
->save('test_copy.jpg')
;
br/, (*58)
, (*59)
Canvas fit
The canvas fit setting determines whether or not the output image canvas must be cropped to match the image.
This setting is only relevant when setting the object fit setting to ImageProcessor::OBJECT_FIT_CONTAIN, (*60)
There are 2 possible values:, (*61)
| Value |
Constant |
Description |
1 |
ImageProcessor::CANVAS_FIT_KEEP |
The canvas will not be cropped. The output image dimensions will always be set to the specified width and height settings. |
2 |
ImageProcessor::CANVAS_FIT_CROP |
The canvas will be cropped to fit the containing image. The output image dimensions may differ from the specified width and height settings. This is the default setting. |
, (*62)
public function getCanvasFit() : int
Returns the current canvas fit setting., (*63)
br/, (*64)
, (*65)
public function setCanvasFit(int $canvasFit = self::CANVAS_FIT_CROP) : ImageProcessor
Set the canvas fit setting to be applied to the image upon output., (*66)
For chainability, this method returns the ImageProcessor instance., (*67)
Parameters, (*68)
| Name |
Type |
Required |
Default value |
Description |
$canvasFit |
int |
Yes |
2 |
Canvas fit setting to use |
Example:, (*69)
$image = new ImageProcessor('test.jpg');
$image
->setWidth(100)
->setHeight(100)
->setCanvasFit(ImageProcessor::CANVAS_FIT_KEEP)
->save('test_copy.jpg')
;
br/, (*70)
, (*71)
Type (mimetype)
When loading an image from a file path, the output file type will automatically be of the same type by default.
When no type can be determined (e.g. when loading an image from raw image data), image/jpeg will be used by default., (*72)
You may also specify an output type to be used., (*73)
Currently, there are 3 supported file types:, (*74)
| Value |
Constant |
Description |
image/jpeg |
ImageProcessor::TYPE_JPEG |
JPEG image. |
image/png |
ImageProcessor::TYPE_PNG |
PNG-24 image with alpha channel (if applicable). |
image/gif |
ImageProcessor::TYPE_GIF |
Gif image with transparency (if applicable). |
, (*75)
public function getType() : string
Returns the current type setting., (*76)
br/, (*77)
, (*78)
public function setType(string $type = self::TYPE_JPEG) : ImageProcessor
Set the type to be used on the output image., (*79)
For chainability, this method returns the ImageProcessor instance., (*80)
Parameters, (*81)
| Name |
Type |
Required |
Default value |
Description |
$type |
string |
Yes |
image/jpeg |
Output image mime type |
Example:, (*82)
$image = new ImageProcessor('test.jpg');
$image
->setType(ImageProcessor::TYPE_PNG)
->save('test.png')
;
br/, (*83)
, (*84)
Quality
Sets the image quality (compression level) to be used on the output image., (*85)
This setting does not apply to gif images, and has little to no effect on most png images., (*86)
The quality setting uses a 1-100 value, but has some built-in defaults:, (*87)
| Value |
Constant |
Description |
25 |
ImageProcessor::QUALITY_LOW |
Low quality. |
50 |
ImageProcessor::QUALITY_MEDIUM |
Medium quality. |
75 |
ImageProcessor::QUALITY_HIGH |
High quality. |
100 |
ImageProcessor::QUALITY_MAXIMUM |
Highest quality. |
, (*88)
public function getQuality() : int
Returns the current quality setting., (*89)
br/, (*90)
, (*91)
public function setQuality(int $quality = self::QUALITY_HIGH) : ImageProcessor
Set the quality setting to be applied to the image upon output., (*92)
For chainability, this method returns the ImageProcessor instance., (*93)
Parameters, (*94)
| Name |
Type |
Required |
Default value |
Description |
$quality |
int |
Yes |
75 |
Quality setting to use |
Example:, (*95)
$image = new ImageProcessor('test.jpg');
$image
->setQuality(ImageProcessor::QUALITY_LOW)
->save('test_copy.jpg')
;
br/, (*96)
, (*97)
Background fill
Sets the background fill style/color to be used on the output image., (*98)
The background may be set to any valid hex color (e.g. #FF0000), but the short (#F00) notation is not supported.
The pound/hash sign is optional., (*99)
For png or gif images, you may use the TRANSPARENT setting.
Some built-in values:, (*100)
| Value |
Constant |
Description |
TRANSPARENT |
ImageProcessor::BACKGROUND_TRANSPARENT |
Transparent fill (gif and png images only). |
#000000 |
ImageProcessor::BACKGROUND_BLACK |
Black background fill. |
#FFFFFF |
ImageProcessor::BACKGROUND_WHITE |
Black background fill. |
, (*101)
public function getBackground() : string
Returns the current background setting., (*102)
br/, (*103)
, (*104)
public function setBackground(string $background = self::QUALITY_HIGH) : ImageProcessor
Set the background setting to be applied to the image upon output., (*105)
For chainability, this method returns the ImageProcessor instance., (*106)
Parameters, (*107)
| Name |
Type |
Required |
Default value |
Description |
$background |
string |
Yes |
#000000 |
Background setting to use |
Example:, (*108)
$image = new ImageProcessor('test.png');
$image
// ->setBackground('#FF0000')
->setBackground(ImageProcessor::BACKGROUND_WHITE)
->save('test_copy.png')
;
br/, (*109)
, (*110)
public function getFileData(int $encoding = self::DATA_ENCODING_RAW) : string
Returns the output image as a string of image data., (*111)
The data encoding may be specified using the encoding parameter, which can be set to one of:, (*112)
| Value |
Constant |
Description |
1 |
ImageProcessor::DATA_ENCODING_RAW |
Raw image data. |
2 |
ImageProcessor::DATA_ENCODING_BASE64 |
Base-64 encoded image data. |
3 |
ImageProcessor::DATA_ENCODING_BASE64_DATA_URI |
Base-64 encoded image data with a data:_mimetype_ prefix that automatically contains the appropriate mime type (e.g. image/jpeg). |
Parameters, (*113)
| Name |
Type |
Required |
Default value |
Description |
$encoding |
int |
No |
1 |
Output setting to use |
Example:, (*114)
$image = new ImageProcessor('test.jpg');
$image->setWidth(100);
$rawImageData = $image->getFileData();
br/, (*115)
, (*116)
public function save(string $outputPath) : ImageProcessor
Saves the output image to given path., (*117)
For chainability, this method returns the ImageProcessor instance., (*118)
Parameters, (*119)
| Name |
Type |
Required |
Default value |
Description |
$outputPath |
string |
Yes |
n/a |
Output path to write to |
Example:, (*120)
$image = new ImageProcessor('test.png');
$image
->setWidth(100)
->save('test_copy.jpg')
;
br/, (*121)
, (*122)
class ImageResource
This class is used internally and has no accessible public members or methods., (*123)