Using GD it outputs text instead of an image
Moderator: General Moderators
-
joshlunsford
- Forum Newbie
- Posts: 4
- Joined: Wed Sep 20, 2006 10:10 pm
Using GD it outputs text instead of an image
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>Ž
‰PNG IHDRv .÷PLTE3fÿÌf?Yf¿¥fŸ’f_lf߸ffFfî°ûCVIDATxœí‘MOƒ@†‡R>Ž
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
problem fixed
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...
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
feyd | Please use
And then I call it with
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]-
joshlunsford
- Forum Newbie
- Posts: 4
- Joined: Wed Sep 20, 2006 10:10 pm
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
-
joshlunsford
- Forum Newbie
- Posts: 4
- Joined: Wed Sep 20, 2006 10:10 pm
Problem solved
Added the code to a file and then called it with a standard HTML image code
Thank you for your help
Thank you for your help