Russian (CIS)
 
FPDF error: Alpha channel not supported
15.05.2012 00:00

Иногда при генерации pdf файлов с помощью FPDF при использовании png картинок может возникнуть ошибка FPDF error: Alpha channel not supported если в картинке используется альфа канал.

Необходимо перед использованием картинки из нее удалить альфа канал, для этого сами напишем функцию и будем ею пользоваться.

class RemoveAlphaPng{
	
function RemoveAlphaPng($file){
	if($file=='')return;
	$info=$this->_parsepng($file);
	if ($info=='alpha'){
		// needs GD 2.x extension
		// pixel-wise operation, not very fast
		list($wpx, $hpx) = getimagesize($file);
        $img = imagecreatefrompng($file);
        $alpha_img = imagecreate( $wpx, $hpx );

        // generate gray scale pallete
        for($c=0;$cError('Not a PNG file: '.$file);
	//Read header chunk
	fread($f,4);
	if(fread($f,4)!='IHDR')
	        $this->Error('Incorrect PNG file: '.$file);
	$w=$this->_readint($f);
	$h=$this->_readint($f);
	$bpc=ord(fread($f,1));
	if($bpc>8)
	        $this->Error('16-bit depth not supported: '.$file);
	$ct=ord(fread($f,1));
	if($ct==0)
	        $colspace='DeviceGray';
	elseif($ct==2)
	        $colspace='DeviceRGB';
	elseif($ct==3)
	        $colspace='Indexed';
	else {
	        fclose($f);      // the only changes are
	        return 'alpha';  // made in those 2 lines
	}
	if(ord(fread($f,1))!=0)
	        $this->Error('Unknown compression method: '.$file);
	if(ord(fread($f,1))!=0)
	        $this->Error('Unknown filter method: '.$file);
	if(ord(fread($f,1))!=0)
	        $this->Error('Interlacing not supported: '.$file);
	fread($f,4);
	$parms='/DecodeParms ';
	//Scan chunks looking for palette, transparency and image data
	$pal='';
	$trns='';
	$data='';
	do
	{
	        $n=$this->_readint($f);
	        $type=fread($f,4);
	        if($type=='PLTE')
	        {
	                //Read palette
	                $pal=fread($f,$n);
	                fread($f,4);
	        }
	        elseif($type=='tRNS')
	        {
	                //Read transparency info
	                $t=fread($f,$n);
	                if($ct==0)
	                        $trns=array(ord(substr($t,1,1)));
	                elseif($ct==2)
	                        $trns=array(ord(substr($t,1,1)),
	                        ord(substr($t,3,1)),ord(substr($t,5,1)));
	                else
	                {
	                        $pos=strpos($t,chr(0));
	                        if($pos!==false)
	                                $trns=array($pos);
	                }
	                fread($f,4);
	        }
	        elseif($type=='IDAT')
	        {
	                //Read image data block
	                $data.=fread($f,$n);
	                fread($f,4);
	        }
	        elseif($type=='IEND')
	                break;
	        else
	                fread($f,$n+4);
	}
	while($n);
	if($colspace=='Indexed' && empty($pal))
	        $this->Error('Missing palette in '.$file);
	fclose($f);
	return array('w'=>$w,'h'=>$h,'cs'=>$colspace,'bpc'=>$bpc,'f'=>'FlateDecode',
'parms'=>$parms,'pal'=>$pal,'trns'=>$trns,'data'=>$data);
}

function _readstream($f, $n)
{
	//Read n bytes from stream
	$res='';
	while($n>0 && !feof($f))
	{
	        $s=fread($f,$n);
	        if($s===false)
	                $this->Error('Error while reading stream');
	        $n-=strlen($s);
	        $res.=$s;
	}
	if($n>0)
	        $this->Error('Unexpected end of stream');
	return $res;
}

function _readint($f)
{
    //Read a 4-byte integer from stream
    $a=unpack('Ni',$this->_readstream($f,4));
    return $a['i'];
}
// GD seems to use a different gamma, this method is used to correct it again
function _gamma($v){
    return pow ($v/255, 2.2) * 255;
}

}

 
reload Загадки, Афоризмы

Легче всего создаются трудности

Посещаемость

Яндекс.Метрика