Jump to content

Recommended Posts

its first time trying to use GD and i dont really know what im doing but im trying to make a percentage bar, than once ive got that sorted out i would like to put the values of the bar over the top this is the code i tried with but all it does is display the url

 

<?php
//get bar percentage
if( $_GET['real'] > 0){
	$perc = ( $_GET['real'] / $_GET['max']) * 100;
} else {
	$perc = 0;
}
    // create image
    $per = imagecreate(90,10);

//get needed colours
    $background = imagecolorallocate($per, 0xFF, 0xFF, 0xFF);
    $foreground = imagecolorallocate($per, 0x00, 0x8A, 0x01);
$textcol = imagecolorallocate($per, 000, 000, 000);
    $border = imagecolorallocate($per, 0x99, 0x99, 0x99);

//lenght of bar
$bar_lenght = $perc * 0.9;

//draw the bar
imagefilledrectangle($per, 0, 0, $bar_lenght, 2, $foreground);

    imagerectangle($per, $bar_lenght+1, -2, 90, 10, $border);

    header("Content-type: image/png");
    imagepng($per, '', 100); 
imagedestroy($per);
?> 

 

anyone have any ideas?

Link to comment
https://forums.phpfreaks.com/topic/54128-solved-gd-help/
Share on other sites

One I prepared earlier

 

This sample displays the bars

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<META http-equiv="content-language" content="en">
<META name="author" content="Barand">
<META name="generator" content="PHPEd 4.5">
<title>Bar sample</title>
</head>
<body>
<table width="300" border='1'>
   <tr>
      <td>
         Item A
      </td>
      <td>
         20%
      </td>
      <td>
         <img src='bar.php?val=20&max=100'>
      </td>
   </tr>
   <tr>
      <td>
         Item B
      </td>
      <td>
         40%
      </td>
      <td>
         <img src='bar.php?val=40&max=100'>
      </td>
   </tr>
   <tr>
      <td>
         Item C
      </td>
      <td>
         30%
      </td>
      <td>
         <img src='bar.php?val=30&max=100'>
      </td>
   </tr>
   <tr>
      <td>
         Item D
      </td>
      <td>
         10%
      </td>
      <td>
         <img src='bar.php?val=10&max=100'>
      </td>
   </tr>
</table>

</body>
</html>

 

and this is the gd code ::bar.php::

<?php
// set dimensions
     $w = 102;
     $h = 12;
// create image
     $im = imagecreate($w, $h);
// set colours to be used
     $bg = imagecolorallocate($im, 0xE0, 0xE0, 0xE0);
     $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
     $barcolor  = imagecolorallocate($im, 0xFF, 0x00, 0x00);
// draw border
     imagerectangle($im, 0,0,$w-1,$h-1,$black);
// get value and max value from query string
     $val = isset($_GET['val']) ? $_GET['val'] : 0;
     $max = isset($_GET['max']) ? $_GET['max'] : 100; 
// 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);
// send image header
     header("content-type: image/png");
// send png image
     imagepng($im);
     imagedestroy($im);
?>

Link to comment
https://forums.phpfreaks.com/topic/54128-solved-gd-help/#findComment-267823
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.