无法保持上传脚本的透明度

时间:2015-08-06 03:03:07

标签: php image image-uploading uploading

所以我不确定自己做错了什么,我以为自己完成了这个工作,直到我上传了一个透明背景的PNG,然后用黑色背景保存了。任何人都可以指出我做错了什么吗?这是我的图像类和上传代码。我认为' imagecreatefrompng'什么会保持透明度,但也许我错了或做错了?

图像类

    <?php
ini_set("memory_limit","50M");

function normal_resize_image($source, $destination, $image_type, $max_size, $image_width, $image_height, $quality){

    if($image_width <= 0 || $image_height <= 0){return false;} //return false if nothing to resize

    //do not resize if image is smaller than max size
    if($image_width <= $max_size && $image_height <= $max_size){
        if(save_image($source, $destination, $image_type, $quality)){
            return true;
        }
    }

    //Construct a proportional size of new image
    $image_scale    = min($max_size/$image_width, $max_size/$image_height);
    $new_width      = ceil($image_scale * $image_width);
    $new_height     = ceil($image_scale * $image_height);

    $new_canvas     = imagecreatetruecolor( $new_width, $new_height ); //Create a new true color image
    imagealphablending($new_canvas,true);

    //Copy and resize part of an image with resampling
    if(imagecopyresampled($new_canvas, $source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height)){
        save_image($new_canvas, $destination, $image_type, $quality); //save resized image
    }

    return true;
}

function thumb_resize_image($source, $destination, $image_type, $max_size, $image_width, $image_height, $quality){

    if($image_width <= 0 || $image_height <= 0){return false;} //return false if nothing to resize

    //do not resize if image is smaller than max size
    if($image_width <= $max_size && $image_height <= $max_size){
        if(save_image($source, $destination, $image_type, $quality)){
            return true;
        }
    }

    //Construct a proportional size of new image
    $image_scale    = min($max_size/$image_width, $max_size/$image_height);
    $new_width      = ceil($image_scale * $image_width);
    $new_height     = ceil($image_scale * $image_height);

    $new_canvas     = imagecreatetruecolor( $new_width, $new_height ); //Create a new true color image
    imagealphablending($new_canvas,true);

    //Copy and resize part of an image with resampling
    if(imagecopyresampled($new_canvas, $source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height)){
        save_image($new_canvas, $destination, $image_type, $quality); //save resized image
    }

    return true;
}

##### This function corps image to create exact square, no matter what its original size! ######
function crop_image_square($source, $destination, $image_type, $square_size, $image_width, $image_height, $quality){
    if($image_width <= 0 || $image_height <= 0){return false;} //return false if nothing to resize

    if( $image_width > $image_height )
    {
        $y_offset = 0;
        $x_offset = ($image_width - $image_height) / 2;
        $s_size     = $image_width - ($x_offset * 2);
    }else{
        $x_offset = 0;
        $y_offset = ($image_height - $image_width) / 2;
        $s_size = $image_height - ($y_offset * 2);
    }
    $new_canvas = imagecreatetruecolor( $square_size, $square_size); //Create a new true color image
    imagealphablending($new_canvas,true);

    //Copy and resize part of an image with resampling
    if(imagecopyresampled($new_canvas, $source, 0, 0, $x_offset, $y_offset, $square_size, $square_size, $s_size, $s_size)){
        save_image($new_canvas, $destination, $image_type, $quality);
    }

    return true;
}

##### Saves image resource to file ##### 
function save_image($source, $destination, $image_type, $quality){
    switch(strtolower($image_type)){//determine mime type
        case 'image/png': 
            imagepng($source, $destination); return true; //save png file
            break;
        case 'image/gif': 
            imagegif($source, $destination); return true; //save gif file
            break;          
        case 'image/jpeg': case 'image/pjpeg': 
            imagejpeg($source, $destination, $quality); return true; //save jpeg file
            break;
        default: return false;
    }
}
?>

以下是要上传的代码。

if(isset($_POST['addImages'])) {    
        $valid_formats = array("jpg", "jpeg", "png", "gif", "bmp", "JPG", "JPEG", "PNG", "GIF", "BMP");
        $max_file_size = 50971520; //50mb
        $path = "images/media/"; // Upload directory
        $count = 0;

        $thumb_square_size = 300; //Thumbnails will be cropped 
        $max_image_size = 1200; //Maximum image size (height and width)
        $thumb_prefix = "_thumb"; //Normal thumb Prefix
        $destination_folder = 'images/media/'; //upload directory ends with / (slash)
        $jpeg_quality = 90; //jpeg quality

        // Loop $_FILES to execute all files
        foreach ($_FILES['files']['name'] as $foto => $name) {
            if ($_FILES['files']['error'][$foto] == 4) {
                continue; // Skip file if any error found
            }          
            if ($_FILES['files']['error'][$foto] == 0) {          
                if ($_FILES['files']['size'][$foto] > $max_file_size) {
                    $message[] = "$name is too large!.";
                    continue; // Skip large files
                }
                elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
                    $message[] = "$name is not a valid format";
                    continue; // Skip invalid file formats
                }
                else { // No error found! 
                    $image_name = $name; //file name
                    $image_size = $_FILES['files']['size'][$foto]; //file size
                    $image_temp = $_FILES['files']['tmp_name'][$foto]; //file temp
                    $image_size_info = getimagesize($image_temp); //get image size

                    if($image_size_info){
                        $image_width = $image_size_info[0]; //image width
                        $image_height = $image_size_info[1]; //image height
                        $image_type = $image_size_info['mime']; //image type
                    } else {
                        die("Make sure image file is valid!");
                    }

                //switch statement below checks allowed image type as well as creates new image from given file.
                switch($image_type){
                    case 'image/png':
                        $image_res = imagecreatefrompng($image_temp);
                        imageAlphaBlending($image_res, true);
                        imageSaveAlpha($image_res, true);
                        break;
                    case 'image/gif':
                        $image_res = imagecreatefromgif($image_temp); break;            
                    case 'image/jpeg': case 'image/pjpeg':
                        $image_res = imagecreatefromjpeg($image_temp); break;
                    default:
                        $image_res = false;
                }

                    if($image_res){
                        //Get file extension and name to construct new file name 
                        $image_info = pathinfo($image_name);
                        $image_extension = strtolower($image_info["extension"]); //image extension
                        $image_name_only = strtolower($image_info["filename"]);//file name only, no extension
                        $dateRec = date('d-M-Y-h-i-s');

                        //create a random name for new image (Eg: fileName_293749.jpg) ;
                        $new_file_name = $dateRec. '_' .  rand(0, 9999999999);
                        //folder path to save resized images and thumbnails
                        $thumb_save_folder  = $destination_folder . $new_file_name . $thumb_prefix . '.' . $image_extension ; 
                        $image_save_folder  = $destination_folder . $new_file_name. '.' . $image_extension;

                        //call normal_resize_image() function to proportionally resize image
                        if(normal_resize_image($image_res, $image_save_folder, $image_type, $max_image_size, $image_width, $image_height, $jpeg_quality)) {
                            //call crop_image_square() function to create square thumbnails
                            if(!thumb_resize_image($image_res, $thumb_save_folder, $image_type, $thumb_square_size, $image_width, $image_height, $jpeg_quality)) { 
                                die('Error Creating thumbnail'); 
                            }

                        }
                    }

                    $post_img = $new_file_name.'.'.$image_extension;
                    $post_img_thumb = $new_file_name.$thumb_prefix.'.'.$image_extension;
                    $stmt = $mysqli->prepare('INSERT INTO media_images(image, image_thumb, members_id) VALUES (?,?,?)');
                    $stmt->bind_param('ssi', $post_img, $post_img_thumb, $members_id);
                    $stmt->execute();
                    $stmt->close();

                    $count++;
                }
            }
        }
        $MEMUS = memory_get_usage();
        ob_start();
        header("Location: media.php");
    }

3 个答案:

答案 0 :(得分:1)

You need to enable alpha and save the setting

    case 'image/png':
        $image_res =  imagecreatefrompng($image_temp);
        imageAlphaBlending($image_res, true);
        imageSaveAlpha($image_res, true);
        break;

答案 1 :(得分:1)

您的问题已通过以下解决。

  function resizeImage($source, $destination, $image_type, $max_size, $image_width, $image_height, $quality) {
                //RETURN FALSE IF NOTHING TO RESIZE
                if($image_width <= 0 || $image_height <= 0){return false;}

                //CHECK IF SMALLER THAN THE MAX SIZES
                if($image_width <= $max_size && $image_height <= $max_size) { //IF SMALLER THAN MAX SIZES
                    $new_width = $image_width;
                    $new_height = $image_height;
                } else { //ELSE CONSTRUCT PROPORTIONAL SIZE OF NEW IMAGE
                    $image_scale = min($max_size/$image_width, $max_size/$image_height);
                    $new_width = ceil($image_scale * $image_width);
                    $new_height = ceil($image_scale * $image_height);
                }

                $new_canvas = imagecreatetruecolor($new_width, $new_height);
                $black = imagecolorallocate($new_canvas, 0, 0, 0);
                imagecolortransparent($new_canvas, $black);
                imagealphablending($new_canvas,false);
                imagesavealpha($new_canvas, true);

                if(imagecopyresampled($new_canvas, $source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height)){
                    $this->save_image($new_canvas, $destination, $image_type, $quality);
                }
                return true;
            }
//Resize Image Normal
        function normal_resize_image($source, $destination, $image_type, $max_size, $image_width, $image_height, $quality) {
            //RETURN FALSE IF NOTHING TO RESIZE
            if($image_width <= 0 || $image_height <= 0){return false;}

            //CHECK IF SMALLER THAN THE MAX SIZES
            if($image_width <= $max_size && $image_height <= $max_size) { //IF SMALLER THAN MAX SIZES
                $new_width = $image_width;
                $new_height = $image_height;
            } else { //ELSE CONSTRUCT PROPORTIONAL SIZE OF NEW IMAGE
                $image_scale = min($max_size/$image_width, $max_size/$image_height);
                $new_width = ceil($image_scale * $image_width);
                $new_height = ceil($image_scale * $image_height);
            }

            $new_canvas = imagecreatetruecolor($new_width, $new_height);
            $black = imagecolorallocate($new_canvas, 0, 0, 0);
            imagecolortransparent($new_canvas, $black);
            imagealphablending($new_canvas,false);
            imagesavealpha($new_canvas, true);

            if(imagecopyresampled($new_canvas, $source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height)){
                $this->save_image($new_canvas, $destination, $image_type, $quality);
            }
            return true;
        }

让我知道是否可行。可能需要一些调整。

答案 2 :(得分:0)

你必须使用imagealphablending()。将function saveSettings(){ var count = 1; //count for db var checked=[]; //array of checked values var inc = 0; //only incremented when checkbox is checked var dataString = "x"; for(var i=0; i<=2; i++) { //loads checked array with checked values if(document.getElementById("check"+i).checked == true){ checked[inc] = document.getElementById("check"+i).value; alert(checked[inc]) // <------ executing as expected inc++; } } alert("made it") // <------ not executing if(checked.length>0){ // loads checked values into dataString for(var i=0; i == checked.length; i++){ if(i == 0){ dataString = "co_" + count +"="+checked[i] } else { dataString = dataString +"&co_" + count +"="+checked[i] } count++; } } alert(dataString) 放在imagealphablending($new_canvas,true);命令之后。

您也需要在创建图像或图层时使用imagealphablending。因此,在您的上传中,您将在imagecreatefromX开关之后以及在您的imagecopyresampled调用之后使用它。