SQL PHP HTML ASP JavaScript articles and free scripts to download
PHP GD

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.

Be the first to post comment on this article :


Google+
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer