9951 explained code solutions for 126 technologies


php-gdHow to draw a line


<?php

$im = imagecreatetruecolor(400, 300);

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

imageline($im, 50, 50, 350, 250, $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

imageline

draws line using given coordinates and color

$c_green

color to use as line color

50, 50

starting coordinates for our line

350, 250

end of line coordinates

imagePng

saves image in PNG format to the given path


How to draw a line, php gd

Usage example

<?php

$im = imagecreatetruecolor(400, 300);

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

imageline($im, 50, 50, 350, 250, $c_green);

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