简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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