PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 11, 2022

[FIXED] Why is a GIF image losing its transparency with imagegif() even when I set the alpha blending settings?

 October 11, 2022     gd, image, php     No comments   

Issue

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.


Solution

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



Answered By - huysentruitw
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing