【php开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了php开源代码-PNG.php的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
<?php
/**
* @package Zend_Pdf
*/
/** Zend_Pdf_Font */
require_once 'Zend/Pdf/Resource/Image.php';
/** Zend_Pdf_Const */
require_once 'Zend/Pdf/Const.php';
/** Zend_Pdf_Exception */
require_once 'Zend/Pdf/Exception.php';
/** Zend_Pdf_Element_Numeric */
require_once 'Zend/Pdf/Element/Numeric.php';
/** Zend_Pdf_Element_Name */
require_once 'Zend/Pdf/Element/Name.php';
/**
* PNG image
*
* @package Zend_Pdf
*/
class Zend_Pdf_Image_PNG extends Zend_Pdf_Image
{
const PNG_COMPRESSION_DEFAULT_STRATEGY = 0;
const PNG_COMPRESSION_FILTERED = 1;
const PNG_COMPRESSION_HUFFMAN_ONLY = 2;
const PNG_COMPRESSION_RLE = 3;
const PNG_FILTER_NONE = 0;
const PNG_FILTER_SUB = 1;
const PNG_FILTER_UP = 2;
const PNG_FILTER_AVERAGE = 3;
const PNG_FILTER_PAETH = 4;
const PNG_INTERLACING_DISABLED = 0;
const PNG_INTERLACING_ENABLED = 1;
const PNG_CHANNEL_GRAY = 0;
const PNG_CHANNEL_RGB = 2;
const PNG_CHANNEL_INDEXED = 3;
const PNG_CHANNEL_GRAY_ALPHA = 4;
const PNG_CHANNEL_RGB_ALPHA = 6;
/**
* Object constructor
*
* @param string $imageFileName
* @throws Zend_Pdf_Exception
*/
public function __construct($imageFileName)
{
if (($imageFile = @fopen($imageFileName, 'rb')) === false ) {
throw new Zend_Pdf_Exception( "Can not open '$imageFileName' file for reading." );
}
parent::__construct();
//Check if the file is a PNG
fseek($imageFile, 1, SEEK_CUR); //First signature byte (%)
if ('PNG' != fread($imageFile, 3)) {
throw new Zend_Pdf_Exception('Image is not a PNG');
}
fseek($imageFile, 12, SEEK_CUR); //Signature bytes (Includes the IHDR chunk) IHDR processed linerarly because it doesnt contain a variable chunk length
$wtmp = unpack('Ni',fread($imageFile, 4)); //Unpack a 4-Byte Long
$width = $wtmp['i'];
$htmp = unpack('Ni',fread($imageFile, 4));
$height = $htmp['i'];
$bits = ord(fread($imageFile, 1)); //Higher than 8 bit depths are only supported in later versions of PDF.
$color = ord(fread($imageFile, 1));
if (ord(fread($imageFile, 1)) != Zend_Pdf_Image_PNG::PNG_COMPRESSION_DEFAULT_STRATEGY) {
//TODO: Add compression conversions
throw new Zend_Pdf_Exception( "Only the default compression strategy is currently supported." );
}
if (ord(fread($imageFile,1)) != Zend_Pdf_Image_PNG::PNG_FILTER_NONE) {
//TODO: Support PNG Filtering
throw new Zend_Pdf_Exception( "Filtering methods are not currently supported. " );
}
if (ord(fread($imageFile,1)) != Zend_Pdf_Image_PNG::PNG_INTERLACING_DISABLED) {
//TODO: Support Interlacing
throw new Zend_Pdf_Exception( "Only non-interlaced images are currently supported." );
}
fseek($imageFile, 4, SEEK_CUR); //4 Byte Ending Sequence
$imageData = '';
/*
* The following loop processes PNG chunks. 4 Byte Longs are packed first give the chunk length
* followed by the chunk signature, a four byte code. IDAT and IEND are manditory in any PNG.
*/
while($chunkLengthBytes = fread($imageFile, 4)) {
$chunkLengthtmp = unpack('Ni', $chunkLengthBytes);
$chunkLength = $chunkLengthtmp['i'];
$chunkType = fread($imageFile, 4);
switch($chunkType) {
//TODO: Support all PNG chunks
case 'IDAT': //Image Data
$imageData .= fread($imageFile, $chunkLength);
fseek($imageFile, 4, SEEK_CUR);
break;
case 'PLTE': //Palette
if ($color != Zend_Pdf_Image_PNG::PNG_CHANNEL_INDEXED) {
throw new Zend_Pdf_Exception( "Only indexed color PNG's can contain palette entries." );
}
$paletteData = fread($imageFile, $chunkLength);
fseek($imageFile, 4, SEEK_CUR);
break;
case 'tRNS': //Basic (non-alpha channel) transparency. (untested)
switch ($color) {
case Zend_Pdf_Image_PNG::PNG_CHANNEL_GRAY:
$baseColor = unpack('n',fread($imageFile, 2));
$transparencyData = array($baseColor['n']);
fseek($imageFile, $chunkLength - 2, SEEK_CUR);
break;
case Zend_Pdf_Image_PNG::PNG_CHANNEL_RGB:
$red = unpack('n', fread($imageFile, 2));
$green = unpack('n', fread($imageFile, 2));
$blue = unpack('n', fread($imageFile, 2));
$transparencyData = array($red['n'], $green['n'], $blue['n']);
fseek($imageFile, $chunkLength - 6, SEEK_CUR);
break;
case Zend_Pdf_Image_PNG::PNG_CHANNEL_INDEXED:
//TODO: Read "For color type 3 (indexed color), the tRNS chunk contains a series of one-byte alpha values, corresponding to entries in the PLTE chunk"
fseek($imageFile, $chunkLength, SEEK_CUR);
throw new Zend_Pdf_Exception( "tRNS c