Jump to content

GD Library help


--ss--

Recommended Posts

Well basically I've written the script below to connect to a SQL databse, retrieve a random name for it, with the name it gets an image from an external sites and overlays it ontop of a background then writes the name as a string onto the image and finally outputs it.

It was all working fine when all it had to do was get the image, I tried making it write the text but it messed up and now I keep getting errors :/

 

Any suggestions?

<?php
header("Content-type: image/gif");
//Info
$dbhost = "localhost";
$dbuser = "   ";
$dbpass = "   ";
$dbname = "   ";

//Connect
mysql_connect("$dbhost", "$dbuser", "$dbpass");
mysql_select_db("$dbname");

//Random Habbo
$random = mysql_query("SELECT name FROM names ORDER BY RAND() LIMIT 1");
$habbo = mysql_fetch_array($random);

//Generate Image
$text = $habbo[0];
$white = imagecolorallocate($im, 255, 255, 255);
$font = volter.ttf;
$im = imagecreatefromgif("bg.gif");
$in = imagecreatefromgif("http://www.habbo.co.uk/habbo-imaging/avatarimage?user=$habbo[0]&action=std&direction=4&head_direction=3&gesture=sml&size=l&img_format=gif");
$in_x = imagesx($in);
$in_y = imagesy($in);
imagecopymerge($im,$in,24,11,0,0,$in_x,$in_y,100);
imagettftext($im, 8, 0, 10, 20, $white, $font, $text);
imagegif($im);

imagedestroy($im);
?>

Btw, I know the database info is missing, that's cause I removed it myself.

 

Thanks for reading :).

Link to comment
Share on other sites

$in = imagecreatefromgif("http://www.habbo.co.uk/habbo-imaging/avatarimage?user=$habbo[0]&action=std&direction=4&head_direction=3&gesture=sml&size=l&img_format=gif");

 

should be

$in = imagecreatefromgif("http://www.habbo.co.uk/habbo-imaging/avatarimage?user=$text&action=std&direction=4&head_direction=3&gesture=sml&size=l&img_format=gif");

 

But without knowing the errors i can't say!

Link to comment
Share on other sites

$in = imagecreatefromgif("http://www.habbo.co.uk/habbo-imaging/avatarimage?user=$habbo[0]&action=std&direction=4&head_direction=3&gesture=sml&size=l&img_format=gif");

 

should be

$in = imagecreatefromgif("http://www.habbo.co.uk/habbo-imaging/avatarimage?user=$text&action=std&direction=4&head_direction=3&gesture=sml&size=l&img_format=gif");

 

But without knowing the errors i can't say!

Technically it's just the same thing so it doesn't matter but thanks for pointing it out :P

 

Anyway the error I get is the normal GD Library error off:

The image “http://www.shanes.sawhosting.com/php/show.php” cannot be displayed, because it contains errors.

 

I beleive it has nothing to do with the actual two images merging and the first part and it's the text bit which is causing problematic as that's when the error started appearing :(.

Link to comment
Share on other sites

imagecreatefromgif() takes a file name, not a URL.

I've used an almost identical script to this in the past where it did the same job of merging two images and it's always worked before though?

 

As I said , I believe that part is fine as it only started messing up when I tried adding text :(.

Link to comment
Share on other sites

ah, that's new info for me. thanks for that.

 

is your font file in the same directory as the script?

Indeed it is.

 

I'm not sure why this is happening now but when I remove the line to add text the script messed up (Even though it was fine before) :(

Link to comment
Share on other sites

this works for me:

 

<?php
$habbo = "hello world";

//Generate Image
$text = $habbo;
$font = 'Andale Mono.ttf';
$im = imagecreatefromgif("coloradoflag.gif");
$white = imagecolorallocate($im, 255, 255, 255);
$in = imagecreatefromgif("http://www.habbo.co.uk/habbo-imaging/avatarimage?user=$habbo[0]&action=std&direction=4&head_direction=3&gesture=sml&size=l&img_format=gif");
$in_x = imagesx($in);
$in_y = imagesy($in);
imagecopymerge($im,$in,24,11,0,0,$in_x,$in_y,100);
imagettftext($im, 8, 0, 10, 20, $white, $font, $text);
header("Content-type: image/gif");
imagegif($im);

imagedestroy($im);
?>

 

note my changed font. i don't have the one you used.

Link to comment
Share on other sites

this works for me:

 

<?php
$habbo = "hello world";

//Generate Image
$text = $habbo;
$font = 'Andale Mono.ttf';
$im = imagecreatefromgif("coloradoflag.gif");
$white = imagecolorallocate($im, 255, 255, 255);
$in = imagecreatefromgif("http://www.habbo.co.uk/habbo-imaging/avatarimage?user=$habbo[0]&action=std&direction=4&head_direction=3&gesture=sml&size=l&img_format=gif");
$in_x = imagesx($in);
$in_y = imagesy($in);
imagecopymerge($im,$in,24,11,0,0,$in_x,$in_y,100);
imagettftext($im, 8, 0, 10, 20, $white, $font, $text);
header("Content-type: image/gif");
imagegif($im);

imagedestroy($im);
?>

 

note my changed font. i don't have the one you used.

 

Yay, now we're getting somewhere:

I know get this error instead:

Fatal error: Call to undefined function imagettftext() in /home/shanessa/public_html/php/show2.php on line 25

 

Line 25 being:

 imagettftext($im, 8, 0, 10, 20, $white, $font, $text);

 

Thanks for all your help so far!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.