Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7418225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:50:51+00:00 2026-05-29T07:50:51+00:00

I am trying to make a function to handle avatar uploads. My problem is,

  • 0

I am trying to make a function to handle avatar uploads.
My problem is, when I upload a transparent image, it turned everything which is transparent black after I resize it.

I have tried using the imagesavealpha() & imagealphablending() options, but the background is still turning black.

It might just be me being blind and not seeing the problem in the code, but I have no idea why it does this.

I can confirm that after the image has been moved to the image/avatars folder just after being uploaded, the background is still transparent.

this is my current code, I have been testing with .png images:

function upload_avatar(){
    $base_path = SYSTEM_PATH;

    if($_FILES['avatar_img']['tmp_name'] != '') {
        $id = md5($this->user_id());
        $filename = $_FILES['avatar_img']['name'];
        $file_basename = substr($filename, 0, strripos($filename, '.')); // strip extention
        $file_ext = substr($filename, strripos($filename, '.')); // strip name
        $filesize = $_FILES['avatar_img']['size'];
        $newfilename = $id . $file_ext;

        if ($file_ext == ".jpg" || $file_ext == ".JPG" || $file_ext == ".jpeg" || $file_ext == ".png" || $file_ext == ".gif"){
            if($filesize <= 153600){

                move_uploaded_file($_FILES['avatar_img']['tmp_name'], $base_path."/images/avatars/" . $newfilename);

                //resize image form
                list($width, $height) = getimagesize($base_path."/images/avatars/" . $newfilename);
                $scale_height = $height/$width;
                $scale_width = $width/$height;

                //Find height and width of the image
                if($width > $height && $width > 150){
                    $width_new = 150;
                    $height_new = round($width_new*$scale_height);
                }else if($height > $width && $height > 150){
                    $height_new = 150;
                    $width_new = round($height_new*$scale_width);
                }else{
                    $height_new = $height;
                    $width_new = $width;
                }

                switch($file_ext) {
                    case ".jpg" :
                        case ".jpeg":
                            $source = imagecreatefromjpeg($base_path."/images/avatars/" . $newfilename);
                    break;

                    case ".png" :
                        $source = imagecreatefrompng($base_path."/images/avatars/" . $newfilename);
                    break;

                    default:
                        $source = imagecreatefromgif($base_path."/images/avatars/" . $newfilename);
                    break;
                }

                $destination = imagecreatetruecolor($width_new, $height_new);

                imagesavealpha($destination, true);
                imagealphablending($destination, true);

                imagecopyresized($destination, $source, 0, 0, 0, 0, $width_new, $height_new, $width, $height);

                switch($file_ext) {
                    case ".jpg":
                    case ".jpeg":
                        imagejpeg($destination, $base_path."/images/avatars/" . $newfilename, 85);
                    break;

                    case ".png":
                        imagepng($destination, $base_path."/images/avatars/" . $newfilename, 8);
                    break;

                    default:
                        imagegif($destination, $base_path."/images/avatars/" . $newfilename, 85);
                    break;
                }

                return $newfilename;
            }else{
                $this->upload_avatar = '<br />But the avatar was not updated. The avatar\'s size exceeded the 150kb limit. ';
                return '';
            }
        }else{
            $this->upload_avatar = '<br />But the avatar was not updated. The avatar must be one of the following formats: jpg, jpeg, png or gif. ';
            return '';
        }
    }else{
        return '';
    }
}

Any help would be appreciated as I am going nuts looking at this now.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:50:52+00:00Added an answer on May 29, 2026 at 7:50 am

    This here may be what you’re looking for. The second comment contains an example of resizing pngs and gifs while preserving transparency.

    p.s. I’d have added this as a comment but I don’t have the rights to do that yet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

trying to make a page which will recursively call a function until a limit
Im trying to make a function that will return an element of type point:
I'm trying to make a function that holds state but is called with foo().
I'm trying to make a function that will take short hand hex color to
GCC seem to think that I am trying to make a function call in
I'm using folding-mode in emacs and was trying to make a function to insert
I'm trying to make a Javascript function that will take a mathematical expression and
I am trying to make a search function using the combination of DOM, PHP
im trying to make a small name summary function depending on the size of
I'm trying to make a autoscrolling/carousel like function for an unordered list of images.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.