Jump to content

PHP graphic display


tmyonline

Recommended Posts

You need php_gd2.dll in your extension folder, and make sure this line in php.ini is uncommented (ie no ; at start)

extension=php_gd2.dll

 

(if linux, it's a .so file instead of .dll)

 

Here's a simple way to do a bar chart

 

:: bar.php ::

<?php
// set dimensions
     $w = 102;
     $h = 20;
// create image
     $im = imagecreate($w, $h);
// set colours to be used
     $bg = imagecolorallocate($im, 0xE0, 0xE0, 0xE0);
     $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
     $red  = imagecolorallocate($im, 0xFF, 0x00, 0x00);
     $green  = imagecolorallocate($im, 0x50, 0xB6, 0x30);  
// 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)
     {
        $barcolor = $val < 50 ? $red : $green;
     	imagefilledrectangle($im, 1, 1, $barw, $barh, $barcolor);
     }
// send image header
     header("content-type: image/png");
// send png image
     imagepng($im);
     imagedestroy($im);
?>

 

To use it

<!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='0'>
   <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>
         60%
      </td>
      <td>
         <img src='bar.php?val=60&max=100'>
      </td>
   </tr>
</table>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/96976-php-graphic-display/#findComment-496277
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.