mercredi 25 mars 2015

PHP images: imagepng gives strange results

I am generating an image using the imagepng method in PHP using the following code. This solution Get imagejpeg to display properly provides insight into the header that must be set. Please see the code below, instead of displaying an image, image data is displayed in text form.


Code:



<?php

$img = imagecreatetruecolor(50, 50);
$new_pixel = imagecolorallocate($img, 255, 0, 0);
imagesetpixel($img, 25 , 25 , $new_pixel);
header('Content-Type: image/png');
imagepng($map_img);
imagedestroy($map_img);

?>


Browser Result (not exactly this but similar):



�PNG


IHDR1
KP�IDAT8���K
� Eѫ�������(�V�*��EF�@a�
k�tJ�.]���} hh9��&M.z޻4\J`����8�m@�k�П��g��⼺ӀrHw//���,
���zuom�H�� 7�=ʫ��I?�-/W��IEND�B`�


One solution suggests using the php file as an image source as such.



<img src="generate.php" />


But I find this highly impractical for my needs.


Thanks in advance!


Aucun commentaire:

Enregistrer un commentaire