【问题标题】:PHP resizing PNG images generate black backgroundPHP调整PNG图像大小生成黑色背景
【发布时间】:2014-05-07 03:45:15
【问题描述】:

这是我用来显示或调整大小并显示缓存标头的部分代码。它适用于 JPG 图像。但是 PNG 图像被添加为黑色背景。此类问题可在 SO 和 Google 中找到,但接受或建议的解决方案几乎是相同的:imagealphablending - FALSE 和 imagesavealpha - TRUE。但就我而言,没有任何效果。会有什么问题?

$image_information=getimagesize($img);
if($image_information['mime']=='image/gif')
{
    $img=imagecreatefromgif($img);
    $type="gif";
    $image_header="image/gif";
}
elseif($image_information['mime']=='image/png')
{
    $img=imagecreatefrompng($img);
    $type="png";
    $image_header="image/png";
}
else
{
    $img=imagecreatefromjpeg($img);
    $type="jpg";
    $image_header="image/jpeg";
}

$width=imagesx($img);
$height=imagesy($img);

if((isset($w))&(!isset($h))) // If Width or Height is posted, calculate the aspect dimensions
{
    $h=($height/$width*$w);
}

if((isset($h))&(!isset($w)))
{
    $w=(($h*$width)/$height);
}

if(!isset($w))
{
    $w=$width;
}

if(!isset($h))
{
    $h=$height;
}


$new_image=imagecreatetruecolor($w, $h);

if($type=="gif")
{
    $background=imagecolorallocate($new_image, 0, 0, 0);
    // removing the black from the placeholder
    imagecolortransparent($new_image, $background); 
}
elseif($type=="png")
{
    imagealphablending($new_image, FALSE);
        imagesavealpha($new_image, TRUE);
    $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
        imagefilledrectangle($new_image, 0, 0, $w, $h, $transparent);
}

imagecopyresampled($new_image,$img,0,0,0,0,$w,$h,$width,$height);
$seconds_to_cache = 864000; // Add cache headers
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
header("Expires: $ts");
header("Pragma: cache");
header("Cache-Control:max-age=$seconds_to_cache, must-revalidate");
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $lastModified)." GMT");


header('Content-Type: $image_header');
if($type="jpg")
imagejpeg($new_image, NULL, 75);
elseif($type=="png")
imagepng($new_image, NULL, 75);
elseif($type=="gif")
imagegif($new_image, NULL);
imagedestroy($new_image);

编辑:我将图像保存到我的系统,尝试打开,

  1. Picasa 照片查看器 - 图片以黑色背景打开。
  2. Ms Paint - 图像以黑色背景打开。
  3. Photoshop CS6 - 弹出错误消息:由于程序错误而无法打开。

【问题讨论】:

  • 澄清一下,问题是通过PHP保存图像还是通过PHP在网页上显示图像?
  • 用 PHP 显示图像。当需要多种尺寸时,我使用以下限制,“images/image-name.extension/int/int”-> 扩展名将是 jpg 或 png 或 gif,第一个 int 将是数字宽度,第二个将是数字高度.可以省略 Wither 或两个 int 值。
  • @Timvp:很好,正在使用中。

标签: php image


【解决方案1】:

您没有保存原始图像中的 Alpha 通道。

添加

imagealphablending($img, true);

就在下面代码的正上方

$width=imagesx($img); $height=imagesy($img);

编辑:

然后试试这个

$transparent = imagecolortransparent($new_image, imagecolorallocatealpha($new_image, 255, 255, 255, 127));
imagefill($new_image, 0, 0, $transparent);

而不是填充矩形的透明代码

【讨论】:

    【解决方案2】:

    我建议您使用编码良好的类来进行图像处理。 Verot.net's php class for image upload 在一个 DRY、结构良好的 API 中提供除了上传操作之外的各种图像处理功能。最后,您的代码将更具可移植性和面向未来的能力。

    【讨论】:

    • 谢谢pixeline,但我只是想知道我的代码不起作用的原因。我已经在 Internet 上看到过类似的脚本。
    • 好的。然后,我会查看您的特定服务器配置正在使用什么图像处理库。是GD吗? GD2?图像魔术?根据我的经验,ImageMagick 是最好的和最通用的。查看已安装的(phpinfo() 将为您提供)。否则,你就是在黑暗中拍摄。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多