dounie0889 2014-03-20 16:01
浏览 75

问题上传PNG图像是否透明?

I'm having some issues uploading PNG images with transparency.

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.

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){

    // 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);

}
  • 写回答

1条回答 默认 最新

  • douqi1928 2014-03-20 16:05
    关注

    You need to use imagesavealpha() in order to preserve the alpha channel when saving.

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮