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

时间:2014-04-18 16:03:58

标签: php image transparency gd image-resizing

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

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

2 个答案:

答案 0 :(得分:1)

尝试使用

  imagecolortransparent 

代码。

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

答案 1 :(得分:0)

避免使用dropzone resizeWidth方法。 调整后端图像的大小。