[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

PHP Image Creation

Hi Guys:

I have seen this done before, but not sure if I can apply it to what I need... What I want to do is to allow webmasters to put a button of mine on their website. However, the catch is I want to be able to place some dynamic content in that button. To be specific, the visitors IP address. Now I have specific image background I want to use but not sure if its possible. Below I have included the code as it would appear if in a HTML page:

------------------------------------------
<?php
$proxy = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
$IP = $HTTP_SERVER_VARS["REMOTE_ADDR"];
?>
<html>

<head>
<title>Website Button</title>
</head>

<body topmargin="0" leftmargin="0">

<table border="0" width="142" cellspacing="0" cellpadding="0" height="32">
  <tr>
    <td width="100%" background="button.gif" align="center">
      <?php if ( isset($IP) && ($proxy) )
      {
            echo "<font color='#FFFFFF' size='2' face='Tahoma'><b>IP: </b>";
            echo $proxy;
            echo "</font></td>";
      }
      else
      {
            echo "<font color='#FFFFFF' size='2' face='Tahoma'><b>IP: </b>";
            echo $IP;
            echo "</font></td>";
      }
      ?>
  </tr>
</table>
</body>

</html>
------------------------------------------

Thanks in advance guys!
0
SR301
Asked:
SR301
  • 8
  • 8
1 Solution
 
crackyCommented:
Are you asking whether the code would work or whether the formatting is right?

Have you tested this code? If you are using a more recent PHP version, you can access the $_SERVER superglobal rather than the depreciated $HTTP_SERVER_VARS.

Note that to use this code, the webmaster must have PHP installed and have the correct path to your button. Also, from memory, the background element of the td element is also depreciated, so you would be better off generating the button using CSS.
0
 
SR301Author Commented:
I would run all of it on my own server, and they would be linking to it !

What I'm saying is, I need to create each button every time someone requests it... The code I posted, will simply place the IP address over the image which is how I want it to 'appear'. My question is, how can I make the image WITH the IP address already over the top ?
0
 
crackyCommented:
Sounds like you would need to use GD.
http://www.php.net/gd

This is how I would do it:

- Get the remote site to link to your image using http://www.yoursite.com/images/button.png

- Use Apache mod_rewrite to rewrite that URL to a PHP script.
http://httpd.apache.org/docs/misc/rewriteguide.html

- Generate the button using GD and place the remote server variables on the button using GD.
http://codewalkers.com/tutorials.php?show=3&page=1

- Use PHP header() to send the output as PNG. E.G: header("Content-Type: image/png")

I would be happy to help you through the stages. The above links will equip you to get started and you can always ask questions along the way related to specific sticking points.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
SR301Author Commented:
Would be happy to get your help on every stage more like it lmao ! Will increase the points for you of course... :)

Regards,

SR...
0
 
SR301Author Commented:
Point increase from 250 to 500... :)
0
 
crackyCommented:
This would take a bit of coding and I believe it's better to learn the practices by going through the steps than just have someone write it for you. I write code for money, I'll help you for free ;)

So if it's code that you want, I can't do it, but I would be more than happy to help you if you reach a sticking point with existing code.

It might be helpful if you make a start and then ask a question related to a specific stage.
0
 
SR301Author Commented:
OK - This is what I came up with:

Question being: How can I change the text colour to white, font type to tahoma, size and center it ?

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

// Some code removed...
      
$im    = imagecreatefrompng("button.png");
$px    = (imagesx($im) - 7.5 * strlen($IP)) / 2;
imagestring($im, 3, $px, 9, $IP, $orange);
imagepng($im);
imagedestroy($im);
?>
0
 
SR301Author Commented:
When I made an attempt to use the following function I *think* I should be using, the follow occurs:

Call to undefined function:  imagettftext()

Does this mean I can't use specific fonts ?

Also worked out the color problem with the following:

$textcolor = imagecolorallocate($im, 255, 255, 255);
imagestring($im, 3, $px, 9, $IP, $textcolor);
0
 
crackyCommented:
Have a look at the user contributed notes in the manual for:
http://www.php.net/manual/en/function.imagestring.php

There are a few comments which address your exact issue.

Also, have you tried http://www.php.net/imagettftext ?
0
 
crackyCommented:
To use imagettftext, you need the FreeType 2 library installed. This article describes how this is done:
http://www.onlamp.com/pub/a/php/2003/03/27/php_gd.html?page=2

It seems like you might be able to write decent text without the lib, but using the lib is better.
0
 
SR301Author Commented:
Having that installed is something for my webhost is it not ?
0
 
crackyCommented:
It depends if you have root access to a dedicated server or VPS. If you don't, or are not confident installing new libraries, then I would post a ticket to your hosting support asking them to install the FreeType 2 lib.

If you look at the user contributed notes for imagestring, you might be able to still write that text, but FreeType is prettier.
0
 
SR301Author Commented:
OK... This is what I came up with so if you could just check on it, make any improvments:
//Some code removed from top

$im    = imagecreatefrompng("button.png");
$textcolor = imagecolorallocate($im, 255, 255, 255);
$x = (imagesx($im) / 2 - strlen("IP: $IP") * imagefontwidth("IP: $IP") / 2) / 2 + (imagesx($im) / 2 - strlen("IP: $IP") * imagefontwidth("IP: $IP") / 2) / 4;
$y = 10;
imagestring($im, 2, $x, $y, "IP: $IP", $textcolor);
imagepng($im);
imagedestroy($im);

Now that $x var should center the text as best as possible, but I didn't worry about height...
0
 
crackyCommented:
That looks like it would work. Have you tried it?

Try to load the script and see what happens ...
0
 
SR301Author Commented:
Yeah, Works Fine...
Was just looking for any possible improvments... :)

Don't think I will bother in regards to having my host set up the new fonts... I have noticed some of the newer functions in PHP don't work anyway - So, until they upgrade PHP, I dare say I'm wasting my time anyway !
0
 
crackyCommented:
Cool. Glad you got it working :)

cracky
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now