# GD library problem

**URL:** <https://forum.kirupa.com/t/gd-library-problem/183383>\
**Category:** Uncategorized\
**Created:** [March 26, 2006, 3:30pm UTC](https://forum.kirupa.com/t/gd-library-problem/183383 "2006-03-26T15:30:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![The\_Stranger](https://avatars.discourse-cdn.com/v4/letter/t/a5b964/32.png) [@The\_Stranger](https://forum.kirupa.com/u/The_Stranger)\
**Post date:** [March 26, 2006, 3:30pm UTC](https://forum.kirupa.com/t/gd-library-problem/183383/1 "2006-03-26T15:30:01Z")

</div>

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

<?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.

---

<div class="post-metadata">

**Author:** ![Ankou](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ankou/32/1351_2.png) [@Ankou](https://forum.kirupa.com/u/Ankou)\
**Post date:** [March 26, 2006, 6:07pm UTC](https://forum.kirupa.com/t/gd-library-problem/183383/2 "2006-03-26T18:07:42Z")

</div>

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
<?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
<?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>

```

---

<div class="post-metadata">

**Author:** ![The\_Stranger](https://avatars.discourse-cdn.com/v4/letter/t/a5b964/32.png) [@The\_Stranger](https://forum.kirupa.com/u/The_Stranger)\
**Post date:** [March 27, 2006, 3:40pm UTC](https://forum.kirupa.com/t/gd-library-problem/183383/3 "2006-03-27T15:40:42Z")

</div>

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