Image Class
The Image class is used to easily add common manipulations to an image such as resizing, cropping, and so on.
The Image class has a few limitations that should be made aware of. First off, the GD library handles transparency quite badly.
Due to this, Image::rotate() can not use transparent background. The rounding image in imagemagick
is also flawed as images with transparent corners will get opaque circles in the corners.
Using multiple transformations in GD can result in abnormal results.
The Image class accepts the following configuration options. Copy the file from fuel/core/config/image.php to fuel/app/config/image.php
driver |
string |
'gd'
|
Can be any name of a valid library, currently only 'gd', 'imagemagick' and 'imagick'
|
bgcolor |
string |
null
|
Background in hex for (Ex: #ff0, #4f32de). Set to null for a transparent background.
|
watermark_alpha |
integer |
75
|
The transparency of any watermarks applied to the image. Ranges from 0-100.
|
quality |
integer |
100
|
Used for the quality in jpeg images and pngs.
|
filetype |
string |
null
|
Defines an override default image type if no extension is given. If set to null, it inherits the original extension instead.
|
imagemagick_dir |
string |
'/usr/bin/'
|
Where the imagemagick executables are stored. Must add leading slash.
|
temp_dir |
string |
'/tmp/'
|
Temporary directory to store image files that are being edited.
|
temp_append |
string |
'fuel_image'
|
The string to append to the temp images, as to avoid conflicts.
|
debug |
boolean |
false
|
Turns on debug mode, which skips setting header and outputs debug information on an image.
|
Presets are a feature in the Image class that allow defining of a set of tasks in the config, and call that preset. An example is:
In app/config/image.php
'presets' => array(
'mypreset' => array(
'bgcolor' => '#f00',
'filetype' => 'jpg',
'quality' => 75,
'actions' => array(
array('crop_resize', 200, 200),
array('watermark', '$1'),
array('output', 'png')
)
)
)
In your controller:
Image::load('filename.gif')->preset('mypreset', 'watermark.gif');
The forge method creates a new Image_Driver instance.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$config |
array()
|
An array of configuration options for the Image_Driver instance |
|
Returns |
Image_Driver |
Example |
$image = Image::forge();
$image = Image::forge(array(
'quality' => 80
));
$image
->load('image.png')
->output('image.jpeg');
|
Changes the value of a configuration option.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$index |
Required |
The index to be set, or an array of configuration options. |
$value |
null
|
The value to be set if $index is not an array. |
|
Returns |
Image_Driver |
Example |
Image::load('filename.gif')
->config('bgcolor', '#f00')
->resize(100, 100, true, true);
|
The load method attempts to load an image for editing.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$filename |
Required |
The path to the image file to be loaded. |
$return_data |
false
|
Determines whether to return image data, and only works with GD. |
$force_extension |
false
|
Whether or not to force the image extension (to $force_extension), and only works with GD. |
|
Returns |
Image_Driver |
Example |
Image::load('filename.gif');
Upload::process(array(
'path' => DOCROOT.DS.'files'
));
if (Upload::is_valid())
{
$data = Upload::get_files(0);
Image::load($data['file'], false, $data['extension'])
->crop_resize(200, 200)
->save('image');
}
|
Crops the image using coordinates or percentages.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$x1 |
Required |
The position of the first coord on the x-axis. |
$y1 |
Required |
The position of the first coord on the y-axis. |
$x2 |
Required |
The position of the second coord on the x-axis. |
$y2 |
Required |
The position of the second coord on the y-axis. |
|
Returns |
Image_Driver |
Example |
Image::load('filename.gif')
->crop(20, 20, 180, 180);
Image::load('filename.gif')
->crop(40, 40, -40, -40);
Image::load('filename.gif')
->crop('15%', '15%', '-15%', '-15%');
|
Resizes the image. If the width or height is null, it will resize retaining the original aspect ratio.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$width |
Required |
The new width of the image. |
$height |
null
|
The new height of the image |
$keepar |
true
|
If set to true, will keep the Aspect Ratio of the image identical to the original. |
$pad |
false
|
If set to true and $keepar is true, it will pad the image with the configured bgcolor. |
|
Returns |
Image_Driver |
Example |
Image::load('filename.gif')
->resize(100, 100);
Image::load('filename.gif')
->resize('50%', '50%');
Image::load('filename.gif')
->resize(100, 100, false);
Image::load('filename.gif')
->resize(100, 200, true, true);
|
Resizes the image and crops it to fit the given width and height.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$width |
Required |
The new width of the image. |
$height |
null
|
The new height of the image |
|
Returns |
Image_Driver |
Example |
Image::load('filename.gif')
->crop_resize(200, 200);
|
Rotates the image clockwise.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$degrees |
Required |
The degrees to rotate the image by. Accepts positive and negatives. |
|
Returns |
Image_Driver |
Example |
Image::load('filename.gif')
->rotate(90);
Image::load('filename.gif')
->rotate(-90);
Image::load('filename')
->rotate(450);
|
Flip the image vertically and / or horizontally.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$direction |
Required |
the flip direction. Accepts "horizontal", "vertical" or "both" |
|
Returns |
Image_Driver |
Example |
Image::load('filename.gif')
->flip('vertical');
Image::load('filename.gif')
->flip('horizontal');
Image::load('filename')
->flip('both');
|
Adds a watermark to the image.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$filename |
Required |
The location of the image file to use as a watermark. |
$position |
Required |
The position of the watermark, accepts "(top|center|middle|bottom) (left|center|middle|bottom)". |
$padding |
5
|
The amount of padding from the edge, in pixels. |
|
Returns |
Image_Driver |
Example |
Image::load('filename.gif')
->watermark('watermark.ext', "top left", 15);
Image::load('filename.gif')
->watermark('watermark.ext', "bottom right");
Image::load('filename.gif')
->watermark('watermark.ext', "center middle");
|
Adds a border to the image..
Static |
Yes |
Parameters |
Param |
Default |
Description |
$size |
Required |
The size of the border in pixels. |
$color |
null
|
The color of the border, defaults to the background color. |
|
Returns |
Image_Driver |
Example |
Image::load('filename.gif')
->border(10, '#000000');
Image::load('filename.gif')
->border(5, '#FF0000')
->border(5, '#00FF00')
->border(5, '#0000FF');
|
Applies a mask to the image by blending the alpha channel of the mask with those of the loaded image.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$maskimage |
Required |
The location of the image to mask with. |
|
Returns |
Image_Driver |
Example |
Image::load('filename.gif')
->mask('mask.ext');
|
Applies rounded corners to the image.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$radius |
Required |
The location of the image to mask with. |
$sides |
null
|
Accepts any combination of "tl tr bl br" seperated by spaces, or null for all sides |
$antialias |
1
|
The distance away from the actual circle to anti-alias. 0 disables anti-aliasing. |
|
Returns |
Image_Driver |
Example |
Image::load('filename.gif')
->rounded(10);
Image::load('filename.gif')
->rounded(10, "tl tr");
Image::load('filename.gif')
->rounded(10, null, 0);
|
Returns sizes for the currently loaded image, or the image given in the $filename.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$filename |
null
|
The location of the file to get sizes for.. |
|
Returns |
stdClass |
Example |
$sizes = Image::sizes('filename.gif');
Object
(
[width] => 500
[height] => 400
)
|
Returns the extension of the file, which represents the type and is discovered at construction.
Static |
No |
Parameters |
N/A
|
Returns |
string |
Example |
$ext = Image::load('uploaded_file.jpg')
->extension();
Image::load('placeholder.png')
->output($ext);
|
Turns the image into a grayscale version.
Static |
Yes |
Parameters |
N/A |
Returns |
Image_Driver |
Example |
Image::load('filename.gif')
->grayscale();
|
Saves the image, and optionally attempts to set permissions.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$filename |
null
|
The location where to save the image. If filename is null, loaded image filename will be used. If no extension is added, one will be appended based on the loaded file's extension. |
$permissions |
null
|
Accepts a unix-style permissions (Ex: 755), or null to not set permissions |
|
Returns |
void |
Example |
Image::load('filename.gif')
->save('filename2');
Image::load('filename.gif')
->save('filename2.png');
Image::load('filename.gif')
->save('filename2', 755);
|
Saves the image to the same location with a prepended and/or appended filename, and optionally attempts to set permissions.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$append |
Required |
The string to add to the beginning of the filename. |
$prepend |
null
|
The string to add to the end of the filename, and before the extension. |
$extension |
null
|
Can set the new images extension, defaults to the loaded images extension if null. |
$permissions |
null
|
Accepts a unix-style permissions (Ex: 755), or null to not set permissions |
|
Returns |
void |
Example |
Image::load('filename.gif')
->save_pa('prepend_', '_append');
|
Outputs the image directly and sets headers.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$filetype |
null
|
The filetype to output the image as (Ex: png, gif, jpeg, ect). Defaults to the loaded file's extension. |
|
Returns |
void |
Example |
Image::load('filename.gif')
->output();
Image::load('filename.gif')
->output('jpeg');
|