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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:54:09+00:00 2026-06-01T09:54:09+00:00

Suppose I have any image(say passport type images where the background will be same

  • 0

Suppose I have any image(say passport type images where the background will be same around the user).

What I want to do is make that background image as transparent using PHP GD. So please let me know how can I achieve this? The example image is shown here. I want yellow color to be transparent.
enter image description here

  • 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-06-01T09:54:11+00:00Added an answer on June 1, 2026 at 9:54 am

    What you basically want to do is replace colors ‘close’ to your background color. By ‘close’ I mean colors that are similar to it:

    // $src, $dst......
    $src = imagecreatefromjpeg("dmvpic.jpg");
    
    // Alter this by experimentation
    define( "MAX_DIFFERENCE", 20 );
    
    // How 'far' two colors are from one another / color similarity.
    // Since we aren't doing any real calculations with this except comparison,
    // you can get better speeds by removing the sqrt and just using the squared portion.
    // There may even be a php gd function that compares colors already. Anyone?
    function dist($r,$g,$b) {
       global $rt, $gt, $bt;
       return sqrt( ($r-$rt)*($r-$rt) + ($g-$gt)*($g-$gt) + ($b-$bt)*($b-$bt) );
    }
    
    // Alpha color (to be replaced) is defined dynamically as 
    // the color at the top left corner...
    $src_color = imagecolorat( $src ,0,0 );
    $rt = ($src_color >> 16) & 0xFF;
    $gt = ($src_color >> 8) & 0xFF;
    $bt = $src_color & 0xFF;
    
    // Get source image dimensions and create an alpha enabled destination image
    $width = imagesx($src);
    $height = imagesy($src);
    $dst = =imagecreatetruecolor( $width, $height ); 
    imagealphablending($dst, true);
    imagesavealpha($dst, true);
    
    // Fill the destination with transparent pixels
    $trans = imagecolorallocatealpha( $dst, 0,0,0, 127 ); // our transparent color
    imagefill( $dst, 0, 0, $transparent ); 
    
    // Here we examine every pixel in the source image; Only pixels that are
    // too dissimilar from our 'alhpa' or transparent background color are copied
    // over to the destination image.
    for( $x=0; $x<$width; ++$x ) {
      for( $y=0; $y<$height; ++$y ) {
         $rgb = imagecolorat($src, $x, $y);
         $r = ($rgb >> 16) & 0xFF;
         $g = ($rgb >> 8) & 0xFF;
         $b = $rgb & 0xFF;
    
         if( dist($r,$g,$b) > MAX_DIFFERENCE ) {
            // Plot the (existing) color, in the new image
            $newcolor = imagecolorallocatealpha( $dst, $r,$g,$b, 0 );
            imagesetpixel( $dst, $x, $y, $newcolor );
         }
      }
    }
    
    header('Content-Type: image/png');
    imagepng($dst);
    imagedestroy($dst);
    imagedestroy($src);
    

    Please note above code is untested, I just typed it in stackoverflow so I may have some retarded spelling errors, but it should at bare minimal point you in the right direction.

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

Sidebar

Related Questions

Android 2.1 or greater Suppose I have two images that are the same size,
HI all ,what i want is to map the images.Suppose i have two images
suppose I have selected image from image picker,and now I want to scale that
Suppose we have the name written in any none-latin letters - languages, like Arabic,
Say I have a directory structure like so: /public /public/company /public/globals /public/globals/images /public/jobs /public/jobs/it
Basically, suppose that I have a fingerprint. I know the dimension of my image,
Suppose I have all these commercial products' logo (they are just images) in my
Suppose I have a large image (5000 x 5000), how can I randomly select
Problem: I have a set of images, lets say 5. There is an structure
Suppose I have a table view, and I want to implement something like this:

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.