PHP ImageMagick - Miscellaneous Functions



Applying Functions

We have seen about predefined constants in ImageMagick in the "Predefined Constants" chapter. To apply these function constants, ImageMagick provides an inbuilt function `functionImage()` with three functions: undefined, polynomial and sinusoid. You can use this function to apply these constants to images.

Syntax

public Imagick::functionImage(int $function, array $arguments, int $channel = Imagick::CHANNEL_DEFAULT): bool

This function mainly contains 2 parameters: function and arguments.

  • Function refers to the list of function constants (sinusoid or polynomial).

  • Arguments specifies the arguments to be passed to the function in an array format.

To apply the function, we need to create a new image using the ‘newPseudoImage()’ function and on that image, this function needs to be applied by specifying the parameters. It doesn’t take any input but produces the output after applying the function.

Example

In this example, you can see the implementation of the function ‘functionImage()’ in Imagemagick. This code consists of the creation of a new Imagick object and taking an image as input.

A new image is created using ‘newPseudoImage()’ function with the required measurements and then the function is applied. The function to be applied is specified as one of the parameters in the ‘functionImage()’ function. The output image obtained is displayed using ‘writeImage()’.

<?php
   $image=new Imagick($_SERVER['DOCUMENT_ROOT']."/test/image5.jpeg");
   $image->newPseudoImage(500, 400, 'gradient:white-brown');
   $image->functionImage(Imagick::FUNCTION_SINUSOID, array(19, 33));
   $image->writeImage($_SERVER['DOCUMENT_ROOT']."/test/functionImage.png");
?>

Output

Applying Functions

Image Annotation

To recognize the whole image or meaning of the image, the image must be annotated. Image annotation is a way of explanation in the form of text that is added to an image. For this purpose, there is a function ‘annotateImage()’ provided in the Imagemagick library.

Syntax

public Imagick::annotateImage(ImagickDraw $draw_settings,float $x, float $y, float $angle, string $text): bool

This function has 5 parameters which are draw-settings, x, y, angle, and text. draw_settings’ is an ImageMagick object that contains settings for drawing the text.

  • x is the horizontal offset in pixels to the left of the text.

  • y is the vertical offset in pixels to the baseline of the text.

  • angle specifies the angle at which the text must be written

  • text is the string to draw.

This function takes an image as input and the image with some annotated text is obtained as output.

Example

In the following example, we create a new Imagick object ($img) and pass the image to it. Then, create a new ImagickDraw object ($draw), and set the font size on that draw object.

Finally, use 'annotateImage()' on your created Imagick object ('$img'), passing in your drawobject, x-coordinate, y-coordinate, angle and text as parameters. The output obtained is in the form of 'annotateImage.png'.

<?php
   $draw=new ImagickDraw();
   $img=new Imagick($_SERVER['DOCUMENT_ROOT']."/test/image.jpeg");
   $draw->setFontSize(30);
   $img->annotateImage($draw, 525, 820, 0, 'HAPPY DIWALI');
   $image->writeImage($_SERVER['DOCUMENT_ROOT']."/test/annotateImage.png");
?>

Assume that the following is the input image (image.jpeg) in the program &minnus;

Image Annotation

Output

Image Annotation

Grouping Images

Appending a set of images is the process of grouping all the images together such that each image is attached to the end of another image and so on. To do this, Imagemagick has provided an inbuilt function ‘appendImages()’ where you can append a set of images into a larger image.

Syntax

public Imagick::appendImages(bool $stack): Imagick

This function takes in a single Boolean parameter which is ‘stack’. This value is used to decide whether to stack the images vertically or horizontally. The default value of ‘stack’ is false. It means that the images are stacked from left to right. If the ‘stack’ value is true, then the images are stacked from top to bottom

Example

The below example implements the ‘appendImages()’ function in PHP. In this example, 5 new images are created with same measurements but different colors. Then, pixel iterator is reset using ‘resetIterator()’ and then, all the created images are appended using ‘appendImages()’ function and the output is obtained.

<?php
   $image=new Imagick();
   $image->newImage(100, 100, "black");
   $image->newImage(100, 100, "white");
   $image->newImage(100, 100, "black");
   $image->newImage(100, 100, "white");
   $image->newImage(100, 100, "black");
   $image->resetIterator();
   $combined=$image->appendImages(false);
   $combined->writeImage($_SERVER['DOCUMENT_ROOT']."/test/appendImages.png");
?>

Output

Grouping Images
Advertisements