简体   繁体   English

上载具有透明性的PNG图像时出现问题?

[英]Issue uploading PNG images with transparency?

I'm having some issues uploading PNG images with transparency. 我在上传具有透明性的PNG图像时遇到了一些问题。

I upload, crop and insert the images in the DB and when I try to show them in the website they come up black. 我将图像上传,裁剪并插入到数据库中,当我尝试在网站上显示它们时,它们会变黑。 It's only happening with PNG images, for JPG, JPEG and GIF it's working perfect. 只有PNG图像才发生这种情况,对于JPG,JPEG和GIF来说,它工作得非常完美。

Can somebody help me with that? 有人可以帮我吗?

Thanks in advance! 提前致谢!

Example

This is my code: 这是我的代码:

public function create_square_image($original_file, $destination_file=NULL, $square_size = 100){ 公共函数create_square_image($ original_file,$ destination_file = NULL,$ square_size = 100){

    // get width and height of original image
    $imagedata = getimagesize($original_file);
    $original_width = $imagedata[0];    
    $original_height = $imagedata[1];

    if($original_width > $original_height){
        $new_height = $square_size;
        $new_width = $new_height*($original_width/$original_height);
    }
    if($original_height > $original_width){
        $new_width = $square_size;
        $new_height = $new_width*($original_height/$original_width);
    }
    if($original_height == $original_width){
        $new_width = $square_size;
        $new_height = $square_size;
    }

    $new_width = round($new_width);
    $new_height = round($new_height);

    // load the image
    if(substr_count(strtolower($original_file), ".jpg") or substr_count(strtolower($original_file), ".jpeg")){
        $original_image = imagecreatefromjpeg($original_file);
    }
    if(substr_count(strtolower($original_file), ".gif")){
        $original_image = imagecreatefromgif($original_file);
    }
    if(substr_count(strtolower($original_file), ".png")){


                    $original_image = imagecreatefrompng($original_file);       
    }

    $smaller_image = imagecreatetruecolor($new_width, $new_height);
    $square_image = imagecreatetruecolor($square_size, $square_size);

    imagecopyresampled($smaller_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);

    if($new_width>$new_height){
        $difference = $new_width-$new_height;
        $half_difference =  round($difference/2);
        imagecopyresampled($square_image, $smaller_image, 0-$half_difference+1, 0, 0, 0, $square_size+$difference, $square_size, $new_width, $new_height);
    }
    if($new_height>$new_width){
        $difference = $new_height-$new_width;
        $half_difference =  round($difference/2);
        imagecopyresampled($square_image, $smaller_image, 0, 0-$half_difference+1, 0, 0, $square_size, $square_size+$difference, $new_width, $new_height);
    }
    if($new_height == $new_width){
        imagecopyresampled($square_image, $smaller_image, 0, 0, 0, 0, $square_size, $square_size, $new_width, $new_height);
    }


    // if no destination file was given then display a png      
    if(!$destination_file){
        imagepng($square_image,NULL,9);
    }

    // save the smaller image FILE if destination file given
    if(substr_count(strtolower($destination_file), ".jpg")){
        imagejpeg($square_image,$destination_file,100);
    }
    if(substr_count(strtolower($destination_file), ".gif")){
        imagegif($square_image,$destination_file);
    }
    if(substr_count(strtolower($destination_file), ".png")){
        imagepng($square_image,$destination_file,9);
    }

    imagedestroy($original_image);
    imagedestroy($smaller_image);
    imagedestroy($square_image);

}

保存时需要使用imagesavealpha()来保留Alpha通道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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