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

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

图片转代码服务由CSDN问答提供 功能建议

我使用以下代码调整图像大小。 当上传透明图像(png / gif)时,创建的新图像具有黑色背景。 如何在调整大小后使背景变白? 请帮助

  $ info = pathinfo($ _ FILES ['file'] ['name']); 
 $ ext = $ info ['extension'];  //获取文件的扩展名
 $ 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')\  n {
 $ 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条回答 默认 最新

相关推荐 更多相似问题