Click to See Complete Forum and Search --> : Not displaying jpeg


daschristie
02-05-2001, 04:07 AM
I have a PHP script that generates a bar chart, I call it with the following HTML
<IMG SRC="<?PHP print $barurl ?>" WIDTH="400" HEIGHT="200">

$barurl looks like the following:
barchart.php?graph_url=http%3A%2F%2Fwap.genie.co.uk%2F&realstartdate=976698000&realenddate=976704056&vmargin=20&hmargin=38&height=200&width=400

However the image doesn't display, yet if I change the imagejpeg($jpeg_im); to output the image to a file, the jpeg created looks fine.

I am using the following header at the start of the script: header("Content-type: image/jpeg");


and
imagejpeg($jpeg_im);

imagedestroy($jpeg_im);

at the end.

I am using Personal Web Server, IE 5.5, PHP 4.0.4pl1, GD 1.6.2 or higher (phpinfo) with jpeg,gif,png enabled.

I also get the same results with gifs.

Any suggestions?

vincente
02-05-2001, 07:13 AM
HTML doesn't work that way. You can't print the image data itself inside the page.


Read this article:

http://www.phpbuilder.com/columns/florian19991014.php3

daschristie
02-05-2001, 07:34 AM
This is my code that I am currently using:

<?PHP
header("HTTP/1.1 200 OK");
header("Content-type: image/jpeg");

// generates bar graph

include ("odbclib.inc");
include ("genlib.inc");
include ("dateslib.inc");
include ("graphslib.inc");

// globals taken from URL
global $graph_url;
global $realstartdate;
global $realenddate;
global $vmargin;
global $hmargin;
global $height;
global $width;


// other globals used
global $results;
global $results1;
$startdate = "";
$enddate = "";

// values used for drawing graph

convert_date_to_string($realstartdate,&$startdate);
convert_date_to_string($realenddate,&$enddate);


open_db();

query_db("SELECT duration+dial_duration+authen_duration,start_time FROM load_test_run_results WHERE error_code = '200' AND url = '$graph_url' AND (start_time >= #$startdate# AND start_time <= #$enddate#) ;");

get_query_results2();

close_db();

// get these to use in graph
$nval = sizeof($results);
$maxval = max($results);

// echo $maxval . "<BR>\n";

round_up($maxval,&$maxval);

// echo $maxval . "<BR>\n";

// create image image handle
$jpeg_im = ImageCreate($width,$height);

// colors
$white = imagecolorallocate($jpeg_im,0xFF,0xFF,0xFF);
$black = imagecolorallocate($jpeg_im,0x00,0x00,0x00);
$navy = imagecolorallocate($jpeg_im,0x00,0x00,0x80);
$gray = imagecolorallocate($jpeg_im,0xC0,0xC0,0xC0);

// draw border round graph
$base = floor(($width - $hmargin)/$nval); // distance between columns

$ysize = $height - 2 * $vmargin; // y-axis plot
$xsize = $nval * $base; // x-axis plot

ImageFilledRectangle($jpeg_im,0,0,$height,$width,$white);
imagerectangle($jpeg_im, $hmargin, $vmargin, $hmargin+$xsize, $vmargin+$ysize, $black);

// draw title
$titlefont = 3;
$title = $graph_url;

$txtsz = imagefontwidth($titlefont) * strlen($title);

$xpos = (int)($hmargin + ($xsize - $txtsz)/2);
$xpos = max(1,$xpos);
$ypos = 3;

imagestring($jpeg_im,$titlefont,$xpos,$ypos,$title,$black);

// y labels and grid lines
$labelfont = 2;
$ngrid = 8; // number of grid lines

$dydat = $maxval/$ngrid; // data units between grid lines
$dypix = $ysize/($ngrid + 1); // pixels between grid lines

for($i = 0; $i <= ($ngrid + 1);$i++) {
// iterate over y ticks
$ydat = (int)($i * $dydat); // height of grid line in units of data
$ypos = $vmargin + $ysize - (int)($i*$dypix); // height of grid line in pixels

$txtsz = imagefontwidth($labelfont) * strlen($ydat); // pixel width of label
$txtht = imagefontheight($labelfont); // pixel-height of label

$xpos = (int)(($hmargin - $txtsz)/2);
$xpos = max(1,$xpos);

imagestring($jpeg_im,$labelfont, $xpos, $ypos - (int)($txtht/2),$ydat,$black);

if (!($i == 0) && !($i > $ngrid))
imageline($jpeg_im,$hmargin - 3, $ypos, $hmargin + $xsize, $ypos, $gray);

}

// columns and x labels
$padding = 3; // half of spacing between columns
$yscale = $ysize / (($ngrid+1)*$dydat); // pixels per data unit

$xpix = ($realenddate - $realstartdate)/($width-$hmargin);

for ($i = 0; list($xval,$yval) = each($results); $i++) {
// vertical columns

$expl = explode(" ",$results1[$i]);

$temp = explode("-",$expl[0]);
$temp1 = explode(":",$expl[1]);
$currentdate = mktime($temp1[0],$temp1[1],$temp1[2],$temp[1],$temp[2],$temp[0]);

$xcurpos = ($currentdate - $realstartdate)/$xpix;


$ymax = $vmargin + $ysize;
$ymin = $ymax - (int)($yval*$yscale);

$xmax = $hmargin + $xcurpos+10;
$xmin = $hmargin + $xcurpos;

imagefilledrectangle($jpeg_im,$xmin,$ymin,$xmax,$ymax,$navy);
}

// x-axis labels
$nxlabels = 3;
$xdatinc = ($realenddate - $realstartdate)/$nxlabels;
$currentdate = $realstartdate;
$xdat = "";
for ($i=0; $i <= ($nxlabels + 1); $i++) {

$currentdate = $currentdate + $xdatinc;
$xcurpos = ($currentdate - $realstartdate)/$xpix;

convert_date_to_string1($currentdate,&$xdat);
$temp = explode(" ",$xdat); // split date/time up ready for printing on x-axis

// time
$txtsz = imagefontwidth($labelfont) * strlen($temp[0]);
$txtht = imagefontheight($labelfont);

imagestring($jpeg_im,$labelfont,($xcurpos - ($txtsz/2)),($ymax+6),$temp[0],$black);

// date
$txtsz = imagefontwidth($labelfont) * strlen($temp[1]);
$txtht = imagefontheight($labelfont);

imagestring($jpeg_im,$labelfont,($xcurpos - ($txtsz/2)),($ymax+6+$txtht),$temp[1],$black);

imageline($jpeg_im,$xcurpos,$ymax,$xcurpos,$ymax+3,$black);
}

// flush image

imagejpeg($jpeg_im,"devim.jpg");

imagedestroy($jpeg_im);
?>

daschristie
02-05-2001, 07:36 AM
As can been see I am not trying to print the data, but print the url with parameters to use to call the PHP script I have attached.

daschristie
02-05-2001, 08:32 AM
oops

imagejpeg($jpeg_im,"devim.jpg");
should be imagejpeg($jpeg_im);

took code from my dev version while trying to sort this out. Allows me to check graph is correct.