📄 tiff.php
字号:
<?php/** * Zend Framework * * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://framework.zend.com/license/new-bsd * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@zend.com so we can send you a copy immediately. * * @package Zend_Pdf * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License *//** Zend_Pdf_Resource_Image */require_once 'Zend/Pdf/Resource/Image.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';/** * TIFF image * * @package Zend_Pdf * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */class Zend_Pdf_Resource_Image_Tiff extends Zend_Pdf_Resource_Image{ const TIFF_FIELD_TYPE_BYTE=1; const TIFF_FIELD_TYPE_ASCII=2; const TIFF_FIELD_TYPE_SHORT=3; const TIFF_FIELD_TYPE_LONG=4; const TIFF_FIELD_TYPE_RATIONAL=5; const TIFF_TAG_IMAGE_WIDTH=256; const TIFF_TAG_IMAGE_LENGTH=257; //Height const TIFF_TAG_BITS_PER_SAMPLE=258; const TIFF_TAG_COMPRESSION=259; const TIFF_TAG_PHOTOMETRIC_INTERPRETATION=262; const TIFF_TAG_STRIP_OFFSETS=273; const TIFF_TAG_SAMPLES_PER_PIXEL=277; const TIFF_TAG_STRIP_BYTE_COUNTS=279; const TIFF_COMPRESSION_UNCOMPRESSED = 1; const TIFF_COMPRESSION_CCITT1D = 2; const TIFF_COMPRESSION_GROUP_3_FAX = 3; const TIFF_COMPRESSION_GROUP_4_FAX = 4; const TIFF_COMPRESSION_LZW = 5; const TIFF_COMPRESSION_JPEG = 6; const TIFF_COMPRESSION_FLATE = 8; const TIFF_COMPRESSION_FLATE_OBSOLETE_CODE = 32946; const TIFF_COMPRESSION_PACKBITS = 32773; const TIFF_PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO=0; const TIFF_PHOTOMETRIC_INTERPRETATION_BLACK_IS_ZERO=1; const TIFF_PHOTOMETRIC_INTERPRETATION_RGB=2; const TIFF_PHOTOMETRIC_INTERPRETATION_RGB_INDEXED=3; const TIFF_PHOTOMETRIC_INTERPRETATION_CMYK=5; const TIFF_PHOTOMETRIC_INTERPRETATION_YCBCR=6; const TIFF_PHOTOMETRIC_INTERPRETATION_CIELAB=8; protected $_width; protected $_height; protected $_imageProperties; protected $_endianType; protected $_fileSize; protected $_bitsPerSample; protected $_compression; protected $_filter; protected $_colorCode; protected $_whiteIsZero; protected $_blackIsZero; protected $_colorSpace; protected $_imageDataOffset; protected $_imageDataLength; const TIFF_ENDIAN_BIG=0; const TIFF_ENDIAN_LITTLE=1; const UNPACK_TYPE_BYTE=0; const UNPACK_TYPE_SHORT=1; const UNPACK_TYPE_LONG=2; const UNPACK_TYPE_RATIONAL=3; /** * Byte unpacking function * * Makes it possible to unpack bytes in one statement for enhanced logic readability. * * @param int $type * @param string $bytes * @throws Zend_Pdf_Exception */ protected function unpackBytes($type, $bytes) { if(!isset($this->_endianType)) { throw new Zend_Pdf_Exception("The unpackBytes function can only be used after the endianness of the file is known"); } switch($type) { case Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_BYTE: $format = 'C'; $unpacked = unpack($format, $bytes); return $unpacked[1]; break; case Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_SHORT: $format = ($this->_endianType == Zend_Pdf_Resource_Image_Tiff::TIFF_ENDIAN_LITTLE)?'v':'n'; $unpacked = unpack($format, $bytes); return $unpacked[1]; break; case Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_LONG: $format = ($this->_endianType == Zend_Pdf_Resource_Image_Tiff::TIFF_ENDIAN_LITTLE)?'V':'N'; $unpacked = unpack($format, $bytes); return $unpacked[1]; break; case Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_RATIONAL: $format = ($this->_endianType == Zend_Pdf_Resource_Image_Tiff::TIFF_ENDIAN_LITTLE)?'V2':'N2'; $unpacked = unpack($format, $bytes); return ($unpacked[1]/$unpacked[2]); break; } } /** * 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." ); } $byteOrderIndicator = fread($imageFile, 2); if($byteOrderIndicator == 'II') { $this->_endianType = Zend_Pdf_Resource_Image_Tiff::TIFF_ENDIAN_LITTLE; } else if($byteOrderIndicator == 'MM') { $this->_endianType = Zend_Pdf_Resource_Image_Tiff::TIFF_ENDIAN_BIG; } else { throw new Zend_Pdf_Exception( "Not a tiff file or Tiff corrupt. No byte order indication found" ); } $version = $this->unpackBytes(Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_SHORT, fread($imageFile, 2)); if($version != 42) { throw new Zend_Pdf_Exception( "Not a tiff file or Tiff corrupt. Incorrect version number." ); } $ifdOffset = $this->unpackBytes(Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_LONG, fread($imageFile, 4)); $fileStats = fstat($imageFile); $this->_fileSize = $fileStats['size']; /* * Tiff files are stored as a series of Image File Directories (IFD) each direcctory * has a specific number of entries each 12 bytes in length. At the end of the directories * is four bytes pointing to the offset of the next IFD. */ while($ifdOffset > 0) { if(fseek($imageFile, $ifdOffset, SEEK_SET) == -1 || $ifdOffset+2 >= $this->_fileSize) { throw new Zend_Pdf_Exception("Could not seek to the image file directory as indexed by the file. Likely cause is TIFF corruption. Offset: ". $ifdOffset); } $numDirEntries = $this->unpackBytes(Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_SHORT, fread($imageFile, 2)); /* * Since we now know how many entries are in this (IFD) we can extract the data. * The format of a TIFF directory entry is: * * 2 bytes (short) tag code; See TIFF_TAG constants at the top for supported values. (There are many more in the spec) * 2 bytes (short) field type * 4 bytes (long) number of values, or value count. * 4 bytes (mixed) data if the data will fit into 4 bytes or an offset if the data is too large. */ for($dirEntryIdx = 1; $dirEntryIdx <= $numDirEntries; $dirEntryIdx++) { $tag = $this->unpackBytes(Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_SHORT, fread($imageFile, 2)); $fieldType = $this->unpackBytes(Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_SHORT, fread($imageFile, 2)); $valueCount = $this->unpackBytes(Zend_Pdf_Resource_Image_Tiff::UNPACK_TYPE_LONG, fread($imageFile, 4)); switch($fieldType) { case Zend_Pdf_Resource_Image_Tiff::TIFF_FIELD_TYPE_BYTE: $fieldLength = $valueCount; break; case Zend_Pdf_Resource_Image_Tiff::TIFF_FIELD_TYPE_ASCII: $fieldLength = $valueCount; break; case Zend_Pdf_Resource_Image_Tiff::TIFF_FIELD_TYPE_SHORT: $fieldLength = $valueCount * 2; break; case Zend_Pdf_Resource_Image_Tiff::TIFF_FIELD_TYPE_LONG: $fieldLength = $valueCount * 4; break; case Zend_Pdf_Resource_Image_Tiff::TIFF_FIELD_TYPE_RATIONAL: $fieldLength = $valueCount * 8; break; default: $fieldLength = $valueCount; } $offsetBytes = fread($imageFile, 4); if($fieldLength <= 4) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -