Jump to content


Photo

Making A Dynamic Image


  • Please log in to reply
1 reply to this topic

#1 kobel4k3r5

kobel4k3r5
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 23 September 2006 - 04:36 PM

Anyone have some basic guidelines on how to make a dynamic image? Say for example: A sig banner that captures content from websites or an image that captures variables that are sent to it (image.php?var=value for example).

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 23 September 2006 - 05:20 PM

Save this as "bar.php"
<?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);
?>

Then save and run this
<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>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users