【问题标题】:Why is a GIF image losing its transparency with imagegif() even when I set the alpha blending settings?为什么即使我设置了 Alpha 混合设置,使用 imagegif() 的 GIF 图像也会失去透明度?
【发布时间】:2012-08-11 05:07:36
【问题描述】:

我正在尝试允许在我的网站上上传透明的个人资料图片(PNG 和 GIF),因为有时用户上传透明的个人资料图片会很烦人,并且透明区域会变黑。问题是即使使用了imagealpha*() 函数,透明度仍然会丢失。

我确实意识到对此还有其他问题,但这些问题的答案对我不起作用。

这是我的代码:

// [...]

switch(strtolower($_FILES['picture']['type'])) {
   case 'image/jpeg':
      $image = imagecreatefromjpeg($_FILES['picture']['tmp_name']);
   break;
   case 'image/png':
      $image = imagecreatefrompng($_FILES['picture']['tmp_name']);
   break;
   case 'image/gif':
      $image = imagecreatefromgif($_FILES['picture']['tmp_name']);
   break;
   default:
      msg('Sorry, but the type of file that you selected is not allowed. We only allow JPEG, PNG, and GIF.','error');
      header("Location: /settings/profile");
      exit;
}

// Target dimensions
$max_width = 143;
$max_height = 143;

// Get current dimensions
$old_width  = imagesx($image);
$old_height = imagesy($image);

// Calculate the scaling we need to do to fit the image inside our frame
$scale      = min($max_width/$old_width, $max_height/$old_height);

// Get the new dimensions
$new_width  = ceil($scale*$old_width);
$new_height = ceil($scale*$old_height);

// Create new empty image
$new = imagecreatetruecolor($new_width, $new_height);

// Resize old image into new
imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
$file_name = 'avatar_'.randString(20).mt_rand(111,999).'.'.str_replace('image/','',$_FILES['picture']['type']);

switch(strtolower($_FILES['picture']['type'])) {
   case 'image/jpeg':
      $img = imagejpeg($new, 'user/uploads/'.$file_name, 95);
   break;
   case 'image/png':
      imagealphablending($new, false);
      imagesavealpha($new, true);
      $img = imagepng($new, 'user/uploads/'.$file_name, 95);
   break;
   case 'image/gif':
      imagealphablending($new, false);
      imagesavealpha($new, true);
      $img = imagegif($new, 'user/uploads/'.$file_name);
   break;
}

imagedestroy($image);
imagedestroy($new);

if($img) {
   $dbUpdate = mysql_query("UPDATE users SET user_pic = '$file_name' WHERE uid = $userid");
}

if($img && $dbUpdate) {
   msg("Your profile picture has been changed successfully.","success");
   header("Location: /settings/profile");
   exit;
}

// [...]

我尝试上传这个 GIF 只是为了测试:

但是上传后就失去了透明度:

我正在尝试保留透明度信息,但它似乎不起作用。我做的不对吗?

提前致谢。

【问题讨论】:

  • 您使用哪个浏览器查看生成的图像?
  • 嗯。这实际上可能并不重要,因为它看起来像是对 gif 图像本身的警告。您需要将透明度颜色设置为黑色,但当图像中有黑色时,您必须绕过。也许试试这个:mummey.org/2008/11/transparent-gifs-with-php-and-gd
  • 我在 OS X 上使用 Google Chrome。但它在 Finder 中甚至看起来像。
  • 我想 GIF 实际上并不重要,我只关心使 PNG 不会比 GIF 更失去透明度。但我不喜欢透明部分变黑的方式。有没有办法让它变白呢? (虽然我仍然希望保持 PNG 透明,但此时 GIF 不是我的主要关注点)
  • 是的,PNG 不是问题,因为它们一直非常适合具有透明背景的图像(IE 6 及更低版本不支持 PNG 的透明度,尽管在浏览器中查看它们时)。对于 gif 文件,您可以使用 imagecolortransparent 将透明度设置为白色

标签: php image gd


【解决方案1】:

创建一个透明颜色并在复制之前用该颜色填充$new 图像。如果不这样做,新图像的背景颜色将默认为黑色。

$new = imagecreatetruecolor($new_width, $new_height); 
$transparent = imagecolorallocatealpha($new, 0, 0, 0, 127);
imagefill($new, 0, 0, $transparent);
imagealphablending($new, true); 

您也可以查看question

【讨论】:

    猜你喜欢
    • 2012-06-17
    • 2012-04-13
    • 2017-09-25
    • 2023-03-21
    • 2017-02-14
    • 2011-09-08
    • 2015-02-03
    • 2015-12-05
    • 1970-01-01
    相关资源
    最近更新 更多