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 32347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:44:54+00:00 2026-05-10T13:44:54+00:00

The following PHP code snippet uses GD to resize a browser-uploaded PNG to 128×128.

  • 0

The following PHP code snippet uses GD to resize a browser-uploaded PNG to 128×128. It works great, except that the transparent areas in the original image are being replaced with a solid color- black in my case.

Even though imagesavealpha is set, something isn’t quite right.

What’s the best way to preserve the transparency in the resampled image?

$uploadTempFile = $myField[ 'tmp_name' ] list( $uploadWidth, $uploadHeight, $uploadType )    = getimagesize( $uploadTempFile );  $srcImage = imagecreatefrompng( $uploadTempFile );     imagesavealpha( $targetImage, true );  $targetImage = imagecreatetruecolor( 128, 128 ); imagecopyresampled( $targetImage, $srcImage,                      0, 0,                      0, 0,                      128, 128,                      $uploadWidth, $uploadHeight );  imagepng(  $targetImage, 'out.png', 9 ); 
  • 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. 2026-05-10T13:44:55+00:00Added an answer on May 10, 2026 at 1:44 pm
    imagealphablending( $targetImage, false ); imagesavealpha( $targetImage, true ); 

    did it for me. Thanks ceejayoz.

    note, the target image needs the alpha settings, not the source image.

    Edit: full replacement code. See also answers below and their comments. This is not guaranteed to be be perfect in any way, but did achieve my needs at the time.

    $uploadTempFile = $myField[ 'tmp_name' ] list( $uploadWidth, $uploadHeight, $uploadType )    = getimagesize( $uploadTempFile );  $srcImage = imagecreatefrompng( $uploadTempFile );   $targetImage = imagecreatetruecolor( 128, 128 );    imagealphablending( $targetImage, false ); imagesavealpha( $targetImage, true );  imagecopyresampled( $targetImage, $srcImage,                      0, 0,                      0, 0,                      128, 128,                      $uploadWidth, $uploadHeight );  imagepng(  $targetImage, 'out.png', 9 ); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw the following code snippet: <?php if(!empty($_POST)): // case I: what is the
Following is code snippet : function something() { $include_file = 'test.php'; if ( file_exists($include_file)
Look at the following code snippet: <?php class A { function fn() { print
I have the following PHP code that uses cURL: $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,http://area51.stackexchange.com/users/flair/31.json); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
Good day. I am having the following code snippet in init.php. <?php include_once(config.php); include_once(__SITE_PATH
I have the following php code, which has been snipped for brevity. I am
I am running following PHP code to interact with a MS Access database. $odbc_con
I am using following PHP code to connect to MS Access database: $odb_conn =
I'm detecting @replies in a Twitter stream with the following PHP code using regexes.
I'm working with some old PHP code that has a lot of the following:

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.