PHP imagecreatefromjpeg работает, так почему же не работает png/bmp/gif?

Хочу загружать и изменять размеры картинок с разными расширениями. PHP обрезает максимально возможный квадрат из центра исходного изображения, а затем сохраняет его в размере 360 * 360 пикселей.

Код отлично работает с файлами jpeg, но с gif, bmp и png я получаю поврежденный файл размером 33 байта.

Вот большая часть кода:

$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);
}

Итак, правильно обрабатываются только файлы jpeg, а файлы gif, png и bmp — нет. У меня нет идей...
Заранее спасибо!

5
задан Hubro 5 April 2012 в 15:54
поделиться