GD library problem

Hi there guys!
Lately I’ve been trying to use GD to generate dynamic images. My problem its that the code must be neccesarily before the <html> tag (if not an error like this appear in the browser:

‰PNG IHDRæ£þÓêPLTE ér¿ŽLÚIDATxœåбAàŸMhF®!ža’+(„WÙË&ª;‘xë”Z…‡Ði'J&shy;D£RÑ+®0wÁ¯«ÙýòÏfø³Sc CYeŸGEý˜õà«*T,(´ªšðñ£Peí|&H»;?&¦±éÖ³½54IÇ%¥öÉé¿×‘[¢•!‘i¡)1kg$;ÇP E‚˜o™¶²,{»—z÷·†Âå:Ž¾´¹Ì”¨Ž,³fSñƒ8Ÿ¨Æ7Ȑ½íEµ¯E´ª,ÒNçÛ@µ4&‚øg••Gé2ü¶'€£D?ijžIEND®B‚ )


The original code:


<?php
$new = ImageCreate (230, 20) or die ("Cannot Create image"); 
$back_color = ImageColorAllocate ($new, 0, 10, 10); 
$txt_color = ImageColorAllocate ($new, 233, 114, 191); 
ImageString ($new, 31, 5, 5,  "GD library", $txt_color); 
ImagePng ($new); 
?> 


When do it ‘right’ all the HTML its not considered, and the browser just shows the image. Its this normal in GD? Is there something wrong with my PHP or GD (PHP 5.1.2 - GD 2.0.28)?

I even try putting the image in a separate file and include() it. Same story happen, a lot of understanding code appear.

My final objective its to create thumb images from a gallery (resizeing and creating new pics from the original images so the browser doesn’t have to load the original big sized images).

Well…any idea will be highly appreciated.

If you’re just making an image to display in the browser I believe you’ll need to add the header info before you start with the image (really before the imagepng() function).

<?php
header( "Content-type: image/png" );

$new = ImageCreate (230, 20) or die ("Cannot Create image");
$back_color = ImageColorAllocate ($new, 0, 10, 10);
$txt_color = ImageColorAllocate ($new, 233, 114, 191);
ImageString ($new, 31, 5, 5,  "GD library", $txt_color);
ImagePng ($new);

imagedestroy($new); // Don't forget this either
?>

If you want to create the image and then use it in an html page, something like so may be needed:

<?php

$new = ImageCreate (230, 20) or die ("Cannot Create image");
$back_color = ImageColorAllocate ($new, 0, 10, 10);
$txt_color = ImageColorAllocate ($new, 233, 114, 191);
ImageString ($new, 31, 5, 5,  "GD library", $txt_color);

ImagePng ($new, "img.png"); // Here you're actually creating the image and saving it to a file named "img.png" to be used later

imagedestroy($new);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
 <head>
  <title>PNG Image Test</title>
 </head>
 <body>
  <img src="img.png" height="20" width="230" alt="PNG Test" />
 </body>
</html>

Alright, that works! Thank you, I belive that I begin to undestand how GD works…