Nick
2006.10.04, 11:09 PM
I'm writing up a simple PHP/GD script that will draw me a ratings bar of sorts, leaning one way or the other as it will. It works perfectly on my local Mac server (using the Sites folder and all that). However, on my online server, it draws differently. It will only draw the bar if it goes positive, and it won't draw the bottom border. Here's the two pictures:
My local output:
http://nick.grave-media.com/dump/localhostbar.jpg
My online output:
http://nick.grave-media.com/dump/onlinebar.jpg
I used gdinfo() and dumped out the info to see if my online host uses a different version of GD but they are the same (2.0.28 I believe). I can't figure this out for the life of me. Any ideas? Here's the script for those who want to see it:
<?php
if(isset($_GET['r']) && isset($_GET['v'])) {
//neutral is 50%, then we find the difference for our rating
$r = ($_GET['r'] / $_GET['v']) - .5;
//figure out the thumbs up and thumbs down
$up = $_GET['r'];
$down = $_GET['v'] - $_GET['r'];
$useText = isset($_GET['t']);
$w = 250;
$h = 20;
if(isset($_GET['w'])) { $w = $_GET['w']; }
if(isset($_GET['h'])) { $h = $_GET['h']; }
header("Content-type: image/png");
$im = imagecreate($w, $h);
//allocate some colors
$background = imagecolorallocate($im, 150, 150, 150);
$border = imagecolorallocate($im, 0, 0, 0);
$text = imagecolorallocate($im, 255, 255, 255);
if($r > 0) {
//positive color
$bar = imagecolorallocate($im, 0, 0, 255);
}
else {
//negative color
$bar = imagecolorallocate($im, 255, 0, 0);
}
//draw our bar first so the rest just overlays it
//that way we don't have to fill in spaces and worry
//about overlapping the border
imagefilledrectangle($im, ($w / 2), 0, ($w / 2) + ($r * $w), $h - 1, $bar);
//write our vote counts on either side
$font = 4;
$top = ($h / 2) - (imagefontheight($font) / 2);
$left = ($w / 4) - (imagefontwidth($font) * strlen($down) / 2);
imagestring($im, $font, $left, $top, $down, $text);
$left = (3 * $w / 4) - (imagefontwidth($font) * strlen($up) / 2);
imagestring($im, $font, $left, $top, $up, $text);
//top border
imagefilledrectangle($im, 0, 0, $w - 1, 1, $border);
//bottom border
//imagefilledrectangle($im, 0, $h - 1, $w - 1, $h - 2, $border);
imagefilledrectangle($im, 0, $h - 1, $w - 1, $h - 2, $border);
//left border
imagefilledrectangle($im, 0, 0, 1, $h - 1, $border);
//right border
imagefilledrectangle($im, $w - 2, 0, $w - 1, $h - 1, $border);
//center line
imagefilledrectangle($im, ($w / 2) - 1, 0, ($w / 2) + 1, $h - 1, $border);
imagepng($im);
imagedestroy($im);
}
?>
My local output:
http://nick.grave-media.com/dump/localhostbar.jpg
My online output:
http://nick.grave-media.com/dump/onlinebar.jpg
I used gdinfo() and dumped out the info to see if my online host uses a different version of GD but they are the same (2.0.28 I believe). I can't figure this out for the life of me. Any ideas? Here's the script for those who want to see it:
<?php
if(isset($_GET['r']) && isset($_GET['v'])) {
//neutral is 50%, then we find the difference for our rating
$r = ($_GET['r'] / $_GET['v']) - .5;
//figure out the thumbs up and thumbs down
$up = $_GET['r'];
$down = $_GET['v'] - $_GET['r'];
$useText = isset($_GET['t']);
$w = 250;
$h = 20;
if(isset($_GET['w'])) { $w = $_GET['w']; }
if(isset($_GET['h'])) { $h = $_GET['h']; }
header("Content-type: image/png");
$im = imagecreate($w, $h);
//allocate some colors
$background = imagecolorallocate($im, 150, 150, 150);
$border = imagecolorallocate($im, 0, 0, 0);
$text = imagecolorallocate($im, 255, 255, 255);
if($r > 0) {
//positive color
$bar = imagecolorallocate($im, 0, 0, 255);
}
else {
//negative color
$bar = imagecolorallocate($im, 255, 0, 0);
}
//draw our bar first so the rest just overlays it
//that way we don't have to fill in spaces and worry
//about overlapping the border
imagefilledrectangle($im, ($w / 2), 0, ($w / 2) + ($r * $w), $h - 1, $bar);
//write our vote counts on either side
$font = 4;
$top = ($h / 2) - (imagefontheight($font) / 2);
$left = ($w / 4) - (imagefontwidth($font) * strlen($down) / 2);
imagestring($im, $font, $left, $top, $down, $text);
$left = (3 * $w / 4) - (imagefontwidth($font) * strlen($up) / 2);
imagestring($im, $font, $left, $top, $up, $text);
//top border
imagefilledrectangle($im, 0, 0, $w - 1, 1, $border);
//bottom border
//imagefilledrectangle($im, 0, $h - 1, $w - 1, $h - 2, $border);
imagefilledrectangle($im, 0, $h - 1, $w - 1, $h - 2, $border);
//left border
imagefilledrectangle($im, 0, 0, 1, $h - 1, $border);
//right border
imagefilledrectangle($im, $w - 2, 0, $w - 1, $h - 1, $border);
//center line
imagefilledrectangle($im, ($w / 2) - 1, 0, ($w / 2) + 1, $h - 1, $border);
imagepng($im);
imagedestroy($im);
}
?>