Shoutbox

PHP GD help - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: PHP GD help (/showthread.php?tid=50232)

PHP GD help by Ezra on 09-08-2005 at 07:35 PM

I made a small code to make a random text-sig some while ago, and today I added Sign the Petition to it, but I realised it was too small to fit the text, so I made it double the size, but not it doesn't look nice with smaller texts anymore...


Anywaaaay, what im trying to ask is :P, can someone help me to make it auto-adjust the size to the text.

I already tried it with imagettfbox but couldn't get it to work. Because I don't think my server can use ttf font's, and I can't change any settings cause it's my hosting company's server.

TIA

And below my code :)

code:
<?php


//randomizer
srand((float) microtime() * 10000000);
$input = array("Ezra", "TSDME", "MsgPlus!", "Nintendo", "SPAM", "Web2Messenger", "Sign the Petition");
$rand_keys = array_rand($input, 2);
$string =  $input[$rand_keys[0]];

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

//
$im = @imagecreate(160, 20);
$background_color = imagecolorallocate($im, 100, 100, 100);
$text_color = imagecolorallocate($im, 000, 000, 200);

//
imagecolortransparent($im, $background_color);
imagestring($im, 5, 7, 3, $string, $text_color);
imagegif($im);
imagedestroy($im);

//statistics
include_once('connection.inc.php');
opendb();

$query = "SELECT * FROM stats";
$result = mysql_query($query);

while($row = mysql_fetch_array($result)){
$sts = ($row[stats]) + 1;
}

$sql = "UPDATE stats
           SET stats = '$sts'";
             
mysql_query($sql);
?>



RE: PHP GD help by KeyStorm on 09-08-2005 at 07:57 PM

imagettfbox is very picky with the font path. Firstly you should check your PHPinfo to see if freetext (2 if possible) is installed. :)


RE: PHP GD help by Ezra on 09-08-2005 at 08:03 PM

Freetext is not installed, Gettext is installed not sure if that's any use to it, but it's called text so I thougt it might be worth telling :-)

And I have PHP4 and 5 btw, so I can also use PHP5only functions if needed.


RE: PHP GD help by KeyStorm on 09-08-2005 at 08:16 PM

Well... check if the GD section of any of both versions says something about fonts or ttf?

Do you get an error when you use that function and not change the headers to image?


RE: PHP GD help by Ezra on 09-08-2005 at 11:02 PM

Stupid Windows FTP, I needed to upload the .ttf file in binary mode, but somehow Windows transferred in ASCII mode or something, because when I forced Binary mode with FireFTP It worked :-)

Anywaay, still working on it, the text looks a bit strange, I'll see if I can make it better and to get the resizing to work :)

progress: http://scripts.tsdme.nl/randomgif.php


RE: PHP GD help by brian on 09-08-2005 at 11:09 PM

I don't like the font? Or is it my explorer?


RE: PHP GD help by Ezra on 09-08-2005 at 11:24 PM

I don't like it either it's supposed to be arial, but it's buttugly right now :P

Anywaay, the resizing works :)

Now, I just need to make the font look nice :P

UH.... It's not showing on IE :S, it's just a GIF with transparancy...

Hmm, nvm, working now on IE :), crazy IE...


So does anyone know how to make it look nicer?

BTW: I still want a transparent background.


RE: PHP GD help by KeyStorm on 09-09-2005 at 01:35 PM

Gif will always do hard backgrounds.

Youy may use PNG.

code:
$img = imagecreatetruecolor($width, $height)
imageAlphaBlending($img, true);
imageSaveAlpha($img, true);
imagecolorallocate($img,0,0,0,127);
[/color]

This will make a transparent backgrounded PNG

You must note that MSIE 6 is the only browser that does not support them. 7 is said to do.

RE: PHP GD help by Ezra on 09-09-2005 at 02:37 PM

On the php.net pages it says I can't use imagesavealpha when imagealphablending is enabled :S

And I keep having a black background for some reason :s


RE: PHP GD help by L. Coyote on 09-09-2005 at 02:40 PM

quote:
Originally posted by Ezra
And I keep having a black background for some reason
Are you using IE?

quote:
Originally posted by KeyStorm
You must note that MSIE 6 is the only browser that does not support them. 7 is said to do.

RE: PHP GD help by -dt- on 09-09-2005 at 02:46 PM

hmm i always found that using the code below worked alot better for transparent backgrounds than the whole alpha blending thing. (use the below code after you create your image)

code:
$tran = imagecolorallocate($image, 233,231,222);
$tran = imagecolortransparent($image,$tran);
imagefill($image,0,0,$tran);


RE: PHP GD help by KeyStorm on 09-09-2005 at 02:48 PM

That will still cause the same hard-edges effect to any ttftext he adds... :-/

Edit:

quote:
Originally posted by Ezra
On the php.net pages it says I can't use imagesavealpha when imagealphablending is enabled :S

It woks on my sig, even If I place the text on the alpha sector. :-/
RE: PHP GD help by Ezra on 09-09-2005 at 03:07 PM

Hmm, but I keep having a black background if I use a TrueColor image...


RE: PHP GD help by KeyStorm on 09-09-2005 at 03:36 PM

Oh, sorry
it's colorallocatealpha($img,$r,$g,$b,$alpha)


RE: PHP GD help by Ezra on 09-09-2005 at 04:14 PM

Still the black background :S

This is what I have now: (Not all of it, just the image part :-))

code:
$im = imagecreatetruecolor($width, $height);
imagealphablending($im, true);
imagesavealpha($im, true);
imagecolorallocatealpha($im, 0, 0, 0, 127);
$font['color'] = imagecolorallocatealpha($im, 0, 0, 255, 25);
imagettftext($im, $font['size'], 0, 0, 15, $font['color'], $font['path'], $string); //TTF text
imagepng($im);
imagedestroy($im);


I'm using FF 1.0.6 btw :), so no IE here :)
RE: PHP GD help by KeyStorm on 09-09-2005 at 05:06 PM

code:
$im = imagecreatetruecolor(50, 50);
imagesavealpha($im,true);
imagealphablending($im, false);
$bg = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im,0,0,$bg);

$font['color'] = imagecolorallocatealpha($im, 0, 0, 255, 25);
imagettftext($im, 10, 0, 0, 15, $font['color'], $font['path'], 'test'); //TTF text
header("Content-type: image/png");
imagepng($im);

imagedestroy($im);


This works :)
[Image: ezra.php]
RE: PHP GD help by Ezra on 09-09-2005 at 05:30 PM

Excellent :-)

I read about that imagefill, but it gave me a 500 error when I tried to use it...

Anywaay, thanks a million :)

I'll use a simple palette with ugly text for IE users :P
and this one for better browser users :)


RE: PHP GD help by KeyStorm on 09-09-2005 at 05:33 PM

Or just do what I did with my sig: a version for IE and one for standars-complying ;)

(look at it in IE)


RE: PHP GD help by Ezra on 09-09-2005 at 06:03 PM

I have it like this.
Alpha transparancy for good browsers and normal (ugly) transparancy for IE.

[Image: sig.php]