douketangyouzh5219 2015-07-23 22:51
浏览 167
已采纳

上传png图像黑色背景

When i upload a png image with php, the background color of the image is setted to black.
I tried to set it to transparent background but it doesn't work.
This is my code :

if( $image_type == IMAGETYPE_PNG )
{
    $dst_r = ImageCreateTrueColor($targ_w, $targ_h);
    imagealphablending($dst_r, false);
    imagesavealpha($dst_r, true);
    imagefill($dst_r,0,0,imagecolorallocatealpha($dst_r, 0,0,0,127));
    imagecopyresampled($dst_r, $this->image, 0, 0, $targ_x, $targ_y, $targ_w, $targ_h, $targ_w, $targ_h);
    imagepng($dst_r,$filename, 9);
}

Edited:

I tought i'v finished with this problem but i was wrong:

$targ_w_thumb = $targ_h_thumb = 220;
if($image_type == IMAGETYPE_PNG)
{
    $dst_r = ImageCreateTrueColor($targ_w_thumb, $targ_h_thumb);
    imagealphablending($dst_r, false);
    imagesavealpha($dst_r, true);
    imagefill($dst_r,0,0,imagecolorallocatealpha($dst_r, 0,0,0,127));
    imagecopyresampled($dst_r, $this->image, 0, 0, $targ_x, $targ_y, $targ_w_thumb, $targ_h_thumb, $targ_w, $targ_h);
    imagepng($dst_r,$filename, 9);
}
  • 写回答

2条回答 默认 最新

  • dshdb64088 2015-07-24 09:29
    关注

    I dont know why but when i added targ_w_thumb its work fine + imagefill():

        $targ_w_thumb = $targ_w_thumb = 200;
        $dst_r = ImageCreateTrueColor($targ_w_thumb, $targ_h_thumb);
        imagealphablending($dst_r, false);
        imagesavealpha($dst_r, true);
        imagefill($dst_r,0,0,imagecolorallocatealpha($dst_r, 0,0,0,127));
        imagecopyresampled($dst_r, $this->image, 0, 0, $targ_x, $targ_y, $targ_w_thumb, $targ_h_thumb, $targ_w, $targ_h);
        imagepng($dst_r,$filename, 9);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统