23 #require_once 'Zend/Pdf/Element/Array.php'; 24 #require_once 'Zend/Pdf/Element/Name.php'; 25 #require_once 'Zend/Pdf/Element/Numeric.php'; 29 #require_once 'Zend/Pdf/Resource/Image.php'; 106 if(!isset($this->_endianType)) {
107 #require_once 'Zend/Pdf/Exception.php'; 108 throw new Zend_Pdf_Exception(
"The unpackBytes function can only be used after the endianness of the file is known");
113 $unpacked = unpack(
$format, $bytes);
118 $unpacked = unpack(
$format, $bytes);
123 $unpacked = unpack(
$format, $bytes);
128 $unpacked = unpack(
$format, $bytes);
129 return ($unpacked[1]/$unpacked[2]);
142 if (($imageFile = @
fopen($imageFileName,
'rb')) ===
false ) {
143 #require_once 'Zend/Pdf/Exception.php'; 147 $byteOrderIndicator = fread($imageFile, 2);
148 if($byteOrderIndicator ==
'II') {
150 }
else if($byteOrderIndicator ==
'MM') {
153 #require_once 'Zend/Pdf/Exception.php'; 154 throw new Zend_Pdf_Exception(
"Not a tiff file or Tiff corrupt. No byte order indication found" );
160 #require_once 'Zend/Pdf/Exception.php'; 161 throw new Zend_Pdf_Exception(
"Not a tiff file or Tiff corrupt. Incorrect version number." );
165 $fileStats = fstat($imageFile);
166 $this->_fileSize = $fileStats[
'size'];
174 while($ifdOffset > 0) {
175 if(fseek($imageFile, $ifdOffset, SEEK_SET) == -1 || $ifdOffset+2 >= $this->_fileSize) {
176 #require_once 'Zend/Pdf/Exception.php'; 177 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);
191 for($dirEntryIdx = 1; $dirEntryIdx <= $numDirEntries; $dirEntryIdx++) {
198 $fieldLength = $valueCount;
201 $fieldLength = $valueCount;
204 $fieldLength = $valueCount * 2;
207 $fieldLength = $valueCount * 4;
210 $fieldLength = $valueCount * 8;
213 $fieldLength = $valueCount;
216 $offsetBytes = fread($imageFile, 4);
218 if($fieldLength <= 4) {
251 $fp = ftell($imageFile);
252 fseek($imageFile, $refOffset, SEEK_SET);
254 fseek($imageFile, $fp, SEEK_SET);
256 $this->_bitsPerSample =
$value;
260 $this->_compression =
$value;
263 $this->_filter =
'None';
270 $this->_filter =
'CCITTFaxDecode';
271 #require_once 'Zend/Pdf/Exception.php'; 272 throw new Zend_Pdf_Exception(
"CCITTFaxDecode Compression Mode Not Currently Supported");
275 $this->_filter =
'LZWDecode';
276 #require_once 'Zend/Pdf/Exception.php'; 280 $this->_filter =
'DCTDecode';
281 #require_once 'Zend/Pdf/Exception.php'; 287 $this->_filter =
'FlateDecode';
288 #require_once 'Zend/Pdf/Exception.php'; 292 $this->_filter =
'RunLengthDecode';
297 $this->_colorCode =
$value;
298 $this->_whiteIsZero =
false;
299 $this->_blackIsZero =
false;
302 $this->_whiteIsZero =
true;
303 $this->_colorSpace =
'DeviceGray';
306 $this->_blackIsZero =
true;
307 $this->_colorSpace =
'DeviceGray';
312 $this->_colorSpace =
'DeviceRGB';
315 $this->_colorSpace =
'Indexed';
318 $this->_colorSpace =
'DeviceCMYK';
321 $this->_colorSpace =
'Lab';
324 #require_once 'Zend/Pdf/Exception.php'; 331 $fp = ftell($imageFile);
332 fseek($imageFile, $refOffset, SEEK_SET);
333 $stripOffsetsBytes = fread($imageFile, $fieldLength);
334 $this->_imageDataOffset = unpack(
$format, $stripOffsetsBytes);
335 fseek($imageFile, $fp, SEEK_SET);
337 $this->_imageDataOffset =
$value;
343 $fp = ftell($imageFile);
344 fseek($imageFile, $refOffset, SEEK_SET);
345 $stripByteCountsBytes = fread($imageFile, $fieldLength);
346 $this->_imageDataLength = unpack(
$format, $stripByteCountsBytes);
347 fseek($imageFile, $fp, SEEK_SET);
349 $this->_imageDataLength =
$value;
360 if(!isset($this->_imageDataOffset) || !isset($this->_imageDataLength)) {
361 #require_once 'Zend/Pdf/Exception.php'; 362 throw new Zend_Pdf_Exception(
"TIFF: The image processed did not contain image data as expected.");
365 $imageDataBytes =
'';
366 if(is_array($this->_imageDataOffset)) {
367 if(!is_array($this->_imageDataLength)) {
368 #require_once 'Zend/Pdf/Exception.php'; 369 throw new Zend_Pdf_Exception(
"TIFF: The image contained multiple data offsets but not multiple data lengths. Tiff may be corrupt.");
371 foreach($this->_imageDataOffset as $idx => $offset) {
372 fseek($imageFile, $this->_imageDataOffset[$idx], SEEK_SET);
373 $imageDataBytes .= fread($imageFile, $this->_imageDataLength[$idx]);
376 fseek($imageFile, $this->_imageDataOffset, SEEK_SET);
377 $imageDataBytes = fread($imageFile, $this->_imageDataLength);
379 if($imageDataBytes ===
'') {
380 #require_once 'Zend/Pdf/Exception.php'; 386 parent::__construct();
388 $imageDictionary = $this->_resource->dictionary;
389 if(!isset($this->_width) || !isset($this->_width)) {
390 #require_once 'Zend/Pdf/Exception.php'; 394 $this->_imageProperties = array();
408 if($this->_whiteIsZero ===
true) {
414 if(isset($this->_filter) && $this->_filter !=
'None') {
418 $this->_resource->value = $imageDataBytes;
419 $this->_resource->skipFilters();
const TIFF_PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO
const TIFF_TAG_IMAGE_WIDTH
const TIFF_TAG_STRIP_BYTE_COUNTS
const TIFF_COMPRESSION_FLATE_OBSOLETE_CODE
const TIFF_PHOTOMETRIC_INTERPRETATION_YCBCR
const TIFF_FIELD_TYPE_LONG
__construct($imageFileName)
const TIFF_PHOTOMETRIC_INTERPRETATION_CMYK
const TIFF_TAG_IMAGE_LENGTH
const TIFF_COMPRESSION_FLATE
unpackBytes($type, $bytes)
const TIFF_FIELD_TYPE_BYTE
const UNPACK_TYPE_RATIONAL
const TIFF_COMPRESSION_PACKBITS
const TIFF_TAG_SAMPLES_PER_PIXEL
const TIFF_COMPRESSION_LZW
const TIFF_COMPRESSION_JPEG
const TIFF_FIELD_TYPE_SHORT
const TIFF_COMPRESSION_UNCOMPRESSED
const TIFF_COMPRESSION_GROUP_3_FAX
const TIFF_PHOTOMETRIC_INTERPRETATION_RGB
const TIFF_TAG_COMPRESSION
const TIFF_PHOTOMETRIC_INTERPRETATION_RGB_INDEXED
const TIFF_TAG_BITS_PER_SAMPLE
const TIFF_PHOTOMETRIC_INTERPRETATION_BLACK_IS_ZERO
const TIFF_FIELD_TYPE_RATIONAL
const TIFF_COMPRESSION_GROUP_4_FAX
const TIFF_TAG_PHOTOMETRIC_INTERPRETATION
const TIFF_PHOTOMETRIC_INTERPRETATION_CIELAB
const TIFF_COMPRESSION_CCITT1D
const TIFF_TAG_STRIP_OFFSETS
const TIFF_FIELD_TYPE_ASCII