CI_Image_lib

class CI_Image_lib

initialize([$props = array()])
Parameters:
  • $props (array) – Image processing preferences
Returns:

TRUE on success, FALSE in case of invalid settings

Return type:

bool

Initializes the class for processing an image.

resize()
Returns: TRUE on success, FALSE on failure
Return type: bool

The image resizing method lets you resize the original image, create a copy (with or without resizing), or create a thumbnail image.

For practical purposes there is no difference between creating a copy and creating a thumbnail except a thumb will have the thumbnail marker as part of the name (i.e. mypic_thumb.jpg).

All preferences listed in the Preferences table are available for this method except these three: rotation_angle, x_axis and y_axis.

Creating a Thumbnail

The resizing method will create a thumbnail file (and preserve the original) if you set this preference to TRUE:

$config['create_thumb'] = TRUE;

This single preference determines whether a thumbnail is created or not.

Creating a Copy

The resizing method will create a copy of the image file (and preserve the original) if you set a path and/or a new filename using this preference:

$config['new_image'] = '/path/to/new_image.jpg';

Notes regarding this preference:

  • If only the new image name is specified it will be placed in the same folder as the original
  • If only the path is specified, the new image will be placed in the destination with the same name as the original.
  • If both the path and image name are specified it will placed in its own destination and given the new name.

Resizing the Original Image

If neither of the two preferences listed above (create_thumb, and new_image) are used, the resizing method will instead target the original image for processing.

crop()
Returns: TRUE on success, FALSE on failure
Return type: bool

The cropping method works nearly identically to the resizing function except it requires that you set preferences for the X and Y axis (in pixels) specifying where to crop, like this:

$config['x_axis'] = 100;
$config['y_axis'] = 40;

All preferences listed in the Preferences table are available for this method except these: rotation_angle, create_thumb and new_image.

Here’s an example showing how you might crop an image:

$config['image_library'] = 'imagemagick';
$config['library_path'] = '/usr/X11R6/bin/';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['x_axis'] = 100;
$config['y_axis'] = 60;

$this->image_lib->initialize($config);

if ( ! $this->image_lib->crop())
{
        echo $this->image_lib->display_errors();
}

Note

Without a visual interface it is difficult to crop images, so this method is not very useful unless you intend to build such an interface. That’s exactly what we did using for the photo gallery module in ExpressionEngine, the CMS we develop. We added a JavaScript UI that lets the cropping area be selected.

rotate()
Returns: TRUE on success, FALSE on failure
Return type: bool

The image rotation method requires that the angle of rotation be set via its preference:

$config['rotation_angle'] = '90';

There are 5 rotation options:

  1. 90 - rotates counter-clockwise by 90 degrees.
  2. 180 - rotates counter-clockwise by 180 degrees.
  3. 270 - rotates counter-clockwise by 270 degrees.
  4. hor - flips the image horizontally.
  5. vrt - flips the image vertically.

Here’s an example showing how you might rotate an image:

$config['image_library'] = 'netpbm';
$config['library_path'] = '/usr/bin/';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['rotation_angle'] = 'hor';

$this->image_lib->initialize($config);

if ( ! $this->image_lib->rotate())
{
        echo $this->image_lib->display_errors();
}
watermark()
Returns: TRUE on success, FALSE on failure
Return type: bool

Creates a watermark over an image, please refer to the Watermarking an Image section for more info.

clear()
Return type: void

The clear method resets all of the values used when processing an image. You will want to call this if you are processing images in a loop.

$this->image_lib->clear();
display_errors([$open = '

[, $close = '

']])

Parameters:
  • $open (string) – Error message opening tag
  • $close (string) – Error message closing tag
Returns:

Error messages

Return type:

string

Returns all detected errors formatted as a string.

echo $this->image_lib->display_errors();
doc_CodeIgniter
2016-10-15 16:31:37
Comments
Leave a Comment

Please login to continue.