如何使用PHP调整png和gif图像的大小并存储透明背景?

时间:2015-07-04 16:44:22

标签: php image png gif image-resizing

如何使用php调整pnggif图像的大小并存储透明背景?

这是我的代码

将图片大小调整为200x200 px并上传到PIE目录。这很好用。 但是当与pnggif图像(透明bg)一起使用时。这将是bg变为黑色。如何在调整大小图像后写入透明的bg?

<form name="setup" method="post" ENCTYPE = "multipart/form-data" onsubmit="return checkform(this);">
<input type="file" name="offer_image_1" id="offer_image_1">
<input type="submit" name="submit" value="Next">
</form>

<?php
include("connect.php");
if(isset($_POST["submit"]))
{

#####  This function will proportionally resize image ##### 
function normal_resize_image($source_thumbnail, $destination_thumbnail, $image_type_thumbnail, $max_size_thumbnail, $image_width_thumbnail, $image_height_thumbnail, $quality_thumbnail){

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

    //do not resize if image is smaller than max size
    if($image_width_thumbnail <= $max_size_thumbnail && $image_height_thumbnail <= $max_size_thumbnail){
        if(save_image($source_thumbnail, $destination_thumbnail, $image_type_thumbnail, $quality_thumbnail)){
            return true;
        }
    }

    //Construct a proportional size of new image
    $image_scale_thumbnail = min($max_size_thumbnail/$image_width_thumbnail, $max_size_thumbnail/$image_height_thumbnail);
    $new_width_thumbnail = ceil($image_scale_thumbnail * $image_width_thumbnail);
    $new_height_thumbnail = ceil($image_scale_thumbnail * $image_height_thumbnail);

    $new_canvas_thumbnail = imagecreatetruecolor( $new_width_thumbnail, $new_height_thumbnail ); //Create a new true color image

    //Copy and resize part of an image with resampling
    if(imagecopyresampled($new_canvas_thumbnail, $source_thumbnail, 0, 0, 0, 0, $new_width_thumbnail, $new_height_thumbnail, $image_width_thumbnail, $image_height_thumbnail)){
        save_image($new_canvas_thumbnail, $destination_thumbnail, $image_type_thumbnail, $quality_thumbnail); //save resized image
    }

    return true;
}

##### This function corps image to create exact square, no matter what its original size! ######
function crop_image_square($source_thumbnail, $destination_thumbnail, $image_type_thumbnail, $square_size_thumbnail, $image_width_thumbnail, $image_height_thumbnail, $quality_thumbnail){
    if($image_width_thumbnail <= 0 || $image_height_thumbnail <= 0){return false;} //return false if nothing to resize

    if( $image_width_thumbnail > $image_height_thumbnail )
    {
        $y_offset_thumbnail = 0;
        $x_offset_thumbnail = ($image_width_thumbnail - $image_height_thumbnail) / 2;
        $s_size_thumbnail   = $image_width_thumbnail - ($x_offset_thumbnail * 2);
    }else{
        $x_offset_thumbnail = 0;
        $y_offset_thumbnail = ($image_height_thumbnail - $image_width_thumbnail) / 2;
        $s_size_thumbnail = $image_height_thumbnail - ($y_offset_thumbnail * 2);
    }
    $new_canvas_thumbnail = imagecreatetruecolor( $square_size_thumbnail, $square_size_thumbnail); //Create a new true color image

    //Copy and resize part of an image with resampling
    if(imagecopyresampled($new_canvas_thumbnail, $source_thumbnail, 0, 0, $x_offset_thumbnail, $y_offset_thumbnail, $square_size_thumbnail, $square_size_thumbnail, $s_size_thumbnail, $s_size_thumbnail)){
        save_image($new_canvas_thumbnail, $destination_thumbnail, $image_type_thumbnail, $quality_thumbnail);
    }

    return true;
}

##### Saves image resource to file ##### 
function save_image($source_thumbnail, $destination_thumbnail, $image_type_thumbnail, $quality_thumbnail){
    switch(strtolower($image_type_thumbnail)){//determine mime type
        case 'image/png': 
            imagepng($source_thumbnail, $destination_thumbnail); return true; //save png file
            break;
        case 'image/gif': 
            imagegif($source_thumbnail, $destination_thumbnail); return true; //save gif file
            break;          
        case 'image/jpeg': case 'image/pjpeg': 
            imagejpeg($source_thumbnail, $destination_thumbnail, $quality_thumbnail); return true; //save jpeg file
            break;
        default: return false;
    }
}




        $thumb_square_size_thumbnail    = 200; //Thumbnails will be cropped to 200x200 pixels
        $max_image_size_thumbnail       = 5000; //Maximum image size (height and width)
        $thumb_prefix_thumbnail     = "img_"; //Normal thumb Prefix
        $destination_folder_thumbnail   = 'PIE/'; //upload directory ends with / (slash)
        $jpeg_quality_thumbnail     = 100; //jpeg quality


        // Start ตรวจสอบ และ upload รูปภาพ //
        //uploaded file info we need to proceed
        $image_type_thumbnail_check = $_FILES['offer_image_1']['type']; //file type
        $image_name_thumbnail = $_FILES['offer_image_1']['name']; //file name
        $image_size_thumbnail = $_FILES['offer_image_1']['size']; //file size
        $image_temp_thumbnail = $_FILES['offer_image_1']['tmp_name']; //file temp
        $image_size_info_thumbnail = getimagesize($image_temp_thumbnail); //get image size



                                        if($image_size_info_thumbnail)
                                            {
                                                $image_width_thumbnail = $image_size_info_thumbnail[0]; //image width
                                                $image_height_thumbnail = $image_size_info_thumbnail[1]; //image height
                                                $image_type_thumbnail = $image_size_info_thumbnail['mime']; //image type





                                                //switch statement below checks allowed image type
                                                //as well as creates new image from given file
                                                switch($image_type_thumbnail)
                                                    {
                                                case 'image/png':
                                                        $image_res_thumbnail =  imagecreatefrompng($image_temp_thumbnail); break;
                                                        case 'image/gif':
                                            $image_res_thumbnail =  imagecreatefromgif($image_temp_thumbnail); break;
                                                        case 'image/jpeg': case 'image/pjpeg':
                                                        $image_res_thumbnail = imagecreatefromjpeg($image_temp_thumbnail); break;
                                                        default:
                                                        $image_res_thumbnail = false;
                                                    }
                                            if($image_res_thumbnail)
                                                    {
                                                //Get file extension and name to construct new file name 
                                                $image_info_thumbnail = pathinfo($image_name_thumbnail);
                                                $image_extension_thumbnail = strtolower($image_info_thumbnail["extension"]); //image extension
                                                $image_name_only_thumbnail = strtolower($image_info_thumbnail["filename"]);//file name only, no extension

                                                //create a random name for new image (Eg: fileName_293749.jpg);
                                                        $new_file_name_thumbnail = 'xxxxxxxxx.'.$image_extension_thumbnail;

                                                //folder path to save resized images and thumbnails
                                                $thumb_save_folder_thumbnail    = $destination_folder_thumbnail . $thumb_prefix_thumbnail . $new_file_name_thumbnail; 
                                                $image_save_folder_thumbnail    = $destination_folder_thumbnail . $new_file_name_thumbnail;




                                                //call normal_resize_image() function to proportionally resize image
if(crop_image_square($image_res_thumbnail, $thumb_save_folder_thumbnail, $image_type_thumbnail, $thumb_square_size_thumbnail, $image_width_thumbnail, $image_height_thumbnail, $jpeg_quality_thumbnail))
                                                            {
                                                                //echo "Success";
                                                            }

                                                imagedestroy($image_res_thumbnail); //freeup memory
                                                    }

                                                $image_path = $destination_folder_thumbnail.''.$thumb_prefix_thumbnail.''.$new_file_name_thumbnail;
                                            }


    }
?>

2 个答案:

答案 0 :(得分:0)

我用图书馆来处理图像。就像创建照片库,转换,调整大小和处理上传的图像一样,应用效果,添加标签,水印和反射以及其他图像编辑功能.... documentation

here

答案 1 :(得分:0)

您可以直接对图像使用html img标签,也可以通过javascript操作它的样式......

<div class="row">
    <div class="col-md-4">
        <h4 class="heading">Upload Patient's Photo :-</h4>
    </div>
    <div class="col-md-4">
        <input type="file" class="form-control" name="patientpic" id="patientpic" onchange="readURL(this)"/>
    </div>
</div>  
<script>
    function readURL(input) {
       if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function (e) 
        {
            $('#patientimg').attr('src', e.target.result);
        }
         reader.readAsDataURL(input.files[0]);
        }
        }
</script>                               
</div>
<div class="col-md-2 col-sm-12">
<div class="thumbnail">
  <img src="#" alt="..." class="img-responsive img-rounded" id="patientimg" style="height:200px;width:200px;background-color:transparent;">
</div>
</div>  

同样这也可以通过php在php string中使用这段代码来完成。