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 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)