Images

Phalcon\Image is the component that allows you to manipulate image files. Multiple operations can be performed on the same image object.

This guide is not intended to be a complete documentation of available methods and their arguments. Please visit the API for a complete reference.

Adapters

This component makes use of adapters to encapsulate specific image manipulator programs. The following image manipulator programs are supported:

ClassDescription
Phalcon\Image\Adapter\GdRequires the GD PHP extension.
Phalcon\Image\Adapter\ImagickRequires the ImageMagick PHP extension.

Implementing your own adapters

The Phalcon\Image\AdapterInterface interface must be implemented in order to create your own image adapters or extend the existing ones.

Saving and rendering images

Before we begin with the various features of the image component, it’s worth understanding how to save and render these images.

  1. <?php
  2. $image = new \Phalcon\Image\Adapter\Gd("image.jpg");
  3. // ...
  4. // Overwrite the original image
  5. $image->save();
  1. <?php
  2. $image = new \Phalcon\Image\Adapter\Gd("image.jpg");
  3. // ...
  4. // Save to 'new-image.jpg'
  5. $image->save("new-image.jpg");

You can also change the format of the image:

  1. <?php
  2. $image = new \Phalcon\Image\Adapter\Gd("image.jpg");
  3. // ...
  4. // Save as a PNG file
  5. $image->save("image.png");

When saving as a JPEG, you can also specify the quality as the second parameter:

  1. <?php
  2. $image = new \Phalcon\Image\Adapter\Gd("image.jpg");
  3. // ...
  4. // Save as a JPEG with 80% quality
  5. $image->save("image.jpg", 80);

Resizing images

There are several modes of resizing:

  • \Phalcon\Image::WIDTH
  • \Phalcon\Image::HEIGHT
  • \Phalcon\Image::NONE
  • \Phalcon\Image::TENSILE
  • \Phalcon\Image::AUTO
  • \Phalcon\Image::INVERSE
  • \Phalcon\Image::PRECISE

\Phalcon\Image::WIDTH

The height will automatically be generated to keep the proportions the same; if you specify a height, it will be ignored.

  1. <?php
  2. $image = new \Phalcon\Image\Adapter\Gd("image.jpg");
  3. $image->resize(
  4. 300,
  5. null,
  6. \Phalcon\Image::WIDTH
  7. );
  8. $image->save("resized-image.jpg");

\Phalcon\Image::HEIGHT

The width will automatically be generated to keep the proportions the same; if you specify a width, it will be ignored.

  1. <?php
  2. $image = new \Phalcon\Image\Adapter\Gd("image.jpg");
  3. $image->resize(
  4. null,
  5. 300,
  6. \Phalcon\Image::HEIGHT
  7. );
  8. $image->save("resized-image.jpg");

\Phalcon\Image::NONE

The NONE constant ignores the original image’s ratio. Neither width and height are required. If a dimension is not specified, the original dimension will be used. If the new proportions differ from the original proportions, the image may be distorted and stretched.

  1. <?php
  2. $image = new \Phalcon\Image\Adapter\Gd("image.jpg");
  3. $image->resize(
  4. 400,
  5. 200,
  6. \Phalcon\Image::NONE
  7. );
  8. $image->save("resized-image.jpg");

\Phalcon\Image::TENSILE

Similar to the NONE constant, the TENSILE constant ignores the original image’s ratio. Both width and height are required. If the new proportions differ from the original proportions, the image may be distorted and stretched.

  1. <?php
  2. $image = new \Phalcon\Image\Adapter\Gd("image.jpg");
  3. $image->resize(
  4. 400,
  5. 200,
  6. \Phalcon\Image::NONE
  7. );
  8. $image->save("resized-image.jpg");

Cropping images

For example, to get a 100px by 100px square from the centre of the image:

  1. <?php
  2. $image = new \Phalcon\Image\Adapter\Gd("image.jpg");
  3. $width = 100;
  4. $height = 100;
  5. $offsetX = (($image->getWidth() - $width) / 2);
  6. $offsetY = (($image->getHeight() - $height) / 2);
  7. $image->crop($width, $height, $offsetX, $offsetY);
  8. $image->save("cropped-image.jpg");

Rotating images

  1. <?php
  2. $image = new \Phalcon\Image\Adapter\Gd("image.jpg");
  3. // Rotate an image by 90 degrees clockwise
  4. $image->rotate(90);
  5. $image->save("rotated-image.jpg");

Flipping images

You can flip an image horizontally (using the \Phalcon\Image::HORIZONTAL constant) and vertically (using the \Phalcon\Image::VERTICAL constant):

  1. <?php
  2. $image = new \Phalcon\Image\Adapter\Gd("image.jpg");
  3. // Flip an image horizontally
  4. $image->flip(
  5. \Phalcon\Image::HORIZONTAL
  6. );
  7. $image->save("flipped-image.jpg");

Sharpening images

The sharpen() method takes a single parameter - an integer between 0 (no effect) and 100 (very sharp):

  1. <?php
  2. $image = new \Phalcon\Image\Adapter\Gd("image.jpg");
  3. $image->sharpen(50);
  4. $image->save("sharpened-image.jpg");

Adding watermarks to images

  1. <?php
  2. $image = new \Phalcon\Image\Adapter\Gd("image.jpg");
  3. $watermark = new \Phalcon\Image\Adapter\Gd("me.jpg");
  4. // Put the watermark in the top left corner
  5. $offsetX = 10;
  6. $offsetY = 10;
  7. $opacity = 70;
  8. $image->watermark(
  9. $watermark,
  10. $offsetX,
  11. $offsetY,
  12. $opacity
  13. );
  14. $image->save("watermarked-image.jpg");

Of course, you can also manipulate the watermarked image before applying it to the main image:

  1. <?php
  2. $image = new \Phalcon\Image\Adapter\Gd("image.jpg");
  3. $watermark = new \Phalcon\Image\Adapter\Gd("me.jpg");
  4. $watermark->resize(100, 100);
  5. $watermark->rotate(90);
  6. $watermark->sharpen(5);
  7. // Put the watermark in the bottom right corner with a 10px margin
  8. $offsetX = ($image->getWidth() - $watermark->getWidth() - 10);
  9. $offsetY = ($image->getHeight() - $watermark->getHeight() - 10);
  10. $opacity = 70;
  11. $image->watermark(
  12. $watermark,
  13. $offsetX,
  14. $offsetY,
  15. $opacity
  16. );
  17. $image->save("watermarked-image.jpg");

Blurring images

The blur() method takes a single parameter - an integer between 0 (no effect) and 100 (very blurry):

  1. <?php
  2. $image = new \Phalcon\Image\Adapter\Gd("image.jpg");
  3. $image->blur(50);
  4. $image->save("blurred-image.jpg");

Pixelating images

The pixelate() method takes a single parameter - the higher the integer, the more pixelated the image becomes:

  1. <?php
  2. $image = new \Phalcon\Image\Adapter\Gd("image.jpg");
  3. $image->pixelate(10);
  4. $image->save("pixelated-image.jpg");