Share » Learn » eZ Publish » Image Manipulation with eZ Components

Image Manipulation with eZ Components

Monday 16 January 2006 8:02:00 am

  • Currently 3 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Now comes a demonstration of how you can combine filters to images. Therefore, we will make a script which crops an image for better composition and converts it to grayscale.

Notice that the sample code is very similar to the scaling example we started with. This time, we have defined two more image filters: crop and colorspace. The crop filter will take a small part of the image, then the colorspace filter will turn the whole image into grayscale.

Additional Filters

Since much of the code is practically the same as the first example we can focus on the definition of the conversion filters in $filters. This is an array which contains three instances of the ezcImageFilter class.

The crop filter is defined by a start position in the original image measured by pixels on the x and y axis. The pixel size of the resulting crop is defined by the width and the height parameters. The colorspace filter simply converts the image to the defined colorspace. In this example we use the constant ezcImageColorspaceFilters::COLORSPACE_GREY to make the image gray scale.

The scale filter remains the same as in the first example. Accurately ordering the filters is important, as the first filter defined is the filter which is first executed. Hence, mixing up the order of crop and scale will cause undesired results.

Sample Script and Result

<?php
 
// Load autoloader functionality
require_once 'Base/base.php';
function __autoload( $className )
{
  ezcBase::autoload( $className );
}
 
// Define which handler to use for the image manipulation.
$settings = new ezcImageConverterSettings(
    array(
       new ezcImageHandlerSettings( 'GD', 'ezcImageGdHandler' ),
        new ezcImageHandlerSettings( 'ImageMagick', 'ezcImageImagemagickHandler' )
        )
    );
 
// Create the converter object.
$converter = new ezcImageConverter( $settings );
 
$filters = array(
    new ezcImageFilter(
        'crop',
        array( 'x' => 740,
               'width' => 1300,
               'y' => 720,
               'height' => 900
               )
        ),
    new ezcImageFilter(
        'colorspace',
        array(
            'space' => ezcImageColorspaceFilters::COLORSPACE_GREY
            )
        ),
    new ezcImageFilter(
        'scale',
        array( 'width'    => 500,
               'height'   => 350,
               'direction' => ezcImageGeometryFilters::SCALE_DOWN
               )
        )
    );
 
// Create the transformation inside the manager
$converter->createTransformation( 'crop', $filters, array( 'image/jpeg' ) );
 
// Transform an image.
$converter->transform( 'crop', "vulcher.jpg", "vulcher_converted.jpg" );
 
print( "<p><img src='vulcher_converted.jpg' /></p>" );
 
?>

The original image used in this example got colors and shows two birds:

This is how the converted image looks like, converted to gray and showing a small part of the original image, being the bird on the left side:

36 542 Users on board!

Tutorial menu

Printable

Printer Friendly version of the full article on one page with plain styles

Author(s)