Image クラス
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.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$config |
array()
|
An array of configuration options for the Image_Driver instance |
|
返り値 |
Image_Driver |
例 |
$image = Image::forge();
$image = Image::forge(array(
'quality' => 80
));
$image
->load('image.png')
->output('image.jpeg');
|
Changes the value of a configuration option.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$index |
必須 |
The index to be set, or an array of configuration options. |
$value |
null
|
The value to be set if $index is not an array. |
|
返り値 |
Image_Driver |
例 |
Image::load('filename.gif')
->config('bgcolor', '#f00')
->resize(100, 100, true, true);
|
The load method attempts to load an image for editing.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$filename |
必須 |
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. |
|
返り値 |
Image_Driver |
例 |
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.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$x1 |
必須 |
The position of the first coord on the x-axis. |
$y1 |
必須 |
The position of the first coord on the y-axis. |
$x2 |
必須 |
The position of the second coord on the x-axis. |
$y2 |
必須 |
The position of the second coord on the y-axis. |
|
返り値 |
Image_Driver |
例 |
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.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$width |
必須 |
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. |
|
返り値 |
Image_Driver |
例 |
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.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$width |
必須 |
The new width of the image. |
$height |
null
|
The new height of the image |
|
返り値 |
Image_Driver |
例 |
Image::load('filename.gif')
->crop_resize(200, 200);
|
Rotates the image clockwise.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$degrees |
必須 |
The degrees to rotate the image by. Accepts positive and negatives. |
|
返り値 |
Image_Driver |
例 |
Image::load('filename.gif')
->rotate(90);
Image::load('filename.gif')
->rotate(-90);
Image::load('filename')
->rotate(450);
|
Flip the image vertically and / or horizontally.
静的 |
はい |
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.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$filename |
必須 |
The location of the image file to use as a watermark. |
$position |
必須 |
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. |
|
返り値 |
Image_Driver |
例 |
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..
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$size |
必須 |
The size of the border in pixels. |
$color |
null
|
The color of the border, defaults to the background color. |
|
返り値 |
Image_Driver |
例 |
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.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$maskimage |
必須 |
The location of the image to mask with. |
|
返り値 |
Image_Driver |
例 |
Image::load('filename.gif')
->mask('mask.ext');
|
Applies rounded corners to the image.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$radius |
必須 |
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. |
|
返り値 |
Image_Driver |
例 |
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.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$filename |
null
|
The location of the file to get sizes for.. |
|
返り値 |
stdClass |
例 |
$sizes = Image::sizes('filename.gif');
Object
(
[width] => 500
[height] => 400
)
|
Turns the image into a grayscale version.
静的 |
はい |
パラメータ |
N/A |
返り値 |
Image_Driver |
例 |
Image::load('filename.gif')
->grayscale();
|
Saves the image, and optionally attempts to set permissions.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$filename |
必須 |
The location where to save the image. 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 |
|
返り値 |
void |
例 |
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.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$append |
必須 |
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 |
|
返り値 |
void |
例 |
Image::load('filename.gif')
->save_pa('prepend_', '_append');
|
Outputs the image directly and sets headers.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$filetype |
null
|
The filetype to output the image as (Ex: png, gif, jpeg, ect). Defaults to the loaded file's extension. |
|
返り値 |
void |
例 |
Image::load('filename.gif')
->output();
Image::load('filename.gif')
->output('jpeg');
|