2

For one of my scripts I need to check if gif images are transparent (have transparency) using php. Note, I only need this for gif images.

My sincere apologies if this has been asked. I couldn't find anything that answers this question or works.

Alex333
  • 217
  • 1
  • 5
  • Perhaps you could outsource the check to the browser? If yes, then paint the gif into a canvas and send back the result to the server. Just an idea. – nalply Jan 11 '20 at 19:32
  • definitely possible, you could implement a gif parser in pure php, but it wouldn't be easy. you can the file specifications here: https://www.w3.org/Graphics/GIF/spec-gif89a.txt - that said, it would be much easier to just ship ffprobe as a base64 string, and creating it as needed, than to implement a parser in pure php. (take a look at the file specifically and it should be fairly obvious. my browser tells me that printing the specs to paper would take 36x A4 papers) – hanshenrik Jan 11 '20 at 23:12

2 Answers2

1

After looking through some documentation there does not appear to be any quick and dirty way to find out if a gif has a transparency.

The only way appears to be to use a library like GD or Imagick.

Jim
  • 3,210
  • 2
  • 17
  • 23
0

Sorry, only possible with GD:

      $image_original = imagecreatefromgif($image_file_path)
      if (imagecolorsforindex($image_original, imagecolorstotal($image_original)-1)['alpha'] == 127) {
        echo '<strong style="color:crimson">TRANSP. GIF</strong>';
      }
adilbo
  • 910
  • 14
  • 22