PHP ImageMagick - Splicing & Spreading



In this chapter, you will be learning to spread an image and splice an image using a few inbuilt functions of Imagemagick.

Image Spreading

In this section, you will be learning to spread an image easily using the ‘spreadImage()’ function provided by Imagemagick. Spreading an image is randomly displacing each pixel in a block.

Syntax

public Imagick::spreadImage(float $radius): bool

This function takes in only one parameter: radius. ‘Radius’ is a float value that specifies the value to displace each pixel in a block.

Example

In the below example, an imagick object is created and an image is taken as input. Now, ‘spreadImage()’ function is applied on the image with a single parameter(radius=5). Then, the final image is displayed as output.

<?php
   $image=new Imagick($_SERVER['DOCUMENT_ROOT']."/test/imagee.png");
   $image->spreadImage(5);
   $image->writeImage($_SERVER['DOCUMENT_ROOT']."/test/spreadImage.png");
?>

Assume that the following is the input image (image.png) in the program −

Image Spreading

Output

Image Spreading

Image Splicing

In this chapter, you will be learning to splice a solid color into the image using an inbuilt function named ‘spliceImage()’ in Imagemagick. This function takes an image as input, and splices a solid color into the image with the specified parameters (dimensions and positions of the splice).

Syntax

public Imagick::spliceImage(
   int $width,
   int $height,
   int $x,
   int $y
): bool

This function contains 4 parameters: width, height, x, and y. ‘Width’ and ‘height’ are integer values that specify the width and height of the splice respectively. ‘x’ and ‘y’ are also integer values that specify the position on the X-axis and Y-axis respectively.

Example

In the below example, an imagick object is created and image is taken as input. On that image, ‘spliceImage()’ function is applied with the parameters (width=50, height=100, x=100, y=50). Then, the image after splicing is obtained as output.

<?php
   $image=new Imagick($_SERVER['DOCUMENT_ROOT']."/test/image.png");
   $image->spliceImage(50, 100, 100, 50);
   $image->writeImage($_SERVER['DOCUMENT_ROOT']."/test/spliceImage.png");
?>

Assume that the following is the input image (image.png) in the program −

Image Splicing

Output

Image Splicing
Advertisements