Jump to content

[SOLVED] Dynamic Image From User Input


Rathlar

Recommended Posts

Ok... well I'm trying to do an online/offline status image code which users can modify the url to output their own custom status checker.

 

here's the code I have so far... I know it's kinda sloppy but I just need it to work and then I can clean it up later.

<?php

Function testconn()
{
	$socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
	$address = gethostbyname($_GET['host']);
	$service_port = $_GET['port'];
	$result = socket_connect($socket, $address, $service_port);
	if($result == "")
		{
		$im = imagecreatefrompng($_GET['offline']);
		}
	else
		{
		$im = imagecreatefrompng($_GET['online']);
		}
}
header("Content-Type: image/png");
testconn();
imagepng($im);
imagedestroy($im);

?>

I also know this requires allow_url_fopen to be turned on in php.ini which I do have on.

 

So far all I can manage to get is the url displayed in the browser with no errors other than the fact that the image isnt showing up.

 

Any help would be greatly appreciated.

Link to comment
Share on other sites

not really sure i'm following your code, but for starters $im is not a global var because its in your function.

 

at the end of your function, before the last bracket }  you need:  return $im;

 

then, when you call the function, asign $im to a var; like this: $im = testconn();

Link to comment
Share on other sites

I did exactly what you told me and it still won't output the image.

 

			}
return $im;
}
header("Content-Type: image/png");
$im = testconn();
imagepng($im);
imagedestroy($im);

?>

 

Same exact result as before...

 

Basically what I'm trying to do is when the user uses the URL:

/status.php?offline=URL_of_PNG1&online=URL_of_PNG2&host=HOSTNAME&port=PORT_NUM

 

They will be displayed the server status of the server they chose in the URL in png output.

 

More or less a dynamic image based on the user input and conditional based on the results of the socket connection.

Link to comment
Share on other sites

gah... triple posting... i wish i could edit my posts...

 

anyway I fixed it myself... i should have been doing sockets differently...

 

<?php 
header("Content-Type: image/png");
$address = gethostbyname($_GET['host']);
$port = $_GET['port'];
if (! $sock = @fsockopen($address, $port, $num, $error, 5)) 
  $im = imagecreatefrompng($_GET['offline']); 
else{ 
  $im = imagecreatefrompng($_GET['online']); 
  fclose($sock);
}
imagepng($im); 
?>

 

less code and works like a charm  :)

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.