thepip3r Posted December 28, 2006 Share Posted December 28, 2006 On the page where I want to see the bars, I'm using: [code]1 <img src='bar.php?b=hp&val=50&max=100'> <br />2 <img src='bar.php?b=man&val=70&max=100'> <br />[/code]and then the bar generating code: [code]if ($_GET['b'] == "man") { header("content-type: image/png"); // set dimensions $w = 100; $h = 12; // create image $im = imagecreate($w, $h); // set colours to be used $bg = imagecolorallocate($im, 255, 255, 255); $border = imagecolorallocate($im, 0, 0, 0); $barcolor = imagecolorallocate($im, 0, 0, 255); $textcolor = imagecolorallocate($im, 0, 0, 0); // draw border imagerectangle($im, 0,0,$w-1,$h-1,$border); // get value and max value from query string $val = $_GET['val']; $max = $_GET['max']; // calculate dimensions of inner bar $barw = $max ? floor(($w-2) * $val / $max) : 0; $barh = $h - 2; // draw inner bar if ($barw) imagefilledrectangle($im, 1, 1, $barw, $barh, $barcolor); $string = $val."/".$max; $px = (imagesx($im) - 7.5 * strlen($string)) / 2; imagestring($im, 3, $px, 0, $string, $textcolor); // send image header // output image imagepng($im); imagedestroy($im);} [/code]And this works. So I wanted to try and dump all of that good stuff into a function but keep getting an "X" in place of my image when I've done so. I don't think the errors are syntax'd based because if I don't call the function, the page doesn't pop an error. Here is my attempt at creating the function: [code]function png_image_bar_text($barWidth,$barHeight,$bgColor,$borderColor,$barColor,$textColor,$textString,$barVal,$barMax) { // send image header header("content-type: image/png"); // check all vars have vals if (!$barHeight || !$barWidth || !$bgColor || !$borderColor || !$barColor || !$textColor || !$textString || !$barVal || !$barMax) return -1; // check image dimensions if (!is_num($barHeight) || !is_num($barWidth) || !is_num($barVal) || !is_num($barMax)) return -1; // check color specs to ensure they follow the "255,255,255" format if (count(split(",",$bgColor)) != 3 || count(split(",",$borderColor)) != 3 || count(split(",",$barColor)) != 3 || count(split(",",$textColor)) != 3) return -1; // create the image $im = imagecreate($barWidth,$barHeight); // create the arrays of colors to be used $bgColor = split(",",$bgColor); $borderColor = split(",",$borderColor); $barColor = split(",",$barColor); $textColor = split(",",$textColor); // check submitted values to ensure they're in the proper ranges (0-255) foreach ($bgColor as $val) { if ($val < 0 || $val > 255 || !is_num($val)) return -1; } foreach ($borderColor as $val) { if ($val < 0 || $val > 255 || !is_num($val)) return -1; } foreach ($barColor as $val) { if ($val < 0 || $val > 255 || !is_num($val)) return -1; } foreach ($textColor as $val) { if ($val < 0 || $val > 255 || !is_num($val)) return -1; } // allocate specified colors to our gen'd image $bgCreate = imagecolorallocate($im, $bgColor[0], $bgColor[1], $bgColor[2]); $borderCreate = imagecolorallocate($im, $borderColor[0], $borderColor[1], $borderColor[2]); $barCreate = imagecolorallocate($im, $barColor[0], $barColor[1], $barColor[2]); $textCreate = imagecolorallocate($im, $textColor[0], $textColor[1], $textColor[2]); // draw border imagerectangle($im, 0,0,$barWidth-1,$barHeight-1,$borderCreate); // calc dimensions for inner bar $barw = $barMax ? floor(($$barWidth-2) * $barVal / $barMax) : 0; $barh = $barHeight - 2; // draw inner bar imagefilledrectangle($im, 1, 1, $barw, $barh, $barCreate); // find middle and draw text $px = (imagesx($im) - 7.5 * strlen($textString)) / 2; imagestring($im, 3, $px, 0, $textString, $textCreate); // output image imagepng($im); imagedestroy($im);}[/code]and here is my attempt at calling the function: [code]if ($_GET['b'] == "hp") { $val = $_GET['val']; $max = $_GET['max']; png_image_bar_text("100","12","255,255,255","0,0,0","0,0,255","0,0,0","test",$val,$max);}[/code]If anyone can help me troubleshoot this, I don't even have any idea as to how I can trap errors using these functions so if you can point me in the right direction or take a quick look at my function to see if there's anything glaringly obvious, i'd greatly appreciate it. thanx in advance. Link to comment https://forums.phpfreaks.com/topic/32125-trying-to-create-a-text-overlay-image-function-not-working/ Share on other sites More sharing options...
trq Posted December 29, 2006 Share Posted December 29, 2006 Theres all sorts of weird stuff going on in that function. Firstly, theres no need to check if all the arguments have been passed to it, if one is missing php will throw a Warning.Secondly, the two arguments $barWidth and $barHeight expect integers, you are passing them strings.The third thing I would do would be to make $bgColor, $borderColor, $barColor and $textColor actually accepts an array instead of splitting them into an array within the function.If you fix all this then the call to use your function should look more like....[code=php:0]png_image_bar_text(100,12,array(255,255,255),array(0,0,0),array(0,0,255),array(0,0,0),"test",$val,$max);[/code] Link to comment https://forums.phpfreaks.com/topic/32125-trying-to-create-a-text-overlay-image-function-not-working/#findComment-149072 Share on other sites More sharing options...
thepip3r Posted January 3, 2007 Author Share Posted January 3, 2007 Thanx for the suggestions thorpe. I changed the code to all of the specifiications you listed and still get the red X for the image. Any other suggestions??[code]function png_image_bar_text($barWidth,$barHeight,$bgColor,$borderColor,$barColor,$textColor,$textString,$barVal,$barMax) { // send image header header("content-type: image/png"); // check image dimensions if (!is_num($barHeight) || !is_num($barWidth) || !is_num($barVal) || !is_num($barMax)) return -1; // check color specs to ensure they follow the "255,255,255" format if (!is_array($bgColor) || !is_array($borderColor) || !is_array($barColor) || !is_array($textColor)) return -1; // check color specs to ensure they follow the "255,255,255" format if (count($bgColor) != 3 || count($borderColor) != 3 || count($barColor) != 3 || count($textColor) != 3) return -1; // create the image $im = imagecreate($barWidth,$barHeight); // check submitted values to ensure they're in the proper ranges (0-255) foreach ($bgColor as $val) { if ($val < 0 || $val > 255 || !is_num($val)) return -1; } foreach ($borderColor as $val) { if ($val < 0 || $val > 255 || !is_num($val)) return -1; } foreach ($barColor as $val) { if ($val < 0 || $val > 255 || !is_num($val)) return -1; } foreach ($textColor as $val) { if ($val < 0 || $val > 255 || !is_num($val)) return -1; } // allocate specified colors to our gen'd image $bgCreate = imagecolorallocate($im, $bgColor[0], $bgColor[1], $bgColor[2]); $borderCreate = imagecolorallocate($im, $borderColor[0], $borderColor[1], $borderColor[2]); $barCreate = imagecolorallocate($im, $barColor[0], $barColor[1], $barColor[2]); $textCreate = imagecolorallocate($im, $textColor[0], $textColor[1], $textColor[2]); // draw border imagerectangle($im, 0,0,$barWidth-1,$barHeight-1,$borderCreate); // calc dimensions for inner bar $barw = $barMax ? floor(($$barWidth-2) * $barVal / $barMax) : 0; $barh = $barHeight - 2; // draw inner bar imagefilledrectangle($im, 1, 1, $barw, $barh, $barCreate); // find middle and draw text $px = (imagesx($im) - 7.5 * strlen($textString)) / 2; imagestring($im, 3, $px, 0, $textString, $textCreate); // output image imagepng($im); imagedestroy($im);}[/code]and the call [code]if ($_GET['b'] == "hp") { $val = $_GET['val']; $max = $_GET['max']; png_image_bar_text(100,12,array(255,255,255),array(0,0,0),array(0,0,255),array(0,0,0),"test",$val,$max);}[/code] Link to comment https://forums.phpfreaks.com/topic/32125-trying-to-create-a-text-overlay-image-function-not-working/#findComment-152422 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.