Home > PHP > Making Border Image with PHP and GD Library

Making Border Image with PHP and GD Library


Advertisement



October 27th, 2008 Admin Leave a comment Go to comments

As i know, PHP don’t have a function to create image border directly. So, how to make border image if PHP doesn’t have it? We must set manually using imageline function that already have in PHP since version 4 built in GD library. Before we go, we need to describe what is image border. That is a line in all side of image, top, right, bottom, and left.

Imageline function just allowed us to draw a single line between two given points. The parameters are :

image
An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().

x1
x-coordinate for first point

y1
y-coordinate for first point

x2
x-coordinate for second point

y2
y-coordinate for second point

color
The line color. A color identifier created with imagecolorallocate()

It will return true on success and false on failed. Because we need four single line in all side of image to create border, so we need to create imageline function four times to cover top, right, bottom, and left. This is the code (don’t forget to manually add <?php and ?> tag in begin and end code).

// create a 200*60 image
$im = imagecreate(200, 60);
 
// white background and blue text
$bg = imagecolorallocate($im, 222, 222, 222);
$textcolor = imagecolorallocate($im, 0, 0, 255);
 
// write the string
imagestring($im, 5, 30, 20, "SmileyLover.Com", $textcolor);
 
	//border
	$bordercolors = imagecolorallocate($im, 255, 255, 20); //Define border color  Yellow
 
	$x = 0;
	$y = 0;
	$w = imagesx($im) - 1; //get width image and decrease 1px or points ?
	$h = imagesy($im) - 1; //get height image and decrease 1px or points ?
	imageline($im, $x,$y,$x,$y+$h,$bordercolors); //left
	imageline($im, $x,$y,$x+$w,$y,$bordercolors); //top
	imageline($im, $x+$w,$y,$x+$w,$y+$h,$bordercolors); //right
	imageline($im, $x,$y+$h,$x+$w,$y+$h,$bordercolors); //bottom
	//end border
 
// output the image
header("Content-type: image/png");
imagepng($im);

This code only give you 1px line border with yellow color. If you want to make a 10px border maybe you can loops this function and decrease a padding -1px for each loops :) Good luck … and this is the screenshot result.


Making Border Image with PHP and GD Library

Related posts:

  1. Calculation Your PHP Processing Time Using Simple Counter
  2. Making WordPress Exchange Links Without Plugins
  1. January 31st, 2009 at 10:45 | #1

    no need to looping the code just use imagesetthickness on the code :)

  2. February 22nd, 2009 at 12:57 | #2

    Nice tutorial.

  3. November 21st, 2009 at 05:36 | #3

    You could have simply used imagesetthickness() with imagerectangle() :)

  1. No trackbacks yet.