Forum Moderators: coopster

Message Too Old, No Replies

dynamically generated images

         

ajs83

11:50 pm on Sep 29, 2005 (gmt 0)

10+ Year Member



I have the following code that generates an image based on the variable appended to a url (example: example.com/file.php?id=1111), it works great, but I have two questions.

1) How do I set it up so that users can place it in their signatures, sites, etc where php is disabled? (right now the link is like example.com/file.php?id=1111 and, obviously, that can be blocked in some locations for security concerns). I'd like to be able allow them to link to something like 1111.gif.

2) How can I add an additional image to appear similar to text separate of the background?

<?
$id = $_GET['id'];

Header ("Content-type: image/gif");

$img_handle = imageCreateFromGIF("http://www.blah.com/dynamic.gif");

$color = ImageColorAllocate ($img_handle, 255, 255, 255);
$colorr = ImageColorAllocate ($img_handle, 000, 000, 000);
$colorrr = ImageColorAllocate ($img_handle, 003, 078, 163);

ImageString ($img_handle, 4, 220, 2, "Item 1", $color);
ImageString ($img_handle, 3, 238, 21, "Item 2", $colorr);
ImageString ($img_handle, 3, 215, 37, "Item 3", $colorr);
if($coffer == 0) {
ImageString ($img_handle, 3, 215, 52, "Item 4", $colorr);
}else{
ImageString ($img_handle, 3, 215, 52, "Item 5", $colorr);
}
ImageString ($img_handle, 3, 215, 68, "Item 6", $colorr);
ImageString ($img_handle, 2, 312, 85, "Item 7", $colorrr);

ImagePng ($img_handle);

ImageDestroy ($img_handle);

?>

coopster

8:12 pm on Sep 30, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You mean you want to create an image from text? There are quite a few examples on the Image Functions [php.net] manual pages.

ajs83

10:09 pm on Sep 30, 2005 (gmt 0)

10+ Year Member



I have the image and it is (pretty much) working as expected, but I have two unresolved issues. The first is I would like to include an image (separate of the background) and the second is getting it to generate the gif/png file (or at least resolving).

whoisgregg

8:49 pm on Oct 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just wrapped up an enormous PHP GD project, so I better be able to help with these questions. :)

Re: 1) Two ways to do this. One, you can recreate the image dynamically each time it is requested (conserves disk space, eats CPU). Two, you can write the image to your server's disk space then serve up the static image to users (eats disk space, conserves CPU).

To recreate the image each time, give the user an img tag that directly calls your script:

<img src="http://yourdomain.com/yourscript.php?id=1234" />

It doesn't matter to the user if they have php enabled or disabled -- your server is doing the php work, they're just calling an image file from a different server.

To store the image to your server's disk space, add a second argument to your last imagepng function, like so:

imagepng($img_handle, 'savedimages/'.$id.'.png');

Then your users can call that image with something like:

<img src="http://yourdomain.com/savedimages/1234.png" />

whoisgregg

8:57 pm on Oct 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Re: 2) Do you mean you want an existing graphic from your server to be used as part of this dynamic image? That's easy to do, just make another image, then copy it onto your main image:

$img_handle2 = imageCreateFromGIF("http://www.blah.com/dynamic2.gif"); 
imagecopy ( $img_handle, $img_handle2, 0, 0, 0, 0, imagesx($img_handle2), imagesy($img_handle2) );
imagedestroy ( $img_handle2 );

To change where the second image is placed on the first image, just change the first 0, 0 pair. Want it 10 pixels right and down from the top left corner? Use:

imagecopy ( $img_handle, $img_handle2, 10, 10, 0, 0, imagesx($img_handle2), imagesy($img_handle2) );

Edited to fix sidescroll. Arg!

whoisgregg

9:01 pm on Oct 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, before I forget... Make sure to 'match up' your headers and final output to be the same file type, either gif, png, jpeg, whatever. Also, if you are outputting to a static file, do not send headers... if you want to show the image in that scenario, use an <img> tag after the php script to call the static image.