GD PNG as a .png file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

GD PNG as a .png file?

Post by waradmin »

I have a php generated PNG file:
http://uberamd.org/testing/gd.php

I would like to use this in forum signatures, however when I name it, gd.php.png for example it doesn't display a image, or if I name it gd.png it also does not display anything. On a old server of mine, I remember i was able to use .php.png on generated images and it would display them as images. Is there a way I can take the php generated (dynamic) image but use it as a actual image, much like the forum signatures that display the user's IP and such do?

thanks!

Here is the code if needed:

Code: Select all

<?PHP
header("Content-type: image/png");
/////////////////////////
$nm = @exec('hostname');
function serverStats()
{
	$uptime = @exec('uptime'); 
	preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/",$uptime,$avgs); 
	$uptime = explode(' up ', $uptime); 
	$uptime = explode(',', $uptime[1]); 
	$up = $uptime[0]; 
	$users = $uptime[1];
	$start=mktime(0, 0, 0, 1, 1, date("Y"), 0); 
	$end=mktime(0, 0, 0, date("m"), date("j"), date("y"), 0); 
	$diff=$end-$start; 
	$days=$diff/86400; 
	$percentage=($up/$days) * 100; 
	$anArray = array($avgs[1], $avgs[2], $avgs[3], $up, $users);
	return $anArray;
}
$statArray = serverStats();
$load = $statArray[0] ." ".$statArray[1]." ".$statArray[2];
$up = $statArray[3];
exec ('dmesg | grep memory', &$x);
$availmem = $x[1];
$realmem  = $x[0];
$availmem = explode("(", $availmem);
$realmem = explode("(", $realmem);
$availmem = str_replace(")", "", $availmem);
$realmem = str_replace(")", "", $realmem);
$availmem = str_replace(" ", "", $availmem);
$realmem = str_replace(" ", "", $realmem);
$mem = $availmem[1] . " / " . $realmem[1];
exec ('dmesg | grep CPU', &$y);
$cpu = $y[0];
$logical = $y[1];
$cpu = explode(":", $cpu);
$logical = explode(":", $logical);
$cpuA = $cpu[1];
/////////////////////////
$font = 'LucidaConsole.ttf';
$font2 = 'LucidaConsole.ttf';
$myimage = imagecreatetruecolor(600, 100);
$text = ImageColorAllocate ($myimage,0,0,0);
$black = imagecolorallocate($myimage,0,0,0);
$white = imagecolorallocate($myimage,255,255,255);
imagefill($myimage, 0, 0, $black);
imagettftext($myimage, 14, 0, 2, 15, $white, $font2, "$nm Dedi. Server Stats.");
imagettftext($myimage, 10, 0, 2, 30, $white, $font2, "Server Load: $load");
imagettftext($myimage, 10, 0, 2, 45, $white, $font2, "Uptime: $up");
imagettftext($myimage, 10, 0, 2, 60, $white, $font2, "Memory: $mem");
imagettftext($myimage, 10, 0, 2, 75, $white, $font2, "Processor: $cpuA");
imagettftext($myimage, 10, 0, 485, 95
, $white, $font2, "By: PHPSN.NET");
ImagePNG($myimage);
imagedestroy($myimage);
?>
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Post by Inkyskin »

If you place an htaccess file in the DIR that these "images" are stored, you can add a handler to make png files act as php ones. I would keep all these in a seperate DIR though to avoid breaking any exisitng images you have.
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

Ah, thank you. Now comes the hard part, making it work with lighttpd (as it does not use htaccess files)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

For lighttpd you must configure this all in the central configuration file. As long as you have access to it, though, the sky is the limit with conditionals
Post Reply