Image


Image - 图1

Overview

The Phalcon\Image namespace exposes adapter that offer image manipulating functionality. These adapters are designed to allow multiple operations to be performed on the same image.

Adapters

This component uses adapters that offer methods to manipulate images. You can easily create your own adapter using the Phalcon\Image\Adapter\AdapterInterface.

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

Constants

Phalcon\Image\Enum holds constants for image resizing and flipping. The available constants are:

Resize

  • AUTO
  • HEIGHT
  • INVERSE
  • NONE
  • PRECISE
  • TENSILE
  • WIDTHFlip

  • HORIZONTAL

  • VERTICAL

Getters

Each adapter offers getters to provide information about the component:

  • getHeight() - int - Returns the image height
  • getImage() - mixed - Returns the image
  • getMime() - string - Returns the image mime type
  • getRealpath() - string - Returns the real path where the image is located
  • getType() - int - Returns the image type (This is driver dependent)
  • getWidth() - int - Returns the image width

GD

Phalcon\Image\Adapters\Gd utilizes the GD PHP extension. In order for you to use this adapter, the extension has to be present in your system. The adapter offers all the methods described below in the operations section.

Imagick

Phalcon\Image\Adapters\Imagick utilizes the ImageMagick PHP extension. In order for you to use this adapter, the extension has to be present in your system. The adapter offers all the methods described below in the operations section.

Operations

background()

Sets the background color for the image. The available parameters are:

  • color - string - the color in hex format
  • opacity - int - the opacity (optional - default 100).
  1. <?php
  2. use Phalcon\Image\Adapter\Gd;
  3. $image = new Gd('image.jpg');
  4. $image->background('#000033', 70);
  5. $image->save('background-image.jpg');

blur()

Blurs the image. The passed integer parameter specifies the radius for the blur operation. The range is between 0 (no effect) and 100 (very blurry):

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

crop()

You can crop images programmatically. The crop() method accepts the following parameters:

  • width - int - the width
  • height - int - the height
  • offsetX - int - the X offset (optional)
  • offsetY - int - the Y offset (optional)The following example crops 100px by 100px from the center of the image:
  1. <?php
  2. use Phalcon\Image\Adapter\Gd;
  3. $image = new Gd('image.jpg');
  4. $width = 100;
  5. $height = 100;
  6. $offsetX = ($image->getWidth() - $width) / 2;
  7. $offsetY = ($image->getHeight() - $height) / 2;
  8. $image->crop($width, $height, $offsetX, $offsetY);
  9. $image->save('crop-image.jpg');

flip()

You can flip an image horizontally or vertically. The flip() method accepts an integer, signifying the direction. You can use the constants for this operation:

  • Phalcon\Image\Enum::HORIZONTAL
  • Phalcon\Image\Enum::VERTICAL
  1. <?php
  2. use Phalcon\Image\Adapter\Gd;
  3. use Phalcon\Image\Enum;
  4. $image = new Gd('image.jpg');
  5. $image->flip(Enum::HORIZONTAL);
  6. $image->save('flip-image.jpg');

liquidRescale()

This method is only available in the Phalcon\Image\Imagick adapter. It uses the liquid rescaling method to rescale the image. The method accepts the following parameters:

  • width - int - the new width
  • height - int - the new height
  • deltaX - int - How much the seam can traverse on x-axis. Passing 0 causes the seams to be straight. (optional - default 0)
  • rigidity - int - Introduces a bias for non-straight seams. (optional - default 0).
  1. <?php
  2. use Phalcon\Image\Adapter\Gd;
  3. $image = new Gd('image.jpg');
  4. $image->liquidRescale(500, 200, 3, 25);
  5. $image->save('liquidrescale-image.jpg');

mask()

Creates a composite image from two images. Accepts the first image as a parameter.

  1. <?php
  2. use Phalcon\Image\Adapter\Gd;
  3. $front = new Gd('front.jpg');
  4. $back = new Gd('back.jpg');
  5. $front->mask($front);
  6. $front->save('mask-image.jpg');

pixelate()

Adds pixelation to the image. The method accepts a single integer parameter. The higher the number, the more pixelated the image becomes:

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

reflection()

Adds reflection to the image. The method accepts the following parameters:

  • height - int - the height
  • opacity - int - the opacity (optional - default 100)
  • fadeIn - bool - whether to fade in or not (optional - default false)
  1. <?php
  2. use Phalcon\Image\Adapter\Gd;
  3. $image = new Gd('image.jpg');
  4. $image->reflection(100, 75, true);
  5. $image->save('reflection-image.jpg');

render()

Renders the image and returns it back as a binary string. The method accepts the following parameters:

  • ext - string - the extension (optional)
  • quality - int - the quality of the image (optional - default 100)
  1. <?php
  2. use Phalcon\Image\Adapter\Gd;
  3. $image = new Gd('image.jpg');
  4. // ....
  5. echo $image->render('jpg', 90);

resize()

Resizes the image based on the passed parameters. The method accepts the following parameters:

  • width - int - the width (optional)
  • height - int - the height (optional)
  • master - int - constant signifying the resizing method (default AUTO)
    • Phalcon\Image\Enum::AUTO
    • Phalcon\Image\Enum::HEIGHT
    • Phalcon\Image\Enum::INVERSE
    • Phalcon\Image\Enum::NONE
    • Phalcon\Image\Enum::PRECISE
    • Phalcon\Image\Enum::TENSILE
    • Phalcon\Image\Enum::WIDTHIf any of the parameters are not correct, a Phalcon\Image\Exception will be thrown.

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. use Phalcon\Image\Adapter\Gd;
  3. use Phalcon\Image\Enum;
  4. $image = new Gd('image.jpg');
  5. $image->resize(300, null, Enum::HEIGHT);
  6. $image->save('resize-height-image.jpg');

INVERSE

Resizes and inverts the width and height passed

  1. <?php
  2. use Phalcon\Image\Adapter\Gd;
  3. use Phalcon\Image\Enum;
  4. $image = new Gd('image.jpg');
  5. $image->resize(400, 200, Enum::INVERSE);
  6. $image->save('resize-inverse-image.jpg');

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. use Phalcon\Image\Adapter\Gd;
  3. use Phalcon\Image\Enum;
  4. $image = new Gd('image.jpg');
  5. $image->resize(400, 200, Enum::NONE);
  6. $image->save('resize-none-image.jpg');

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. use Phalcon\Image\Adapter\Gd;
  3. use Phalcon\Image\Enum;
  4. $image = new Gd('image.jpg');
  5. $image->resize(400, 200, Enum::TENSILE);
  6. $image->save('resize-tensile-image.jpg');

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. use Phalcon\Image\Adapter\Gd;
  3. use Phalcon\Image\Enum;
  4. $image = new Gd('image.jpg');
  5. $image->resize(300, null, Enum::WIDTH);
  6. $image->save('resize-width-image.jpg');

rotate()

Rotates an image based on the given degrees. Positive numbers rotate the image clockwise while negative counter clockwise.

The following example rotates an image by 90 degrees clockwise

  1. <?php
  2. use Phalcon\Image\Adapter\Gd;
  3. $image = new Gd('image.jpg');
  4. $image->rotate(90);
  5. $image->save('rotate-image.jpg');

save()

After manipulating your image, you will most likely want to save it. If you wish to just get the result of the manipulations back as a string, you can use the render() method.

The save() method accepts the filename and quality as parameters:

  • file - string - the target file name (optional)
  • quality - int - the quality of the image (optional - default -1)If a file name is not specified, the manipulated image will overwrite the original image.
  1. <?php
  2. use Phalcon\Image\Adapter\Gd;
  3. $image = new Gd('image.jpg');
  4. $image->rotate(90);
  5. $image->save();

When specifying a file name, the manipulated image will be saved with that name, leaving the original image unchanged.

  1. <?php
  2. use Phalcon\Image\Adapter\Gd;
  3. $image = new Gd('image.jpg');
  4. $image->rotate(90);
  5. $image->save('rotate-image.jpg');

You can also change the format of the image using a different extension. This functionality depends on the adapter you are working with.

  1. <?php
  2. use Phalcon\Image\Adapter\Gd;
  3. $image = new Gd('image.jpg');
  4. $image->rotate(90);
  5. $image->save('rotate-image.png');

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

  1. <?php
  2. use Phalcon\Image\Adapter\Gd;
  3. $image = new Gd('image.jpg');
  4. $image->rotate(90);
  5. $image->save('rotate-image.jpg', 90);

sharpen()

Sharpens the image. The passed integer parameter specifies the amount for the blur operation. The range is between 0 (no effect) and 100 (very sharp):

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

text()

You can add text to your image by calling text(). The available parameters are:

  • text - string - the text
  • offsetX - int/false - the X offset, false to disable
  • offsetY - int/false - the Y offset, false to disable
  • opacity - int - the opacity of the text (optional - default 100)
  • color - string - the color for the text (optional - default "000000")
  • size - int - the size of the font for the text (optional - default 12)
  • fontfile - string - the font file to be used for the text (optional)
  1. <?php
  2. use Phalcon\Image\Adapter\Gd;
  3. $image = new Gd('image.jpg');
  4. $image->text(
  5. 'Phalcon Framework',
  6. 10,
  7. 10,
  8. 75,
  9. '000033',
  10. 14,
  11. '/app/assets/fonts/titilium.tff'
  12. );
  13. $image->save('text-image.jpg');

watermark()

Adds a watermark to an image. The available parameters are:

  • watermark - AdapterInterface - the image to use for the watermark
  • offsetX - int - the X offset (optional)
  • offsetY - int - the Y offset (optional)
  • opacity - int - the opacity of the text (optional - default 100)The following example puts the watermark at the top left corner of the image:
  1. <?php
  2. use Phalcon\Image\Adapter\Gd;
  3. $watermark = new Gd('watermark.jpg');
  4. $image = new Gd('image.jpg');
  5. $offsetX = 10;
  6. $offsetY = 10;
  7. $opacity = 70;
  8. $image->watermark(
  9. $watermark,
  10. $offsetX,
  11. $offsetY,
  12. $opacity
  13. );
  14. $image->save('watermark-image.jpg');

You can also manipulate the watermarked image before applying it to the main image. In the following example we resize, rotate and sharpen the watermark and put it at the bottom right corner with a 10px margin:

  1. <?php
  2. use Phalcon\Image\Adapter\Gd;
  3. $watermark = new Gd('watermark.jpg');
  4. $image = new Gd('image.jpg');
  5. $watermark->resize(100, 100);
  6. $watermark->rotate(90);
  7. $watermark->sharpen(5);
  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('watermark-image.jpg');

Factory

newInstance

The Phalcon\Image\ImageFactory offers an easy way to create image adapter objects. There are two adapters already preset for you:

  1. <?php
  2. use Phalcon\Image\ImageFactory;
  3. $factory = new ImageFactory();
  4. $image = $factory->newInstance('gd', 'image.jpg');

The available parameters for newInstance() are:

  • name - string - the name of the adapter
  • file - string - the file name
  • width - int - the width of the image (optional)
  • height - int - the height of the image (optional)

load

The Image Factory also offers the load method, which accepts a configuration object. This object can be an array or a Phalcon\Config object, with directives that are used to set up the image adapter. The object requires the adapter element, as well as the file element. width and height can also be set as options.

  1. <?php
  2. use Phalcon\Image\ImageFactory;
  3. $factory = new ImageFactory();
  4. $options = [
  5. 'adapter' => 'gd',
  6. 'file' => 'image.jpg',
  7. 'width' => 400,
  8. 'height' => 200,
  9. ];
  10. $image = $factory->load($options);

Exceptions

Any exceptions thrown in the Image components will be of type Phalcon\Image\Exception. You can use this exception to selectively catch exceptions thrown only from this component.

  1. <?php
  2. use Phalcon\Image\Adapter\Gd;
  3. use Phalcon\Image\Exception;
  4. use Phalcon\Mvc\Controller;
  5. class IndexController extends Controller
  6. {
  7. public function index()
  8. {
  9. try {
  10. $image = new Gd('image.jpg');
  11. $image->pixelate(10);
  12. $image->save('pixelated-image.jpg');
  13. } catch (Exception $ex) {
  14. echo $ex->getMessage();
  15. }
  16. }
  17. }

Custom

The Phalcon\Image\Adapter\AdapterInterface interface must be implemented in order to create your own image adapters or extend the existing ones. You can then easily add it to the Phalcon\Image\ImageFactory.

  1. <?php
  2. use Phalcon\Image\Adapter\AdapterInterface;
  3. use Phalcon\Image\Enum;
  4. class MyImageAdapter implements AdapterInterface
  5. {
  6. /**
  7. * Manipulate the background
  8. */
  9. public function background(
  10. string $color,
  11. int $opacity = 100
  12. );
  13. /**
  14. * Blur the image
  15. */
  16. public function blur(int $radius);
  17. /**
  18. * Crop the image
  19. */
  20. public function crop(
  21. int $width,
  22. int $height,
  23. int $offsetX = null,
  24. int $offsetY = null
  25. );
  26. /**
  27. * Flip the image
  28. */
  29. public function flip(int $direction);
  30. /**
  31. * Add a mask to the image
  32. */
  33. public function mask(AdapterInterface $watermark);
  34. /**
  35. * Pixelate the image
  36. */
  37. public function pixelate(int $amount);
  38. /**
  39. * Add a reflection to the image
  40. */
  41. public function reflection(
  42. int $height,
  43. int $opacity = 100,
  44. bool $fadeIn = false
  45. );
  46. /**
  47. * Render th eimage
  48. */
  49. public function render(
  50. string $ext = null,
  51. int $quality = 100
  52. );
  53. /**
  54. * Resize the image
  55. */
  56. public function resize(
  57. int $width = null,
  58. int $height = null,
  59. int $master = Enum::AUTO
  60. );
  61. /**
  62. * Rotate the image
  63. */
  64. public function rotate(int degrees);
  65. /**
  66. * Save the image
  67. */
  68. public function save(string $file = null, int $quality = 100);
  69. /**
  70. * Sharpen the image
  71. */
  72. public function sharpen(int $amount);
  73. /**
  74. * Add text to the image
  75. */
  76. public function text(
  77. string $text,
  78. int $offsetX = 0,
  79. int $offsetY = 0,
  80. int $opacity = 100,
  81. string $color = "000000",
  82. int $size = 12,
  83. string $fontfile = null
  84. );
  85. /**
  86. * Add a watermark tot he image
  87. */
  88. public function watermark(
  89. AdapterInterface $watermark,
  90. int $offsetX = 0,
  91. int $offsetY = 0,
  92. int $opacity = 100
  93. );
  94. }