How do I give a border to an image created using PHP?
+4
A:
function drawBorder(&$img, &$color, $thickness = 1)
{
$x1 = 0;
$y1 = 0;
$x2 = ImageSX($img) - 1;
$y2 = ImageSY($img) - 1;
for($i = 0; $i < $thickness; $i++)
{
ImageRectangle($img, $x1++, $y1++, $x2--, $y2--, $color);
}
}
Then the usage would just to do.
$color = imagecolorallocate($img, 255, 0, 0);
drawBorder($img,$color, 255);
RobertPitt
2010-06-09 13:07:52
Error. You asked for $color but used $color_black.
Neb
2010-06-09 13:09:33
Ive updated my post, pelase review, miner typing error!
RobertPitt
2010-06-09 13:15:21
this is the right method to display images using PHPheader("content-type: image/jpeg"); imagejpeg($image);
learner
2010-06-10 06:09:11
hi it is not working :(
learner
2010-06-10 11:36:37
we would need to see your whole code to see if your sending the right haders etc.
RobertPitt
2010-06-10 12:53:27
+1
A:
With ImageMagick:
bool Imagick::borderImage ( mixed $bordercolor , int $width , int $height )
Surrounds the image with a border of the color defined by the bordercolor ImagickPixel object.
Felix Kling
2010-06-09 13:08:44
A:
There is no built in function for this (except imagick) , but try to be creative :) An idea:
Create a "layer" under the image witch is "n px" bigger than the original picture. Give it another color and you have your border
Nort
2010-06-09 13:09:03
A:
I didn't test this but I think it will do the trick.
function addBorder($image, $width, $height)
{
$gd = imagecreatetruecolor($width, $height);
for($i = 0; $i<$height; $i++)
{
// add left border
imagesetpixel($image,0,$i, imagecolorallocate($gd, 0,0,0) );
// add right border
imagesetpixel($image,$width-1,$i, imagecolorallocate($gd, 0,0,0) );
}
for($j = 0; $j<$width; $j++)
{
// add bottom border
imagesetpixel($image,$j,0, imagecolorallocate($gd, 0,0,0) );
// add top border
imagesetpixel($image,$j,$height-1, imagecolorallocate($gd, 0,0,0) );
}
return $image;
}
$image = //your image
$width = //your iimage width
$height = //your image height
$image = addBorder($image, $width, $height);
John Isaacks
2010-06-09 13:24:50