9951 explained code solutions for 126 technologies


php-gdHow to draw rectangle


<?php

$im = imagecreatetruecolor(400, 300);

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

imagerectangle($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

imagerectangle

creates rectangle

50, 100

top left rectangle coordinates

150, 200

bottom right rectangle coordinates

$c_green

rectangle border color

imagePng

saves image in PNG format to the given path


How to draw rectangle, php gd

Usage example

<?php

$im = imagecreatetruecolor(400, 300);

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

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

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