ImageUtil.php
Easy resize, filter and compression class for images.
All image manipulations are applied to a temporary image. Only the save() method is permanent.
Requires: PHP5 and GD2 library.
Optional: jpegtran, pngcrush.
Usage:
include 'ImageUtil.php';
// Render image without saving example:
// an absolute URL or path to file. Can be .jpg, .jpeg, .gif or .png
$img = new ImageUtil('images/large/input1.jpg');
// set width and height and then render image. Can be .jpg, .jpeg, .gif or .png
$img->resize(320, 240)->render('output.jpg');
// Save image example:
// an absolute URL or path to file. Can be .jpg, .jpeg, .gif or .png
$img = new ImageUtil('images/large/input2.jpg');
// set width, height and image resize option
$img->resize(150, 100, 'crop')
// add watermark
->watermark('images/large/watermark.png')
// save path to file. Can be .jpg, .jpeg, .gif or .png
->save('images/small/output.jpg');
- Author
- Jani Mikkonen
- License
- public domain
- Link
- https://github.com/janisto/ImageUtil/
- Package
- ImageUtil
\ImageUtil
Main class for ImageUtil
- Author
- Jani Mikkonen
- License
- public domain
- Link
- https://github.com/janisto/ImageUtil/
Methods

__construct(
string $file, array $options
=
array()
)
:
voidConstructor.
| Name | Type | Description |
|---|---|---|
| $file | string | an absolute URL or path to file |
| $options | array | an array of options |
| Exception | Description |
|---|---|
| \Exception | invalid image |

blur(
string $type
=
'gaussian'
)
:
objectSet blur.
This method is chainable.
| Name | Type | Description |
|---|---|---|
| $type | string | blur type: gaussian or selective. Default: 'gaussian'. |
| Type | Description |
|---|---|
| object | the current object for fluent interface |
| Exception | Description |
|---|---|
| \Exception | missing imagefilter function |
- Access
- public

brightness(
int $value
=
-20
)
:
objectSet brightness.
This method is chainable.
| Name | Type | Description |
|---|---|---|
| $value | int | level of brightness between -255 to 255. Default: -20. |
| Type | Description |
|---|---|
| object | the current object for fluent interface |
| Exception | Description |
|---|---|
| \Exception | missing imagefilter function |
- Access
- public

contrast(
int $value
=
-10
)
:
objectSet contrast.
This method is chainable.
| Name | Type | Description |
|---|---|---|
| $value | int | level of contrast between -255 to 255. Default: -10. |
| Type | Description |
|---|---|
| object | the current object for fluent interface |
| Exception | Description |
|---|---|
| \Exception | missing imagefilter function |
- Access
- public

getOption(
string $key
)
:
mixedReturns the value of the specified option.
| Name | Type | Description |
|---|---|---|
| $key | string | the name of the option to retrieve |
| Type | Description |
|---|---|
| mixed | option or false |
- Access
- public

greyscale(
)
:
objectConvert image to greyscale.
This method is chainable.
| Type | Description |
|---|---|
| object | the current object for fluent interface |
| Exception | Description |
|---|---|
| \Exception | missing imagefilter function |
- Access
- public

interlace(
)
:
objectInterlace image.
This method is chainable.
| Type | Description |
|---|---|
| object | the current object for fluent interface |
- Access
- public

mask(
string $file, int $top
=
0, int $left
=
0, string $bgColor
=
''
)
:
objectAdd mask layer to image. You can create for example polaroid like images.
This method is chainable.
| Name | Type | Description |
|---|---|---|
| $file | string | an absolute URL or path to mask .png file |
| $top | int | top position of resized image. Default: 0. |
| $left | int | left position of resized image. Default: 0. |
| $bgColor | string | HEX color of the uncovered/transparent zone |
| Type | Description |
|---|---|
| object | the current object for fluent interface |
| Exception | Description |
|---|---|
| \Exception | invalid image |
- Access
- public

noise(
int $value
=
30
)
:
objectSet noise.
This method is chainable.
| Name | Type | Description |
|---|---|---|
| $value | int | factor between 0 and 255. Default: 30. |
| Type | Description |
|---|---|
| object | the current object for fluent interface |
- Access
- public

pixelate(
int $value
=
10, bool $native
=
true
)
:
objectSet pixelate.
This method is chainable.
| Name | Type | Description |
|---|---|---|
| $value | int | size of pixels. Default: 10. |
| $native | bool | use native GD library pixelate filter if available. Enabled by default. |
| Type | Description |
|---|---|
| object | the current object for fluent interface |
- Access
- public

rasterbate(
int $value
=
6
)
:
objectSet rasterbate.
This method is chainable.
| Name | Type | Description |
|---|---|---|
| $value | int | maximum circle size in pixels. Default: 6. |
| Type | Description |
|---|---|
| object | the current object for fluent interface |
- Access
- public

render(
string $file, int $quality
=
80, bool $destroy
=
true
)
:
voidOutput the image to the browser without saving the image.
| Name | Type | Description |
|---|---|---|
| $file | string | output image file name |
| $quality | int | image quality: 0-100. Default: 80. |
| $destroy | bool | destroy image resource identifiers on save. Enabled by default. |
| Exception | Description |
|---|---|
| \Exception | invalid image type |
- Access
- public

resize(
int $newWidth, int $newHeight, string $option
=
'auto'
)
:
objectResize the image. Must be called first if resize is required since this will reset all filters for image.
This method is chainable.
| Name | Type | Description |
|---|---|---|
| $newWidth | int | new width of the image in pixels |
| $newHeight | int | new height of the image in pixels |
| $option | string | one of the image resize options: exact, portrait, landscape, crop or auto. Default: 'auto'. |
| Type | Description |
|---|---|
| object | the current object for fluent interface |
- Access
- public

rotate(
mixed $value
=
'random', string $bgColor
=
'ffffff'
)
:
objectRotate image.
This method is chainable.
| Name | Type | Description |
|---|---|---|
| $value | mixed | rotation angle in degrees between -360 and 360 or random. Default: 'random'. |
| $bgColor | string | HEX color of the uncovered zone after the rotation. Default: 'ffffff'. |
| Type | Description |
|---|---|
| object | the current object for fluent interface |
- Access
- public

save(
string $path, int $quality
=
80, bool $destroy
=
true, int $chmod
=
0644
)
:
voidSave the image.
| Name | Type | Description |
|---|---|---|
| $path | string | file path and output image file name. Directory must writable. |
| $quality | int | image quality: 0-100. Default: 80. |
| $destroy | bool | destroy image resource identifiers on save. Enabled by default. |
| $chmod | int | permissions for saved image. Default: 0644. |
| Exception | Description |
|---|---|
| \Exception | invalid image type or directory is unwritable |
- Access
- public

scatter(
int $value
=
4
)
:
objectSet scatter.
This method is chainable.
| Name | Type | Description |
|---|---|---|
| $value | int | intensity. Default: 4. |
| Type | Description |
|---|---|
| object | the current object for fluent interface |
- Access
- public

sepia(
string $rgb
=
'90, 55, 30', int $brightness
=
-30
)
:
objectSet sepia.
This method is chainable.
| Name | Type | Description |
|---|---|---|
| $rgb | string | comma separated RGB value. Default: '90, 55, 30'. |
| $brightness | int | level of brightness. Default: -30. |
| Type | Description |
|---|---|
| object | the current object for fluent interface |
| Exception | Description |
|---|---|
| \Exception | missing imagefilter function |
- Access
- public

sharpen(
bool $value
=
true
)
:
objectSet sharpen.
| Name | Type | Description |
|---|---|---|
| $value | bool | sharpen image automatically at first save or render with optimal image sharpening detection. Enabled by default. |
| Type | Description |
|---|---|
| object | the current object for fluent interface |
- Access
- public

smooth(
int $value
=
6
)
:
objectSet smooth.
This method is chainable.
| Name | Type | Description |
|---|---|---|
| $value | int | level of smoothness -12 to 12. Values outside of the range -8 to 8 are usually unusable. Default: 6. |
| Type | Description |
|---|---|
| object | the current object for fluent interface |
| Exception | Description |
|---|---|
| \Exception | missing imagefilter function |
- Access
- public

watermark(
string $file, int $transparency
=
40, int $padding
=
2, string $position
=
'BR'
)
:
objectAdd watermark to the image.
This method is chainable.
| Name | Type | Description |
|---|---|---|
| $file | string | an absolute URL or path to file |
| $transparency | int | watermark transparency (0-100). Default: 40. |
| $padding | int | watermark padding in pixels. Default: 2. |
| $position | string | position of watermark (one of: TL, TR, BL or BR). Default: 'BR'. |
| Type | Description |
|---|---|
| object | the current object for fluent interface |
| Exception | Description |
|---|---|
| \Exception | invalid image |
- Access
- public