9951 explained code solutions for 126 technologies


php-gdHow to draw rectangle with border


Approach is to first draw filled rectangle and then draw rectangle border to finally get rectangle with border.

<?php

$im = imagecreatetruecolor(400, 300);

$c_black = imageColorAllocate($im, 0,0,0);
$c_fill = imageColorAllocate($im, 46,204,64);
$c_border = imageColorAllocate($im, 1,255,112);

imagefilledrectangle($im, 50, 100, 150, 200, $c_fill);
imagerectangle($im, 50, 100, 150, 200, $c_border);

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

creates true color GD image object with specified width & height

imageColorAllocate

creates color object to later use in image

imagerectangle

creates rectangle

imagefilledrectangle

creates filled rectangle

imagePng

saves image in PNG format to the given path


How to draw rectangle with border, php gd

Usage example

<?php

$im = imagecreatetruecolor(400, 300);

$c_black = imageColorAllocate($im, 0,0,0);
$c_fill = imageColorAllocate($im, 46,204,64);
$c_border = imageColorAllocate($im, 1,255,112);

imagefilledrectangle($im, 50, 100, 150, 200, $c_fill);
imagerectangle($im, 50, 100, 150, 200, $c_border);

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