To create bar chart using PHP, you can use the built-in function Imagefilledrectangle() which draw a rectangle according the given x and y coordinates.

The Syntax

imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color ) : bool

The Parameters

$x1, $y1 set the top left coordinate of the rectangle
$x2, $y2 set the bottom right coordinate of the rectangle
$color defines the color you want to set for the fill.

<?php
$image_width = 400;
$image_height = 400;

$image = imageCreate($image_width, $image_height);

//Lets create some colors
$bg_color = ImageColorAllocate($image, 255, 255, 255);
$color1 = ImageColorAllocate($image, 154,185,153);
$color2 = ImageColorAllocate($image,252,206,170);
$color3 = ImageColorAllocate($image,244,131,125);
$color4 = ImageColorAllocate($image,235, 73,96);
$color5 = ImageColorAllocate($image,39,54,59);

//x1, y1, x2, y2, color
Imagefilledrectangle($image, 20,100,70,400, $color1); //20+60=80
Imagefilledrectangle($image, 80,150,130,400, $color2); //80+60=140
Imagefilledrectangle($image, 140,200,190,400, $color3);//140+60 = 200
Imagefilledrectangle($image, 200,250,250,400, $color4);//200+60 = 260
Imagefilledrectangle($image, 260,300,310,400, $color5);

ob_clean();
header ("Content-type: image/png");
ImagePng($image);
ImageDestroy($image);
?>

Recomanded reading

Last modified: March 17, 2019

Comments

Write a Reply or Comment

Your email address will not be published.