dsplos5731 2012-08-11 05:07
浏览 49
已采纳

为什么即使我设置了alpha混合设置,GIF图像也会失去imagegif()的透明度?

I am trying to allow the uploading of transparent profile pictures (PNGs and GIFs) on my site because it is sometimes quite annoying for the user to upload a transparent profile picture and the transparent areas become black. The problem is that the transparency is still being lost even after using the imagealpha*() functions.

I do realize that there are other questions about this, but the answers on them aren't working for me.

Here is my code:

// [...]

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

// [...]

I tried uploading this GIF just for testing:

But it lost its transparency after it was uploaded:

enter image description here

I am trying to keep the transparency information with it, but it doesn't seem to be working. Am I not doing something right?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongmopu6734 2012-08-12 06:18
    关注

    Create a transparent color and fill the $new image with that color before the copy. If you don't do that, the background color of the new image will default to black.

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

    You can also check this question

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab