<?php
$value_max = 100; # max abs input value, probably 100
#### get the value passed to the script in the query string
if( isset( $_GET[ 'value' ] ) ){
$value= $_GET[ 'value' ] + 0;
if( $value > $value_max ){
$value = $value_max ; # set to max if too big
} else if( $value < -$value_max ){
$value = -$value_max ;
}
} else {
$value = 0 ; # set to zero if they did not send it
}
$image_width = 100; # width of image in pixels
$image_height = 30 ; #height
$image_margin = 5; # size of left/right margins if you do not want to edge
$image_top_margin = 5; # top/bottom margins
DEFINE( 'POSITIVE_COLOR' , 'blue' ) ;
DEFINE( 'NEGATIVE_COLOR' , 'red' ) ;
# maximum width of color bar
$bar_max = floor( ($image_width - 2*$image_margin) / 2 ) ;
# midpoint
$image_midpoint = floor( $image_width/2 );
# actual width of color bar
$bar_length = (abs($value) /$value_max ) * $bar_max ;
# top edge
$top_start = $image_top_margin ;
# bottom edge
$bottom_end = $image_height - $image_top_margin ;
# find left and right enpoints of bar, and the color
if( $value > 0 ){
$left = $image_midpoint ;
$right = $left + $bar_length ;
$color = POSITIVE_COLOR ;
} else {
$right = $image_midpoint ;
$left = $image_midpoint - $bar_length ;
$color = NEGATIVE_COLOR ;
}
$im = imagecreate($image_width, $image_height);
# light gray background to show size for now, set to white or whatever
$bgcolor = imagecolorallocate($im, 240, 240, 240);
# vertical line color -- black in this case
$black = imagecolorallocate($im, 0, 0, 0);
# bar color
if($color == POSITIVE_COLOR ){
$bar_color = imagecolorallocate($im, 0, 0, 255);
} else {
$bar_color = imagecolorallocate($im, 255, 0, 0);
}
#draw the bar
imagefilledrectangle( $im , $left, $top_start , $right , $bottom_end, $bar_color ) ;
#draw the dividing line
imageline( $im , $image_midpoint , 0 , $image_midpoint , $image_height , $black ) ;
# send the header
header("Content-Type: image/png");
# send the image
imagepng($im);
# clean up after yourself
imagedestroy($im);
?>