Drawing border around an image by using PHP and GD

We 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.
//$add2="images/1.jpg"; // Remove comment if a new image is to be created
$border=30; // Change the value to adjust width
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.
Now let us fix the border color and fill the new image with the rectangle
$border_color = imagecolorallocate($newimage, 255, 255, 255);
Finally let us create the new image by resizing the image

ImageJpeg($newimage,$add,100); // change here to $add2 if a new image is to be created
chmod("$add",0666); // change here to $add2 if a new image is to be created
Now our new image with border is created. We can create a new image by assigning a new path and image location $add2. This script can be modified a little and can be kept inside a loop to change all the picture of a directory by adding border to all. We need to read all the files of the directory and then apply our script to add border.

Using similar concepts we can add shadow to the image and give a 3D look. That is for our next tutorial.
GD functions
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net


    GD functions in PHP for handling images & graphics

    How to check GD support in PHP
    Header for an image in browser
    Drawing of graphic lines using GD
    Plotting line graph by drawing data from table
    Adding vertical grid to line graph
    Adding rectangles using imagerectangle()
    Imagearc drawing arc and circle
    Imagefilledarc drawing filled arc and circle
    Resizing and adding border to Images
    Script for Watermarking image on the fly using PHP GD
    Generate image by using user input form data

    Post your comments , suggestion , error , requirements etc here

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer