PDA

View Full Version : Any PHP programmers out there?



Beowulf
20th-April-2007, 02:59 PM
Yeah.. ask a programming query on a dance forum. I suppose next I'll be asking about footwork on my Visual Studio forum ;)

I have a little PHP script that outputs a dynamically created gif image


<?
Header("Content-type: image/gif");
..
..
.. Various bits of code here that aren't important to the question
..
..

$size = imagettfbbox($sz,0,"/fonts/times.ttf",$text);
$dx = abs($size[2]-$size[0]);
$dy = abs($size[5]-$size[3]);
$xpad=9;
$ypad=9;
$im = imagecreate($dx+$xpad,$dy+$ypad);
$blue = ImageColorAllocate($im, 0x2c,0x6D,0xAF);
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
ImageRectangle($im,0,0,$dx+$xpad-1,$dy+$ypad-1,$black);
ImageRectangle($im,0,0,$dx+$xpad,$dy+$ypad,$white) ;
ImageTTFText($im, $sz, 0, (int)($xpad/2)+1, $dy+(int)($ypad/2), $black, "/fonts/times.ttf", $text);
ImageTTFText($im, $sz, 0, (int)($xpad/2), $dy+(int)($ypad/2)-1, $white, "/fonts/times.ttf", $text);
ImageGif($im);
ImageDestroy($im);

}
?>

Now, it works fine. Until I pass in parameters in the URL

Suppose it's called doesntwork.php and I pass in


http://www.skyfall.co.uk/doesntwork.php?text=Hello!

I get a url link instead NOT the image.
ie
http://www.skyfall.co.uk/doesntwork.php?text=Hello!
(clicking on the url does work though)

However if I put the line $text="hello!" in the body of the php and use

http://www.skyfall.co.uk/image.php
without any parameters then I get

http://www.skyfall.co.uk/image.php

Any ideas how to have a dynamically created php gif using parameters that is still recognised as a valid gif image when run?

dave the scaffolder
20th-April-2007, 03:03 PM
I could pop you up a nice working platform with a lovely double handrail and crashdeck.

If thats any help. XX XXX

Beowulf
20th-April-2007, 03:15 PM
Moderators!!

can we have a special "Scaffolders workyard" area of the forum please. Where Dave and his fellow scaffolders can discuss scaffold stability, and pole diamerter, etc I promise not to ask any geeky questions in there.

Silly me thinking a Geeky question in the Geek's Corner would have been answered by a Geek. :rolleyes:

TheTramp
20th-April-2007, 03:17 PM
Silly me thinking a Geeky question in the Geek's Corner would have been answered by a Geek. :rolleyes:

Well, give them a chance. It's only been 16 minutes since you put up the question. They're probably off, doing geeky things.

Oh, and Dave, should I ever need any scaffolding, I'll be sure to keep you in mind.

Beowulf
20th-April-2007, 03:26 PM
Well, give them a chance. It's only been 16 minutes since you put up the question.

I wasn't expecting immediate gratification.. I was ,however, expecting the eventual first reply to be a sensible one :rolleyes:


They're probably off, doing geeky things.

pah!!.. True geeks can multitask !!

straycat
20th-April-2007, 03:33 PM
Mmm. Tricky(ish) one. The problem really seems to lie with the forum software's definition of an image - there really isn't a problem with putting get params into an image URL - it's just that it doesn't seem to sit too well with the IMG tag on vBulletin.

What I generally do to get around stuff like this is to use Apache RewriteRules. Which assumes that a) you're using Apache, and b) you have enough access to modify your apache conf files. Which, of course, isn't always the case (I rent my own vps, so this isn't a problem).

Basically, I'd have a rewrite rule which would take a url like:


http://www/skyfall.co.uk/images/dynamic/Hello+Beo%21.gif

And map it to the script: doesntwork.php supplying the get param Hello+Beo%21

Fairly easy if you have a basic knowledge of regular expressions (which I'm sure you do)

Let me know if you'd like any more detail on this technique - I use it a lot in various different forms.

TurboTomato
20th-April-2007, 03:40 PM
I could pop you up a nice working platform with a lovely double handrail and crashdeck.

If thats any help. XX XXX

:rofl: Can't give you rep though :rolleyes:

Beowulf
20th-April-2007, 03:47 PM
Basically, I'd have a rewrite rule which would take a url like:


http://www/skyfall.co.uk/images/dynamic/Hello+Beo%21.gif

And map it to the script: doesntwork.php supplying the get param Hello+Beo%21

Fairly easy if you have a basic knowledge of regular expressions (which I'm sure you do)

Let me know if you'd like any more detail on this technique - I use it a lot in various different forms.

Hmm, I'd considered that, and I do have fairly complete access to my Apache installation.

I had also considered using the $PATH_INFO environment variable
and instead of passing in


http://www.skyfall.co.uk/doesntwork.php?text=hello%20Straycat!

I would instead pass in


http://www.skyfall.co.uk/doesntwork.php/text/Hello%20Straycat!

then basically resolve this in code by



<?php

if(isset($PATH_INFO)) {

$paramdata = explode('/', $PATH_INFO);

$num_param = count($paramdata);

if($num_param % 2 == 0) {

$paramdata[] = '';
$num_param++;
}

for(var $i = 1; $i < $num_param; $i += 2) {

$$paramdata[$i] = $paramdata[$i+1];
}
}

?>

but that probably still wouldn't get past the .php part in vBulletin !!

hmm.

straycat
20th-April-2007, 03:56 PM
Yeah - $PATH_INFO (or one of the other Apache vars) would do nicely. Don't forget - you don't need .php in there anywhere. If you set the rewriterule to check only paths like /images/dynamic/.+\.gif, and map that onto a php script, vBulletin would see that as a normal gif.

Check out the following image:
http://lindy-jazz.co.uk/library/images/dynamic_thumbs/150x150/14.jpg

Which has the following url.

http://lindy-jazz.co.uk/library/images/dynamic_thumbs/150x150/14.jpg

If you change the 150x150 part, you can change the max size of the image - this is being done dynamically through a php script - have a play and see. Uses exactly the technique we're talking about - except that the script actually creates and saves the right image on the server, which the rewriterule will check for - so the next time around, the image will actually exist, and won't have to be generated.

Beowulf
27th-April-2007, 01:04 PM
A little .htaccess file editing and

http://www.skyfall.co.uk/scripts/Hahahaha it works!!.gif

Bingo !!.. Now to use that snippet of code in the real PHP scripts :) :clap:

:respect: Stray !!

ducasi
27th-April-2007, 01:25 PM
Yeah, vBulletin by default doesn't allow "dynamic" images – but it recognises them purely by the "?" in the URL.

Beowulf
27th-April-2007, 01:30 PM
Yeah, vBulletin by default doesn't allow "dynamic" images – but it recognises them purely by the "?" in the URL.

As is always the case..the most simple method is usually the best !!

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^scripts/(.+).gif doesntwork.php?text=$1 [nc]

http://www.skyfall.co.uk/scripts/Thanks Straycat and Co!!.gif

bigdjiver
27th-April-2007, 02:17 PM
:sad: I can remember when all you needed to get a picture up was a hammer and a nail ...