PDA

View Full Version : imagettftext with URL param?


student101
07-05-2008, 09:30 AM
This script will read any fonts folders and print the font to the screen.
It all works great but I can't seem to use sessions, forms or URLs to display in the font I have chosen, see this line, I think?imagettftext($im,10,0,5,25,$black, "$ffolder/".$_GET['f'] , 'any text');
I try to replace 'any text' with $_GET['n'] and run the page with pagename.php?n=cool idea it doesn't work, any ideas at what I'm doing wrong?

$ffolder="fonts";
if (empty($_GET['f'])){
$folder=dir($ffolder); //open directory
echo "<HTML><BODY>\n";

while($font=$folder->read())
if(stristr($font,'.ttf')) //only ttf fonts
$fonts[]=$font;

$folder->close();
if (!empty($fonts)){
echo "<table><tr><th colspan='2'>Fonts available in the <em>$ffolder</em> folder</th></tr>"."\n<tr><th>Name</th><th>Appereance</th>";
sort($fonts);
foreach ($fonts as $font)
echo "<tr><td>$font</td><td> <IMG src='".$_SERVER['SCRIPT_NAME']."?f=$font'></td></tr>\n";
}else echo "No fonts found from $ffolder";
echo "\n</HTML></BODY>";
}else{
$im=@imagecreatetruecolor(200,30)
or die("Cannot Initialize new GD image stream");
$black=imagecolorallocate($im,0,0,0);
$white=imagecolorallocate($im,255,255,255);
imagefill($im,0,0,$white);

imagettftext($im,10,0,5,25,$black, "$ffolder/".$_GET['f'] , 'any text');
// this one works with the actual font name, ie: $_GET['f']
//imagettftext($im,14,0,5,25,$black, "$ffolder/".$_GET['f'] , $_GET['f']);

header("Content-type: image/png");
header('Content-Length: ' . strlen($im));
imagepng($im);
imagedestroy($im);
}

davidj
07-05-2008, 10:44 AM
try assigning the $_GET['f'] to a var and use that

student101
07-05-2008, 10:59 AM
Changing that doesn't make a difference.

$f = $_GET['f'];
$n = $_GET['n'];
imagettftext($im,10,0,5,25,$black, "$ffolder/".$f , $n);

Using this doesn't work http://localhost/pagename.php?n=test (http://localhost/pagename.php?n=test)
I think $n needs to be a string?

davidj
07-05-2008, 11:05 AM
you need to run the original script and make sure it works then you need to echo out the $_GET's to see if they contain the correct values

student101
07-05-2008, 11:09 AM
you need to run the original script and make sure it works then you need to echo out the $_GET's to see if they contain the correct values

This is what I am running, it's all original.

$f = $_GET['f'];
$n = $_GET['n'];
$ffolder="fonts";
if (empty($f)){
$folder=dir($ffolder); //open directory
echo "<HTML><BODY>\n";

while($font=$folder->read())
if(stristr($font,'.ttf')) //only ttf fonts
$fonts[]=$font;

$folder->close();
if (!empty($fonts)){
echo "<table><tr><th colspan='2'>Fonts available in the <em>$ffolder</em> folder</th></tr>"."\n<tr><th>Name</th><th>Appereance</th>";
sort($fonts);
foreach ($fonts as $font)
echo "<tr><td>$font</td><td> <IMG src='".$_SERVER['SCRIPT_NAME']."?f=$font'></td></tr>\n";
}else echo "No fonts found from $ffolder";
echo "\n</HTML></BODY>";
}else{
$im=@imagecreatetruecolor(200,30)
or die("Cannot Initialize new GD image stream");
$black=imagecolorallocate($im,0,0,0);
$white=imagecolorallocate($im,255,255,255);
imagefill($im,0,0,$white);

// ------------ Not sure if it's correct as nothing appears.

imagettftext($im,10,0,5,25,$black, "$ffolder/".$f , $n);

// -------------------------------

header("Content-type: image/png");
header('Content-Length: ' . strlen($im));
imagepng($im);
imagedestroy($im);
}

davidj
07-05-2008, 11:24 AM
what values ar you posting to the $_GET's

are they catching the correct values

student101
07-05-2008, 11:28 AM
Forgot to add these to my previous post:
http://localhost/pagname.php?n=test (http://localhost/pagname.php?n=test)

echo $f."<br>";
echo $n;


Does it work for you?
If I add this instead, it works, it just wont get the text being sent via URL
$n = "test";

davidj
07-05-2008, 11:35 AM
what are you passing through the $_GETS

do they contain anything

student101
07-05-2008, 11:38 AM
Yes my URL is this:
http://localhost/gdsite/pagname.php?n=test (http://localhost/gdsite/pagname.php?n=test)
test is the data

Doesn't get mean $_GET['URL value']

student101
07-05-2008, 11:56 AM
For some reason I don't think this is possible.
As much as I appreciate PHP's robust system, some things just wont work.
Unless it's my code.

davidj
07-05-2008, 11:56 AM
please list what is in both $_GETS

student101
07-05-2008, 12:10 PM
get f = GOTHIC.TTF
get n = test ; URL value (anything you enter)
This works like this:
http://localhost:/gdsite/pagename.php?f=GOTHIC.TTF&n=test
It asks to save the image file in IE 7 though.
It displays perfectly it in Firefox, but only with the font as f.

davidj
07-05-2008, 12:12 PM
where are you defining the ampersand & in the query string

student101
07-05-2008, 12:17 PM
I'm not, it's done manually.

davidj
07-05-2008, 12:22 PM
eh

i think we are on different planets with this

all i can sugest is that you run the original code without your mods added

if it works then you need to test what your wanting to pass into it as its your vars which must be at fault

its the process of elimination and part of learning php. You may have to debug someone elses code one day

student101
07-05-2008, 12:31 PM
The original code works fine, and I can use the $f, I can't use any URL value

the & sign comes after calling the font;
f=font&n=test

I did that for test purposes.
The problem is the ?n= part
I can't get the URL value, ?n=test to be displayed unless I use the font name in URL as well.


If I use this, then it works
http://localhost/gdsite/pagename.php?GOTHIC.TTF&n=test

If I use this then it doesn't work
http://localhost/gdsite/pagename.php?n=test

davidj
07-05-2008, 12:37 PM
will need to check this out

will havr to be a bit later though

student101
07-05-2008, 12:39 PM
Cool, thanks.
Not sure why it's giving me an issue as such.

student101
07-05-2008, 01:20 PM
Fixed, changed everything...

header("Content-type: image/png");
$im = imagecreatetruecolor(400, 30);
$white = imagecolorallocate($im, 255, 255, 255) ;
$grey = imagecolorallocate($im, 128, 128, 128) ;
$black = imagecolorallocate($im, 0, 0, 0) ;
imagefilledrectangle($im, 0, 0, 399, 29, $white) ;
$text = $_REQUEST['n'] ;
$font = 'fonts/GOTHIC.TTF' ;
imagettftext($im, 12, 0, 10, 20, $black, $font, $text) ;
imagepng($im) ;
imagedestroy($im) ;

Hope this would be useful to others.

Thank you DJ, made me realise that there was too much c^ap on my page

Cheers