Possibly something like this (completely untested!)...
# ----------- Draw the bars here ------
$i = 0;
foreach($values as $key => $value) {
if (preg_match('|\d{2}|', $value)) { // adjust value spacing on top of bar
$bv_gap = 7;
}
if (preg_match('|\d{3}|', $value)) {
$bv_gap = 4;
}
$x1 = $margins + $gap + $i * ($gap + $bar_width);
$x2 = $x1 + $bar_width;
$y1 = $margins + $graph_height - intval($value * $ratio);
$y2 = $img_height - $margins;
imagestring($img, 0, $x1 + $bv_gap, $y1 - 10, $value, $bar_color); // move values on top of bars
imagestring($img, 0, $x1 + 2, $img_height - 15, $key, $bar_color); // move year numbers on bottom
imagefilledrectangle($img, $x1, $y1, $x2, $y2, $bar_color);
$i++;
}