PDA

View Full Version : Trouble with header function using php gd library



shanehaw
18 Jan 2011, 01:57 AM
Hi,

I'm pretty new to php script writing and I have a funny feeling this is something quite simple but, I have a problem when I try to create an image. It's just a simple script to get a file from the server and resize it. I got most of the code from another website. Here is the code:


<html>
<body>
<?php
// Create source image and dimension.
$src_img = imagecreatefrompng('mouse.png');
$srcsize = getimagesize('mouse.png');
$dest_x = 200;
$dest_y = (200 / $srcsize[0]) * $srcsize[1];
$dst_img = imagecreatetruecolor($dest_x, $dest_y);

// Resize image
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dest_x, $dest_y, $srcsize[0], $srcsize[1]);

// Output image
header("content-type: image/png");
imagepng($dst_img);

// Destroy images
imagedestroy($src_img);
imagedestroy($dst_img);
?>
</body>
</html>


In firefox it says that the image could not be displayed because it contains errors. If I comment out the header it displays the raw file (A bunch of meaningless characters). I'm running WAMPServer Version 2.1, and when I run phpinfo() it says that everything to do with gd is enabled. Any thoughts?

Thanks for your help!

shanehaw
18 Jan 2011, 04:11 AM
I found the problem. The text editor that I was using for my php script had inserted some symbols before the <?php of the script and so it was corrupting the image. It's working nicely now. Thanks anyway.