Using GD it outputs text instead of an image

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joshlunsford
Forum Newbie
Posts: 4
Joined: Wed Sep 20, 2006 10:10 pm

Using GD it outputs text instead of an image

Post by joshlunsford »

It works if the code is in it's own file but if it is called from within phpdesk it shows as text. It looks like this

‰PNG IHDRv .÷PLTE3fÿÌf?Yf¿¥fŸ’f_lf߸ffFfî°ûCVIDATxœí‘MOƒ@†‡R>Ž
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

wrong/missing headers? mixing image data with text? can be many things but we will know for sure if you show us the code...
bureaula
Forum Newbie
Posts: 1
Joined: Thu Sep 21, 2006 2:33 am

problem fixed

Post by bureaula »

I had the very same problem, output was only text no image. Turned out that there was a space or tab between "<?php" and "header("Content-type: image/png");".
Normally php isn't sensitive to spaces or line breaks, so it was quite a hassle to figure this out...
joshlunsford
Forum Newbie
Posts: 4
Joined: Wed Sep 20, 2006 10:10 pm

Sorry heres the code

Post by joshlunsford »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

function createbutton($fontsize, $text)
{
header("Content-type: image/png");
$imagewidth = 118;
$imageheight = 30;
//$fontsize = "11";
$fontangle = "0";
$font = "comicbd.ttf";
//$text = "Create Message";
$backgroundcolor = "003366";
$textcolor = "FFCC66";
// Convert HTML backgound color to RGB
if( eregi( "([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})", $backgroundcolor, $bgrgb ) )
{$bgred = hexdec( $bgrgb[1] );  $bggreen = hexdec( $bgrgb[2] );  $bgblue = hexdec( $bgrgb[3] );}

// Convert HTML text color to RGB
if( eregi( "([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})", $textcolor, $textrgb ) )
{$textred = hexdec( $textrgb[1] );  $textgreen = hexdec( $textrgb[2] );  $textblue = hexdec( $textrgb[3] );}

// Create image
$im = imagecreate( $imagewidth, $imageheight );

// Declare image's background color
$bgcolor = imagecolorallocate($im, $bgred,$bggreen,$bgblue);

// Declare image's text color
$fontcolor = imagecolorallocate($im, $textred,$textgreen,$textblue);

// Get exact dimensions of text string
$box = @imageTTFBbox($fontsize,$fontangle,$font,$text);

// Get width of text from dimensions
$textwidth = abs($box[4] - $box[0]);

// Get height of text from dimensions
$textheight = abs($box[5] - $box[1]);

// Get x-coordinate of centered text horizontally using length of the image and length of the text
$xcord = ($imagewidth/2)-($textwidth/2)-2;

// Get y-coordinate of centered text vertically using height of the image and height of the text
$ycord = ($imageheight/2)+($textheight/2);

// Declare completed image with colors, font, text, and text location
imagettftext ( $im, $fontsize, $fontangle, $xcord, $ycord, $fontcolor, $font, $text );

// Display completed image as PNG
imagepng($im);

// Close the image
imagedestroy($im);
}

And then I call it with

Code: Select all

<?
createbutton("11", "hello");
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Where, by chance, are you calling createbutton()? Is it the only thing in the page request? Have you checked your error logs?
joshlunsford
Forum Newbie
Posts: 4
Joined: Wed Sep 20, 2006 10:10 pm

Post by joshlunsford »

From the member_header.php file of exodesk
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

you can't have your script output text & image all in one go.. that messes things up. you need to put that code in a different file and call that file separately.
joshlunsford
Forum Newbie
Posts: 4
Joined: Wed Sep 20, 2006 10:10 pm

Problem solved

Post by joshlunsford »

Added the code to a file and then called it with a standard HTML image code



Thank you for your help
Post Reply