dongpu2727 2014-04-18 16:03
浏览 122
已采纳

PHP - 调整透明png和gif大小后的黑色背景

I am using the below code to resize images. When transparent images, (png/gif) are uploaded the new images created have black background. How can i make the background white after resizing? pls help

    $info = pathinfo($_FILES['file']['name']);
    $ext = $info['extension']; // get the extension of the file
    $newname = "testing".".".$ext; 
    $target = 'uploads/'.$newname;
    move_uploaded_file( $_FILES['file']['tmp_name'], $target);;
    $filename=$newname;
    if($ext=='jpg'||$ext=='jpeg') 
    {
        $im = imagecreatefromjpeg('uploads/'.$filename);
    } 
    else if ($ext=='gif') 
    {
        $im = imagecreatefromgif('uploads/'.$filename);
    } 
    else if ($ext=='png') 
    {
        $im = imagecreatefrompng('uploads/'.$filename);
    }
    $ox = imagesx($im);
    $oy = imagesy($im);
    $nm = imagecreatetruecolor(245, 184);
    imagealphablending( $nm, FALSE );
    imagesavealpha( $nm, TRUE );
    imagecopyresized($nm, $im, 0,0,0,0,245,184,$ox,$oy);
    imagejpeg($nm,  'uploads/' . $filename);
  • 写回答

1条回答 默认 最新

  • douyihuaimao733955 2014-04-18 16:35
    关注

    Try using

      imagecolortransparent 
    

    in your code.

    http://www.php.net/manual/en/function.imagecolortransparent.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作