note 49024 deleted from function.imagecolortransparent by cmb
| From: | cmb@php.net | Date: | Sun, 01 Dec 2019 08:46:33 +0000 |
| Subject: | note 49024 deleted from function.imagecolortransparent by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213318@lists.php.net to get a copy of this message | ||
Note Submitter: fred at webblake dot net
----
Wow! I am actually surprized there are no functions to retrieve transparency information from the
image. I assume I'm not missing anything since there are all kinds of notes about how to get
around this limitation. At any rate, here is some code to read the gif file and retrieve the missing
information. The code will speak volumes.
<?php
class GifInfo
{
var $m_transparentRed;
var $m_transparentGreen;
var $m_transparentBlue;
var $m_signature;
var $m_version;
var $m_width;
var $m_height;
var $m_width;
var $m_height;
var $m_colorFlag;
var $m_backgroundIndex;
function GifInfo($filename)
{
$fp = fopen($filename,"rb");
$result = fread($fp,13);
$this->m_signature = substr($result,0,3);
$this->m_version = substr($result,3,3);
$this->m_width = ord(substr($result,6,1)) + ord(substr($result,7,1)) * 256;
$this->m_height = ord(substr($result,8,1)) + ord(substr($result,9,1)) * 256;
$this->m_colorFlag = ord(substr($result,10,1)) >> 7;
$this->m_background = ord(substr($result,11));
if($this->m_colorFlag)
{
$tableSizeNeeded = ($this->m_background + 1) * 3;
$result = fread($fp,$tableSizeNeeded);
$this->m_transparentRed = ord(substr($result,$this->m_background * 3,1));
$this->m_transparentGreen = ord(substr($result,$this->m_background * 3 + 1,1));
$this->m_transparentBlue = ord(substr($result,$this->m_background * 3 + 2,1));
}
fclose($fp);
}
}
?>
And to use this class....
<?php
$size = getimagesize($userfile);
$img2 = ImageCreate($w,$h);
$gi = new GifInfo($userfile);
if($gi->m_version == '89a' && $gi->m_colorFlag == 1)
{
$transparent = imagecolorallocate($img2,
$gi->m_transparentRed,
$gi->m_transparentGreen,
$gi->m_transparentBlue);
imagecolortransparent ($img2,$transparent);
}
?>