30% Therapy – 40% Practice – 30% Work project

PHP ImageMagick – Image Bordering



Every picture that is displayed on a digital display has black or white, or colored backgrounds. So, making a separation between the background and the photo is essential to define the visual limits of the image.

This necessity of making separation is even more in black or white backgrounds. So, to serve this purpose, borders must be added to photos. In this chapter, you will learn to add borders to images in PHP using a few inbuilt functions of Imagemagick.

Adding 3D Border

A 3D border can be added to an image using an inbuilt function ‘frameImage()’ provided by Imagemagick.

Syntax

public Imagick::frameImage(mixed $matte_color, int $width, int $height, int $inner_bevel, int $outer_bevel): bool

This method takes 5 parameters

  • matte-color − It is a string representing matte color.

  • width − represents the width of the border.

  • height − represents the height of the border.

  • inner_bevel − represents the width of inner bevel.

  • outer_bevel − represents the width of outer bevel.

The output obtained is an image with a border frame with the specified measurements and color.

Example

The following program shows how to use the Imagick library in PHP to create a new image file. It creates an object of the Imagick class using an existing image, adds a frame around it with specific parameters (in this case pink and 100×100 pixels with 10px border), then writes the modified image as a PNG file.

<?php
   $image=new Imagick($_SERVER[''DOCUMENT_ROOT'']."/test/image.jpg");
   $image->frameImage(''pink'', 100, 100, 10, 10);
   $image->writeImage($_SERVER[''DOCUMENT_ROOT'']."/test/newimage.png");
?>

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

Adding 3D Border

Output

Adding 3D Border

Adding a Normal Border

Imagemagick has provided a method ‘borderImage()’ which adds a border to an image. It takes an image as input and produces an image with a border, as output.

Syntax

public Imagick::borderImage(mixed $bordercolor, int $width, int $height): bool

This function has 3 parameters which are border-color, width, and height.

  • The bordercolor is an imagickpixel object or a string containing border color.

  • The width and height are integer values holding the width and height of the border.

Example

The following program shows how to use the Imagick library in PHP to create an image with a 25px yellow border. It reads the image from the ”test/image.png” file and writes it out as ”test/borderImage.png” with the added border.

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

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

Adding a Normal Border

Output

Adding a Normal Border

Translate »