3

I'm trying to make an image similar to one of these: http://www.danasoft.com/vipersig.jpg

Not really content wise (displaying IP addresses and headers), but more so a PHP image displayed in a PNG format (so I can use it on websites, within BB image tags).

I know how to create a dynamic image, but that's only when you visit the PHP pages. Currently using this if it's matters:

$image = imagecreate(200, 60);
$text_color = imagecolorallocate($image, 255, 255, 0);

header("Content-type: image/png");

imagestring($image, 4, 40, 30, "hello", $text_color);

imagepng($image);
imagecolordeallocate($text_color);
imagedestroy($image);

Any thoughts on how this could be achieved?

A few more examples of what I'm looking for are: http://draynor.net/signatures

Edit: most comments are about converting from PNG to PHP, I want to convert from PHP to PNG, just to clear it up.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • It actually looks like that code, copied from the php manual will do it. You just need to maybe start with a base image, and change "hello" to something more interesting. – Chris Henry Sep 30 '11 at 04:34
  • @ChrisHenry The problem is I can only access it by going to file.php, instead of file.png, which is what I'm looking for. –  Sep 30 '11 at 04:36
  • But... why? The file extension doesn't matter a whole lot. And you can always use the content disposition header if it should matter for some reason. – Ignacio Vazquez-Abrams Sep 30 '11 at 04:37
  • You can't embed PHP extensions as an image on websites, that's what I'm looking for (something to embed into a website). –  Sep 30 '11 at 04:40
  • If it's *your* website you can definitely embed URLs ending with .php as images. Your browser just cares that the content-type is correct. Maybe some poorly written forum software requires it to end in .png but I don't believe that is the norm. – Carl Zulauf Sep 30 '11 at 04:45
  • It isn't my website, it's a forum using IPB software, seeing as that's well written I should be able to do it as long as the PHP extension isn't blocked? –  Sep 30 '11 at 04:55

2 Answers2

3

You are close, create your image like you have done above, helpful variables would be $_SERVER['REMOTE_ADDR'] (ip address), $_SERVER['HTTP_USER_AGENT'] (browser). See the reserved server variables.

Format them on your image and get it looking how you want by viewing the PHP script in your browser.

Then if you are using Apache, set up a rewrite rule to rewrite a URL to point to your PHP script.

Here is an example:

RewriteEngine On
RewriteRule ^/signatures/sig.png$ /signature.php [L]

This will rewrite http://yoursite.com/signatures/sig.png to http://yoursite.com/signature.php That way users can embed that png URL in their signatures or webpages to display your image.

Eventually you can have multiple signatures and rewrite your URLs by rewriting special parameters (i.e. /signature/1234.png to signature.php?id=1234 or /signature/style1/1234.png to signature.php?id=1234&style=style1).

drew010
  • 68,777
  • 11
  • 134
  • 162
  • See my comment below, this is very informative and if it is the only "easy" way I may have to look into switching free web hosts, could you recommend any that run with apache and don't have bulky urls (such as mysite.stackoverflow.users.com)? –  Sep 30 '11 at 04:38
  • I'm not sure about free, and there are so many web hosts out there, I couldn't really recommend one. For starters you could use a dynamic dns service to point a domain name to your internet connection and host your own apache server. But if you look you can find many inexpensive web hosts that will have more than everything you need and good service. – drew010 Sep 30 '11 at 04:54
  • Alright, thank you very much as well as all other other comments from all of you guys, I appreciate it! I'll look into this and hopefully it will work out as easily as it seems! –  Sep 30 '11 at 05:00
1

If you're hosting on apache, you can use mod_rewrite. Here's a sample (not tested):

RewriteEngine On
RewriteRule /signatures/([A-Za-z0-9])+\.png$ /signature.php?name=$1

Here's a tutorial: http://www.easymodrewrite.com/ More info: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Matt Williamson
  • 39,165
  • 10
  • 64
  • 72
  • My webhost doesn't run on apache (afaik, the website is http://alwaysdata.net/ if you happen to know anything about it), is there any other ways that you could do it without needing that, or could you suggest a free webhost without a horrible bulky URL that runs apache? –  Sep 30 '11 at 04:35