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

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

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

  $ 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)  ; 
  code>  pre> 
  div>

Try using

  imagecolortransparent 

in your code.

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