imagecopyresampled将图像转换为黑色

时间:2016-03-14 18:36:38

标签: php

我试图在上传后调整图像大小,但图像总是转换为黑色。虽然图像具有正确的尺寸,但它只显示黑色。我不确定我搞砸了哪里。我已经搜索过无用了。

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $full_path)){

        $orig_image = imagecreatefromjpeg($full_path);
        $image_info = getimagesize($full_path); 
        $width_orig  = $image_info[0]; // current width as found in image file
        $height_orig = $image_info[1]; // current height as found in image file

        if($width_orig > $height_orig){
            $width = 105;
            $ratio = $width/$width_orig;
            $height = $height_orig*$ratio;
        }else{
            $height = 360;
            $ratio = $height/$height_orig;
            $width = $width_orig*$ratio;
        }

        $destination_image = imagecreatetruecolor($width, $height);
        imagecopyresampled($destination_image, $orig_image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
        imagejpeg($destination_image, $full_path, 100);

}

1 个答案:

答案 0 :(得分:0)

检查功能imagecreatefromjpeg()之前的图像类型是否舒适。 像这样:

$image_type = exif_imagetype($full_path);
switch ($image_type)
    {
        case IMAGETYPE_GIF : $orig_image = imagecreatefromgif($file); break;
        case IMAGETYPE_JPEG : $orig_image = imagecreatefromjpeg($file);  break;
        case IMAGETYPE_PNG : $orig_image = imagecreatefrompng($file); break;
        //...
        default: echo 'Wrong image type';  break;
    }