tomclegg.net |
Create a PNG image with PHP Posted May 4, 2003 # Compile PHP with bundled GD library:
cd /tmp
tar xyf /usr/local/src/php-4.3.1.tar.bz2
cd php-4.3.1
'./configure' '--with-gd' '--with-mysql=/usr/local' \
'--with-mhash=/usr/local' '--with-mcrypt=/usr/local' \
'--enable-sockets' '--enable-trans-sid' '--prefix=/usr/local' \
'--with-readline=/usr' '--with-regex=system' '--with-zlib' \
'--enable-force-cgi-redirect' '--enable-track-vars' \
'--with-gettext'
make
cp sapi/cgi/php ~/public_html/php.cgi
# Sample PHP code:
<?
$w = 600;
$h = 400;
$i = imagecreate($w,$h);
$bg = imagecolorallocate ($i, 0x2B, 0x43, 0x71);
$fg = imagecolorallocate ($i, 0xFF, 0xFF, 0xFF);
$sc = imagecolorallocate ($i, 0xFF, 0xFF, 0x00);
imagestring ($i, 1, 30, $h-30, "Link integrity: number of ping packets received
in 15 minute intervals in last 5 days", $sc);
//imageline($i, 10, 10, 10, $h-10, $sc);
imageline($i, 10, 10, $w-10, 10, $sc);
imageline($i, 10, $h-10, $w-10, $h-10, $sc);
$x = 10;
$y = $h - 10;
$fd = popen ("zcat /home/tom/ping-bug.gz " .
"| perl -ne 'print \"$1\n\" if /seq=(\d+)/'", "r");
$base = 0;
$largest = 0;
$interval = 900;
$nextinterval = $interval;
$x = 11;
while (($n = fgets ($fd)) != "") {
// Each line is a sequence number >= 0 of a received packet.
// Sequence numbers wrap around to 0 after 65535.
$n += $base;
if ($n < $largest - 1000) { $base += 65536; $n += 65536; }
if ($n <= $largest) continue; // duplicate packet, or >1 sec rtt
if ($n > $largest) $largest = $n;
if ($n > $nextinterval) {
$nextinterval += $interval;
$newx = $x + 1;
$newy = $h - 10 - (($h - 20) * $pings_this_interval / $interval);
imageline($i, $x, $y, $newx, $newy, $fg);
$x = $newx;
$y = $newy;
$pings_this_interval = 0;
}
$pings_this_interval++;
}
header("Content-Type: image/png");
imagepng($i);
?> |