简体   繁体   English

imagecopyresampled将图像转换为黑色

[英]imagecopyresampled converts image to black

I am trying to resize an image after uploading but the image is always converted to black. 我试图在上传后调整图像大小,但图像总是转换为黑色。 Though the image has the right dimension, it just shows black. 虽然图像具有正确的尺寸,但它只显示黑色。 I'm not sure where I messed up. 我不确定我搞砸了哪里。 I already searched SO to no avail. 我已经搜索过SO无济于事。

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

}

Check image type before function imagecreatefromjpeg() to be shure. 请先检查函数imagecreatefromjpeg()之前的图像类型,以确保正确。 Something like this: 像这样的东西:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM