9951 explained code solutions for 126 technologies


php-gdHow to draw filled rectangle


<?php

$im = imagecreatetruecolor(400, 300);

$c_black = imageColorAllocate($im, 0,0,0);
$c_green = imageColorAllocate($im, 46,204,64);

imagefilledrectangle($im, 50, 100, 150, 200, $c_green);

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

imagefilledrectangle

creates filled rectangle

50, 100

top left rectangle coordinate

150, 200

bottom right rectangle coordinate

imagePng

saves image in PNG format to the given path


How to draw filled rectangle, php gd

Usage example

<?php

$im = imagecreatetruecolor(400, 300);

$c_black = imageColorAllocate($im, 0,0,0);
$c_green = imageColorAllocate($im, 46,204,64);

imagefilledrectangle($im, 50, 100, 150, 200, $c_green);

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