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

为什么即使我设置了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 python怎么在已有视频文件后添加新帧
  • ¥20 虚幻UE引擎如何让多个同一个蓝图的NPC执行一样的动画,
  • ¥15 fluent里模拟降膜反应的UDF编写
  • ¥15 MYSQL 多表拼接link
  • ¥15 关于某款2.13寸墨水屏的问题
  • ¥15 obsidian的中文层级自动编号
  • ¥15 同一个网口一个电脑连接有网,另一个电脑连接没网
  • ¥15 神经网络模型一直不能上GPU
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵