Media Image is a php class who help developers to transform images.
This class is a part of the Ace Framework project.

Media Image require the php_gd extension to work.

Basic usage

At first, you must create a new instance of the class Ace_Media_Image specifying the base image file to use.

$image = new Ace_Media_Image('/var/html/www/image/test.jpg');

Now you can use these functions to transform your image :

After that, you must return the processed image. For this you have two options, the first is to return the image as file :

$image = new Ace_Media_Image('/var/html/www/image/test.jpg');
$image->resize(50, 50);
$image->save('/var/html/www/image/new_test.jpg');

The second solution is to return the image to the response header :

$image = new Ace_Media_Image('/var/html/www/image/test.jpg');
$image->resize(50, 50);

header('content-type:' . $image->getMimeType());
$image->output();
exit;

Resize

To resize an image you have to use the resize() method.

Simple resize

In the example below the left image has been resized from 110x75 to 50x50 without keeping the proportions of the image. It is therefore deformed.

$image = new Ace_Media_Image('/var/html/www/image/test.jpg');
$image->resize(50, 50);

Keep image proportions on resize

The resize() method is designed to resize an image without losing its proportions. The following examples will show you the possibilities.
Below, the image has been resized. The width is the reference for the aspect ratio.

$image = new Ace_Media_Image('/var/html/www/image/test.jpg');
$image->resize(50, 50, Ace_Media_Image::RATIO_WIDTH);

Below, the image has been resized. The height is the reference for the aspect ratio, so the destination is larger than the previous exemple.

$image = new Ace_Media_Image('/var/html/www/image/test.jpg');
$image->resize(50, 50, Ace_Media_Image::RATIO_HEIGHT);

Below, the image has been resized. The both size are the reference for the aspect ratio, so the output image is optimised to fit between the destination sizes.

$image = new Ace_Media_Image('/var/html/www/image/test.jpg');
$image->resize(50, 50, Ace_Media_Image::RATIO_BOTH);

Flip

If you want to flip an image, you have to use the flip() method.
Below, the image has been flipped with an horizontal axis.

$image = new Ace_Media_Image('/var/html/www/image/test.jpg');
$image->flip(Ace_Media_Image::FLIP_HORIZONTAL);

Below, the image has been flipped with a vertical axis.

$image = new Ace_Media_Image('/var/html/www/image/test.jpg');
$image->flip(Ace_Media_Image::FLIP_VERTICAL);

Below, the image has been flipped with an horizontal axis then a vertical axis. This also is equivalent to a 180° rotation.

$image = new Ace_Media_Image('/var/html/www/image/test.jpg');
$image->flip(Ace_Media_Image::FLIP_BOTH);

Rotate

If you want to rotate an image, you have to use the rotate() method.
Below, the image has been rotated with an 45° angle.

$image = new Ace_Media_Image('/var/html/www/image/test.jpg');
$image->rotate(45);

Cut

If you want to cut an image, you have to use the cut() method.

Horizontal cut

To change the horizontal cut zone you have 3 possibles constants :

  • CUT_HORIZONTAL_LEFT
  • CUT_HORIZONTAL_CENTER
  • CUT_HORIZONTAL_RIGHT

$image = new Ace_Media_Image('/var/html/www/image/test.jpg');
$image->cut(100, 50, Ace_Media_Image::CUT_HORIZONTAL_LEFT);

$image = new Ace_Media_Image('/var/html/www/image/test.jpg');
$image->cut(100, 50, Ace_Media_Image::CUT_HORIZONTAL_CENTER);

$image = new Ace_Media_Image('/var/html/www/image/test.jpg');
$image->cut(100, 50, Ace_Media_Image::CUT_HORIZONTAL_RIGHT);

Vertical cut

To change the vertical cut zone you have 3 possibles constants :

  • CUT_VERTICAL_TOP
  • CUT_VERTICAL_MIDDLE
  • CUT_VERTICAL_BOTTOM

$image = new Ace_Media_Image('/var/html/www/image/test.jpg');
$image->cut(100, 50, Ace_Media_Image::CUT_HORIZONTAL_CENTER, Ace_Media_Image::CUT_VERTICAL_TOP);

$image = new Ace_Media_Image('/var/html/www/image/test.jpg');
$image->cut(100, 50, Ace_Media_Image::CUT_HORIZONTAL_CENTER, Ace_Media_Image::CUT_VERTICAL_MIDDLE);

$image = new Ace_Media_Image('/var/html/www/image/test.jpg');
$image->cut(100, 50, Ace_Media_Image::CUT_HORIZONTAL_CENTER, Ace_Media_Image::CUT_VERTICAL_BOTTOM);

Crop

If you want to crop an image, you have to use the crop() method.

$image = new Ace_Media_Image('/var/html/www/image/test.jpg');
$image->crop(int $base_width, int $dest_w, int $dest_h, int $x = 0, int $y = 0, int $w = 0, int $h = 0);

  • base_width, reference width, in most of case that's the image width
  • dest_w, width of the destination image
  • dest_h, height of the destination image
  • x, horizontal coordinate (in pixel) from the left of the image to the crop frame
  • y, vertical coordinate (in pixel) from the top of the image to the crop frame
  • w, width of the crop frame
  • h, height of the crop frame

If w and h parameters are not defined, the size of the crop frame will be the same as the destination image sizes.

Stamp

If you want add a copyright to your image, you have to use the stamp() method.

The stamp image is not resized.

$image = new Ace_Media_Image('/var/html/www/image/test.jpg');
$image->stamp('/var/html/www/image/stamp.png', 10, 10);

The two numbers after the first parameter are the X and Y coordinates.

Download

Changelog

Ace Media Image 1.1 - 8 january 2014
    - Fix available memory calculation to consider the current memory used
    - Fix PHP 5.5 support
    - Add stamp function