I have the following code which works perfectly fine in my php file and generates a histogram image. However as soon as I add an html tags it wouldn’t generate a histogram anymore but an empty image. It seems to me that the problem is in the first line header("Content-type: image/jpeg");. But how can i fix it?
<html>
<?php
// Send the PNG header information. Replace for JPEG or GIF or whatever
header("Content-type: image/jpeg");
// This array of values is just here for the example.
$data = array('3400','2570','245','473','1000','3456','780');
// Get the total value of columns we are going to plot
$sum = array_sum($data);
// Get the height and width of the final image
$height = 255;
$width = 320;
$im = imagecreate($width,$height); // width , height px
// Generate the image variables
$white = imagecolorallocate($im,255,255,255);
$black = imagecolorallocate($im,0,0,0);
$red = imagecolorallocate($im,255,0,0);
imageline($im, 10, 5, 10, 230, $black);
imageline($im, 10, 230, 300, 230, $black);
// Now plot each column
$x = 15;
$y = 230;
$x_width = 20;
$y_ht = 0;
for ($i=0;$i<7;$i++){
$y_ht = ($data[$i]/$sum)* $height;
// This part is just for 3D effect
imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red);
imagestring( $im,2,$x-1,$y+10,$data[$i],$black);
$x += ($x_width+20);
}
imagejpeg($im);
?>
</html>
Remove the tag in your PHP file as it’s not HTML document.
Make sure there’s no space before
<?phpopen tag.
Call that file within and img tag like
<img src="genrated.jpg.php" />