Making Border Image with PHP and GD Library
Advertisement
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 pointy1
y-coordinate for first pointx2
x-coordinate for second pointy2
y-coordinate for second pointcolor
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.
Related posts:

no need to looping the code just use imagesetthickness on the code
Nice tutorial.
You could have simply used imagesetthickness() with imagerectangle()