Yennam Praveenkumar
Yennam Praveenkumar

Reputation: 167

background goes black while creating thumbnail

I have the following script in order to create a thumbnail, works fine! BUT, when I upload a PNG file with transparent background, for some reason the background change to black.

How to fix? What do I have to do?

This is my Code

<?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 } ?>

In above code working in JPG and other extension but when i upload transparent image background is getting black

enter image description here

What i am doing wrong ?

Help me

Thanks in advance.

Upvotes: 4

Views: 832

Answers (1)

Siddharth Ramani
Siddharth Ramani

Reputation: 684

Try this code for 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 } ?>

It will work when you upload transparent image.

Upvotes: 2

Related Questions