创建缩略图时背景变黑

时间:2018-07-30 05:00:14

标签: php thumbnails

我具有以下脚本以便创建缩略图,效果很好!但是,当我上传具有透明背景的PNG文件时,由于某种原因背景会变为黑色。

如何解决?我该怎么办?

这是我的代码

<?php
function cwUpload($field_name = '', $target_folder = '', $file_name = '', $thumb = FALSE, $thumb_folder = '', $thumb_width = '', $thumb_height = ''){

    //folder path setup
    $target_path = $target_folder;
    $thumb_path = $thumb_folder;

    //file name setup
    $filename_err = explode(".",$_FILES[$field_name]['name']);
    $filename_err_count = count($filename_err);
    $file_ext = $filename_err[$filename_err_count-1];
    if($file_name != ''){
        $fileName = $file_name.'.'.$file_ext;
    }else{
        $fileName = $_FILES[$field_name]['name'];
    }

    //upload image path
    $upload_image = $target_path.basename($fileName);

    //upload image
    if(move_uploaded_file($_FILES[$field_name]['tmp_name'],$upload_image))
    {
        //thumbnail creation
        if($thumb == TRUE)
        {
            $thumbnail = $thumb_path.$fileName;
            list($width,$height) = getimagesize($upload_image);
            $thumb_create = imagecreatetruecolor($thumb_width,$thumb_height);
            switch($file_ext){
                case 'jpg':
                    $source = imagecreatefromjpeg($upload_image);
                    break;
                case 'jpeg':
                    $source = imagecreatefromjpeg($upload_image);
                    break;

                case 'png':
                    $source = imagecreatefrompng($upload_image);
                    break;
                case 'gif':
                    $source = imagecreatefromgif($upload_image);
                    break;
                default:
                    $source = imagecreatefromjpeg($upload_image);
            }

            imagecopyresized($thumb_create,$source,0,0,0,0,$thumb_width,$thumb_height,$width,$height);

            switch($file_ext){
                case 'jpg' || 'jpeg':
                    imagejpeg($thumb_create,$thumbnail,100);
                    break;
                case 'png':
                    imagepng($thumb_create,$thumbnail,100);
                    break;

                case 'gif':
                    imagegif($thumb_create,$thumbnail,100);
                    break;
                default:
                    imagejpeg($thumb_create,$thumbnail,100);
            }

        }

        return $fileName;
    }
    else
    {
        return false;
    }
}
if(!empty($_FILES['image']['name'])){

    //call thumbnail creation function and store thumbnail name
    $upload_img = cwUpload('image','uploads/','',TRUE,'uploads/thumbs/','200','160');

    //full path of the thumbnail image
    $thumb_src = 'uploads/thumbs/'.$upload_img;

    //set success and error messages
    $message = $upload_img?"<span style='color:#008000;'>Image thumbnail have been created successfully.</span>":"<span style='color:#F00000;'>Some error occurred, please try again.</span>";

}else{

    //if form is not submitted, below variable should be blank
    $thumb_src = '';
    $message = '';
}

?>

<form method="post" enctype="multipart/form-data">
    <input type="file" name="image"/>
    <input type="submit" name="submit" value="Upload"/>
</form>

<?php if($thumb_src != ''){ ?>
<img src="<?php echo $thumb_src; ?>" alt="">
<?php } ?>

在上面的代码中以JPG和其他扩展名工作,但是当我上传透明图像时,背景变黑了

enter image description here

我做错了什么?

帮帮我

谢谢。

1 个答案:

答案 0 :(得分:1)

尝试使用此PNG代码

<?php
function cwUpload($field_name = '', $target_folder = '', $file_name = '', $thumb = FALSE, $thumb_folder = '', $thumb_width = '', $thumb_height = ''){

    //folder path setup
    $target_path = $target_folder;
    $thumb_path = $thumb_folder;

    //file name setup
    $filename_err = explode(".",$_FILES[$field_name]['name']);
    $filename_err_count = count($filename_err);
    $file_ext = $filename_err[$filename_err_count-1];
    if($file_name != ''){
        $fileName = $file_name.'.'.$file_ext;
    }else{
        $fileName = $_FILES[$field_name]['name'];
    }

    //upload image path
    $upload_image = $target_path.basename($fileName);

    //upload image
    if(move_uploaded_file($_FILES[$field_name]['tmp_name'],$upload_image))
    {
        //thumbnail creation
        if($thumb == TRUE)
        {
            $thumbnail = $thumb_path.$fileName;
            list($width,$height) = getimagesize($upload_image);

            $thumb_create = imagecreatetruecolor($thumb_width,$thumb_height);
            imagealphablending($thumb_create, false);
            imagesavealpha($thumb_create, true); 
            $trans_layer_overlay = imagecolorallocatealpha($thumb_create, 220, 220, 220, 127);
            imagefill($thumb_create, 0, 0, $trans_layer_overlay);
            switch($file_ext){
                case 'png':
                    $source = imagecreatefrompng($upload_image);
                    break;
            }

            imagecopyresized($thumb_create,$source,0,0,0,0,$thumb_width,$thumb_height,$width,$height);

            switch($file_ext){
                case 'png':
                    imagepng($thumb_create,$thumbnail);
                    break;
            }

        }

        return $fileName;
    }
    else
    {
        return false;
    }
}
if(!empty($_FILES['image']['name'])){

    //call thumbnail creation function and store thumbnail name
    $upload_img = cwUpload('image','uploads/','',TRUE,'uploads/thumbs/','200','160');

    //full path of the thumbnail image
    $thumb_src = 'uploads/thumbs/'.$upload_img;

    //set success and error messages
    $message = $upload_img?"<span style='color:#008000;'>Image thumbnail have been created successfully.</span>":"<span style='color:#F00000;'>Some error occurred, please try again.</span>";

}else{

    //if form is not submitted, below variable should be blank
    $thumb_src = '';
    $message = '';
}

?>

<form method="post" enctype="multipart/form-data">
    <input type="file" name="image"/>
    <input type="submit" name="submit" value="Upload"/>
</form>

<?php if($thumb_src != ''){ ?>
<img src="<?php echo $thumb_src; ?>" alt="">
<?php } ?>

当您上传透明图像时,它将起作用。

相关问题