9951 explained code solutions for 126 technologies


php-gdHow to add border to image


<?php

$file = '/var/www/examples/heroine.png';
$size = getimagesize($file);
$im = imagecreatefrompng($file);

imagesetthickness($im, 15);
$c_green = imageColorAllocate($im, 46,204,64);
imagerectangle($im, 0, 0, $size[0]-1, $size[1]-1, $c_green);

imagePng($im, '/tmp/image.png');ctrl + c
$file

source image file add border to

getimagesize

returns image size from given path

imagecreatefrompng

creates GD image object from given PNG image

imagesetthickness

set line thickness (border width, set to 15 pixels in our case)

imageColorAllocate

creates color object to later use in image

imagerectangle

creates rectangle

/tmp/image.png

path to write generated image to


How to add border to image, php gd

Usage example

<?php

$file = '/var/www/examples/heroine.png';
$size = getimagesize($file);
$im = imagecreatefrompng($file);

imagesetthickness($im, 15);
$c_green = imageColorAllocate($im, 46,204,64);
imagerectangle($im, 0, 0, $size[0]-1, $size[1]-1, $c_green);

imagePng($im, '/tmp/image.png');