5

I want to upload and resize pictures with different extensions. The php crops the biggest possible square from the center of the original pic, then saves it in 360*360 pixels.

The code works fine with jpeg files, but with gif, bmp and png I get a corrupted file with 33 Byte size.

Here's most of the code:

$file_temp = $_FILES["pic"]["tmp_name"];
list ($width, $height, $type) = getimagesize ($file_temp);

$picture_name = "... a name.ext ...";
$upload = "... some dir/$picture_name";



if (move_uploaded_file($file_temp, "$upload"))
{



    //switches content-type and calls the imagecreatefrom... function
    if ($type == 1)
    {
        header ('Content-Type: image/gif');
        $image = imagecreatefromgif($upload);
    }
    elseif ($type == 2)
    {
        header ('Content-Type: image/jpeg');
        $image = imagecreatefromjpeg($upload);
    }
    elseif ($type == 3)
    {
        header ('Content-Type: image/png');
        $image = imagecreatefrompng($upload);
    }
    else
    {
        header ('Content-Type: image/x-ms-bmp');
        $image = imagecreatefromwbmp($upload);
    }


    $image_p = imagecreatetruecolor(360, 360);




    //this code below should preserve transparency but I couldn't try it out for now...

    if($type==1 or $type==3)
    {
        imagecolortransparent($image_p, imagecolorallocatealpha($image_p, 0, 0, 0, 127));
        imagealphablending($image_p, true);
        imagesavealpha($image_p, true);
    }





    //this part is for cropping
    $x=0;
    $y=0;

    if ($width > $height)
    {
        $x= ($width - $height)/2;
        $width = $height;
    }
    else
    {
        $y = ($height - $width)/2;
        $height = $width;
    }





    imagecopyresampled ($image_p, $image, 0, 0, $x, $y, 360, 360, $width, $height);
    if ($type == 1)
        imagegif ($image_p, $upload, 80);
    elseif ($type == 2)
        imagejpeg ($image_p, $upload, 80);
    elseif ($type == 3)
        imagepng ($image_p, $upload, 80);
    else
        imagewbmp ($image_p, $upload, 80);
}

So, only jpeg files are processed correctly, gif, png and bmp files aren't. I'm out of ideas...
Thanks in advance!

Hubro
  • 56,214
  • 69
  • 228
  • 381
ezattilabatyi
  • 357
  • 1
  • 2
  • 11
  • How did you populate `$type`? Seems like the likely answer is that it is always `2`... – DaveRandom Apr 05 '12 at 15:57
  • @DaveRandom getimagesize() gets the width, height, type (0-16) and an attribute if I'm not wrong. the type is not always 2, it changes how it should. – ezattilabatyi Apr 05 '12 at 16:41

2 Answers2

4

Your PHP may not be compiled with support for those formats. Run a phpinfo() and inspect the output for something like this:

GD Support => enabled
GD Version => bundled (2.0.34 compatible)
GIF Read Support => enabled
GIF Create Support => enabled
PNG Support => enabled
libPNG Version => 1.2.10
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • Thanks for the help! There was a warning, which I couldn't see because after the processing I was redirecting the page back to the upload form. Turns out I don't have libPNG and didn't know these functions are not part of my PHP libraries. Now I'll download it and try if my code works. – ezattilabatyi Apr 05 '12 at 17:13
1

Since you seem to be getting a corrupt 33 byte image file every time for images other then jpg, this could be a PHP error that is getting written to the file (since it seems like your PHP file is displaying the image contents directly, from your Content Type header). Have you tried opening the file in a text editor and seeing its contents? If its garbeled junk, then its an error, but if its a PHP warning, your version of PHP probably doesn't have support for those extended image types. That, or there could be a warning in your code somewhere.

Jeff Geisperger
  • 583
  • 4
  • 17
  • In the text editor, the first words in jpeg files were CREATOR: gd-jpeg v1.0 (using IJG JPEG v62) in png, gif, and bmp there was the extension like PNG or GIF, and a few random characters – ezattilabatyi Apr 05 '12 at 16:36
  • There was a warning, which I couldn't see because after the processing I was redirecting the page back to the upload form. Turns out I don't have libPNG and didn't know these functions are not part of my PHP libraries. Now I'll download it and try if my code works. Thanks for the help! – ezattilabatyi Apr 05 '12 at 17:15
  • I'm getting the same 33 byte png files. In a text editor I see: "PNG Image: ‘file.png’ pixelWidth: pixelHeight: " phpinfo shows that I have everything enabled properly. Any ideas? – Guy Oct 31 '13 at 17:50
  • Just solved it: imagepng doesn't accept 80 as the compression, needs to be 0-9 – Guy Oct 31 '13 at 17:53