Jump to content

Making A Dynamic Image


kobel4k3r5

Recommended Posts

Save this as "bar.php"
[code]
<?php
// set dimensions
    $w = 62;
    $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, 0xFF, 0x00);
// draw border
    imagerectangle($im, 0,0,$w-1,$h-1,$black);
// 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);
// send image header
    header("content-type: image/png");
// send png image
    imagepng($im);
    imagedestroy($im);
?>[/code]

Then save and run this
[code]
<html>
<head>
<meta name="generator" content="PhpED Version 4.5 (Build 4513)">
<title>Bar sample</title>
<meta name="author" content="Barand">

</head>
<body>
<table>
  <tr>
      <td>
        Value
      </td>
      <td>
        Percent
      </td>
  </tr>
  <tr>
      <td>
        30
      </td>
      <td>
        <img src='bar.php?val=30&max=100'>
      </td>
  </tr>
  <tr>
      <td>
        60
      </td>
      <td>
        <img src='bar.php?val=60&max=100'>
      </td>
  </tr>
  <tr>
      <td>
        10
      </td>
      <td>
        <img src='bar.php?val=10&max=100'>
      </td>
  </tr>
</table>

</body>
</html>
[/code]
Link to comment
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.