SQL PHP HTML ASP JavaScript articles and free scripts to download If you are facing any problem in viewing this page, please tell us
 
 

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.


$add="images/a.jpg";
//$add2="images/1.jpg"; // Remove comment if a new image is to be created
$border=30; // Change the value to adjust width
$im=ImageCreateFromJpeg($add);
$width=ImageSx($im);
$height=ImageSy($im);


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.

$img_adj_width=$width+(2*$border);
$img_adj_height=$height+(2*$border);
$newimage=imagecreatetruecolor($img_adj_width,$img_adj_height);


Now let us fix the border color and fill the new image with the rectangle

$border_color = imagecolorallocate($newimage, 255, 255, 255);
imagefilledrectangle($newimage,0,0,$img_adj_width,$img_adj_height,$border_color);


Finally let us create the new image by resizing the image

imageCopyResized($newimage,$im,$border,$border,0,0,$width,$height,$width,$height);
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.


Further readings
GD functions in PHP for handling images & graphs
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
Resizing and adding border to Images
Script for Watermarking image on the fly using PHP GD










Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
PHP Tutorials
Array Functions
PHP Monthly Planner
PHP Introduction
Loops & structure
Array
Date & Time
Functions
Form Handling
File Handling
Math Functions
String Functions
GD Functions
Comment Posting
Content Management
PHP & Ajax
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.