0

Hey i was trying to learn the GD library and i can't get my code to work, it displays raw image text instead of the actual PNG i want to display. I have Png Support enabled in the GD section by the way.

Code is very simple:

<?php $picture = imagecreatefrompng("test.png"); imagepng($picture); ?>

but the result comes down to something like this:

�PNG IHDR���.�b pHYs���+ IDATx� etc.

Luka
  • 3
  • 1

1 Answers1

2

You have to tell the Browser, that it is image data.

Add

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

So it should be:

<?php
    header("Content-type:image/png");
    $picture = imagecreatefrompng("test.png");
    imagepng($picture);
?>

For more information and other image type you can look here PHP, display image with Header()

ivion
  • 545
  • 1
  • 8
  • 12
  • Thank you for the info! it's not displaying the raw text anymore but now i can't get the image to show, the whole browser page goes gray and there's a square in the middle. The filepath is correct i've checked many times. – Luka Dec 16 '18 at 20:28
  • @Luka you cant do this in a mixed media page, the page outputting the graphic should be separate, then you can call it like a regular image `` –  Dec 16 '18 at 20:31
  • Thank you so much, sorry for these silly questions i'm very new to PHP – Luka Dec 16 '18 at 20:38