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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:59:11+00:00 2026-05-26T17:59:11+00:00

First, I resize the image while saving transparency: /* all the classic routine etcetera:

  • 0

First, I resize the image while saving transparency:

/*
all the classic routine etcetera: 
$canvas = imagecreatefrom[png|gif|jpeg]();
$resize = imagecreatetruecolor();
*/

if($blending){
    $transparentIndex = imagecolortransparent($canvas);
    if($transparentIndex >= 0){
        #GIF
        imagepalettecopy($canvas, $resize);
        imagefill($resize, 0, 0, $transparentIndex);
        imagecolortransparent($resize, $transparentIndex);
        imagetruecolortopalette($resize, true, 256);
    }else{
        #PNG
        imagealphablending($resize, false);
        imagesavealpha($resize, true);                          
        $transparent = imagecolorallocatealpha($resize, 255, 255, 255, 127); 
        imagefill($resize, 0, 0, $transparent); 
    }
}

imagecopyresampled($resize, $canvas, 0, 0, 0, 0, $nx, $ny, $x, $y);

// image[png|gif|jpeg]... (image gets saved)

Then, I want to apply grayscale filter to that previously saved image (within a new function):

/*
classic routine again: 
$canvas = imagecreatefrom[png|gif|jpeg]()
*/

if($blending){          
    imagealphablending($canvas, false);
    imagesavealpha($canvas, true);
}

imagefilter($canvas, IMG_FILTER_GRAYSCALE);

/*
This fully filters PNG's to Grayscale while saving transparency,
but for GIF, black background is added to my grayscaled picture,
plus, the picture isn't fully grayscale (more like gets high-contrasted with acidic colors).
*/

// image[png|gif|jpeg]

What would be the fix, to preserve transparency when applying IMG_FILTER_GRAYSCALE to gif?

What would be the fix, in order to transform gif to grayscale while saving the transparency? (Question revised due to answer provided by @Pierre)

Thanks in advance!

  • 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-26T17:59:12+00:00Added an answer on May 26, 2026 at 5:59 pm

    imagefilter used with the gray scale filter does take care of the alpha channel.

    However, gif does not support alpha. So you won’t be able to store it using the GIF format.

    It is also important to note that the background color (one single color or color index being used as background) has nothing to do with the alpha (level of transparency of a given color or pixel). That means you are responsible to set the background color to the desired single color.

    Update

    It is not directly possible as the color used as transparent will be modified. That could be considered as a bug as the transparent color may or should be ignored by the filter. But that’s another topic.

    A workaround could be:

    
    $logo = imagecreatefromgif('php.gif');
    
    
    $newimg = imagecreatetruecolor(imagesx($logo), imagesy($logo));
    /* copy ignore the transparent color
     * so that we can use black (0,0,0) as transparent, which is what 
     * the image is filled with when created.
     */
    $transparent = imagecolorallocate($newimg, 0,0,0);
    
    imagecolortransparent($newimg, $transparent);
    imagecopy($newimg, $logo, 0,0, 0, 0,imagesx($logo), imagesx($logo));
    imagefilter($newimg, IMG_FILTER_GRAYSCALE);
    imagegif($newimg, 'a.gif');
    

    this code simply fetch the value of the existing transparent color, convert it to gray and set it back to the color index. This code will only work for palette image like gif but that’s the idea.

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

Sidebar

Related Questions

I'm trying to convert a fetched image from JPG to PNG using RMagick, resize
I am painting an jpeg image using IPicture. At first I load it, then
I have a function which first reads an image from disk, resizes it and
First of all, I know how to build a Java application. But I have
I am facing problem while resizing an image in the Popup brwoser window. Image
I am trying to resize an image with Rmagick and if I use the
I need to perform java image crop and resize without an X server. I
first fo all, sorry for my english. I am trying to do dragable and
I'm using the WebImage in MVC3 in order to resize an image. Basically, the
I have a file upload class which returns an image resize object if the

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.