Drawing border around an image by using PHP and GDWe will learn how to add border to an existing image by using php gd library support. This script can be a part of a script where images uploaded by the user can be changed to add border. We will learn only the image manipulation part here to add the border.
Our script will read the width and height of the image. This way we can apply the script to any image of any size. We can use the existing image and add the border or we can create a new image by adding the border.
We are assuming that there is an existing image name a.jpg and this image is to be changed and border to be added. Here is the code to read the height and width of the image.
Our image a.jpg is stored inside the images directory. We care crating a border of width 30 for that the variable $border is assigned a value of 30. This value can be changed from a script. For example we can take the input from the user asking them what should be the border width and then assign that value to $border.
Now we have the width and height of the original image. Once we are adding the border the new size will increase by twice that of the border. We will be adding border to left and right side so width will increase by twice the border and same way height will go up by twice the border from top and bottom. So let us calculate the new width and height and create the image.