Page 1 of 1

Using GD it outputs text instead of an image

Posted: Wed Sep 20, 2006 10:15 pm
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>Ž

Posted: Wed Sep 20, 2006 10:21 pm
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...

problem fixed

Posted: Thu Sep 21, 2006 2:42 am
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...

Sorry heres the code

Posted: Thu Sep 21, 2006 7:41 pm
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]

Posted: Thu Sep 21, 2006 7:45 pm
by feyd
Where, by chance, are you calling createbutton()? Is it the only thing in the page request? Have you checked your error logs?

Posted: Thu Sep 21, 2006 7:57 pm
by joshlunsford
From the member_header.php file of exodesk

Posted: Fri Sep 22, 2006 2:20 am
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.

Problem solved

Posted: Sat Sep 23, 2006 7:44 am
by joshlunsford
Added the code to a file and then called it with a standard HTML image code



Thank you for your help