【问题标题】:PHP - Black background after resizing transparent png and gifPHP - 调整透明 png 和 gif 大小后的黑色背景
【发布时间】:2014-06-03 04:20:13
【问题描述】:

我正在使用以下代码来调整图像大小。上传透明图像 (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);

【问题讨论】:

标签: php image transparency gd image-resizing


【解决方案1】:

尝试使用

  imagecolortransparent 

在您的代码中。

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

【讨论】:

  • 我自己解决了这个问题 $white=imagecolorallocate($dst_img, 255, 255, 255);图像填充($dst_img,0,0,$white);
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2015-03-26
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 2013-11-25
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多