7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: Image.php 16971 2009-07-22 18:05:45Z mikaelkael $
23 /** Zend_Pdf_FileParserDataSource */
24 require_once 'Zend/Pdf/FileParserDataSource.php';
26 /** Zend_Pdf_FileParserDataSource_File */
27 require_once 'Zend/Pdf/FileParserDataSource/File.php';
29 /** Zend_Pdf_FileParserDataSource_String */
30 require_once 'Zend/Pdf/FileParserDataSource/String.php';
33 * Abstract factory class which vends {@link Zend_Pdf_Resource_Image} objects.
35 * This class is also the home for image-related constants because the name of
36 * the true base class ({@link Zend_Pdf_Resource_Image}) is not intuitive for the
41 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
42 * @license http://framework.zend.com/license/new-bsd New BSD License
44 abstract class Zend_Pdf_Image
46 /**** Class Constants ****/
51 const TYPE_UNKNOWN
= 0;
58 const TIFF_FIELD_TYPE_BYTE
=1;
59 const TIFF_FIELD_TYPE_ASCII
=2;
60 const TIFF_FIELD_TYPE_SHORT
=3;
61 const TIFF_FIELD_TYPE_LONG
=4;
62 const TIFF_FIELD_TYPE_RATIONAL
=5;
64 const TIFF_TAG_IMAGE_WIDTH
=256;
65 const TIFF_TAG_IMAGE_LENGTH
=257; //Height
66 const TIFF_TAG_BITS_PER_SAMPLE
=258;
67 const TIFF_TAG_COMPRESSION
=259;
68 const TIFF_TAG_PHOTOMETRIC_INTERPRETATION
=262;
69 const TIFF_TAG_STRIP_OFFSETS
=273;
70 const TIFF_TAG_SAMPLES_PER_PIXEL
=277;
71 const TIFF_TAG_STRIP_BYTE_COUNTS
=279;
73 const TIFF_COMPRESSION_UNCOMPRESSED
= 1;
74 const TIFF_COMPRESSION_CCITT1D
= 2;
75 const TIFF_COMPRESSION_GROUP_3_FAX
= 3;
76 const TIFF_COMPRESSION_GROUP_4_FAX
= 4;
77 const TIFF_COMPRESSION_LZW
= 5;
78 const TIFF_COMPRESSION_JPEG
= 6;
79 const TIFF_COMPRESSION_FLATE
= 8;
80 const TIFF_COMPRESSION_FLATE_OBSOLETE_CODE
= 32946;
81 const TIFF_COMPRESSION_PACKBITS
= 32773;
83 const TIFF_PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO
=0;
84 const TIFF_PHOTOMETRIC_INTERPRETATION_BLACK_IS_ZERO
=1;
85 const TIFF_PHOTOMETRIC_INTERPRETATION_RGB
=2;
86 const TIFF_PHOTOMETRIC_INTERPRETATION_RGB_INDEXED
=3;
87 const TIFF_PHOTOMETRIC_INTERPRETATION_CMYK
=5;
88 const TIFF_PHOTOMETRIC_INTERPRETATION_YCBCR
=6;
89 const TIFF_PHOTOMETRIC_INTERPRETATION_CIELAB
=8;
93 const PNG_COMPRESSION_DEFAULT_STRATEGY
= 0;
94 const PNG_COMPRESSION_FILTERED
= 1;
95 const PNG_COMPRESSION_HUFFMAN_ONLY
= 2;
96 const PNG_COMPRESSION_RLE
= 3;
98 const PNG_FILTER_NONE
= 0;
99 const PNG_FILTER_SUB
= 1;
100 const PNG_FILTER_UP
= 2;
101 const PNG_FILTER_AVERAGE
= 3;
102 const PNG_FILTER_PAETH
= 4;
104 const PNG_INTERLACING_DISABLED
= 0;
105 const PNG_INTERLACING_ENABLED
= 1;
107 const PNG_CHANNEL_GRAY
= 0;
108 const PNG_CHANNEL_RGB
= 2;
109 const PNG_CHANNEL_INDEXED
= 3;
110 const PNG_CHANNEL_GRAY_ALPHA
= 4;
111 const PNG_CHANNEL_RGB_ALPHA
= 6;
113 /**** Public Interface ****/
116 /* Factory Methods */
119 * Returns a {@link Zend_Pdf_Resource_Image} object by file path.
121 * @param string $filePath Full path to the image file.
122 * @return Zend_Pdf_Resource_Image
123 * @throws Zend_Pdf_Exception
125 public static function imageWithPath($filePath)
129 * use old implementation
130 * @todo switch to new implementation
132 require_once 'Zend/Pdf/Resource/ImageFactory.php';
133 return Zend_Pdf_Resource_ImageFactory
::factory($filePath);
136 /* Create a file parser data source object for this file. File path and
137 * access permission checks are handled here.
139 $dataSource = new Zend_Pdf_FileParserDataSource_File($filePath);
141 /* Attempt to determine the type of image. We can't always trust file
142 * extensions, but try that first since it's fastest.
144 $fileExtension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION
));
146 /* If it turns out that the file is named improperly and we guess the
147 * wrong type, we'll get null instead of an image object.
149 switch ($fileExtension) {
151 //Fall through to next case;
153 $image = Zend_Pdf_Image
::_extractTiffImage($dataSource);
156 $image = Zend_Pdf_Image
::_extractPngImage($dataSource);
159 //Fall through to next case;
161 //Fall through to next case;
163 $image = Zend_Pdf_Image
::_extractJpegImage($dataSource);
166 throw new Zend_Pdf_Exception("Cannot create image resource. File extension not known or unsupported type.");
170 /* Done with the data source object.
174 if ($image !== null) {
178 /* The type of image could not be determined. Give up.
180 throw new Zend_Pdf_Exception("Cannot determine image type: $filePath",
181 Zend_Pdf_Exception
::CANT_DETERMINE_IMAGE_TYPE
);
187 /**** Internal Methods ****/
190 /* Image Extraction Methods */
193 * Attempts to extract a JPEG Image from the data source.
195 * @param Zend_Pdf_FileParserDataSource $dataSource
196 * @return Zend_Pdf_Resource_Image_Jpeg May also return null if
197 * the data source does not appear to contain valid image data.
198 * @throws Zend_Pdf_Exception
200 protected static function _extractJpegImage($dataSource)
202 $imageParser = new Zend_Pdf_FileParser_Image_Jpeg($dataSource);
203 $image = new Zend_Pdf_Resource_Image_Jpeg($imageParser);
210 * Attempts to extract a PNG Image from the data source.
212 * @param Zend_Pdf_FileParserDataSource $dataSource
213 * @return Zend_Pdf_Resource_Image_Png May also return null if
214 * the data source does not appear to contain valid image data.
215 * @throws Zend_Pdf_Exception
217 protected static function _extractPngImage($dataSource)
219 $imageParser = new Zend_Pdf_FileParser_Image_PNG($dataSource);
220 $image = new Zend_Pdf_Resource_Image_PNG($imageParser);
227 * Attempts to extract a TIFF Image from the data source.
229 * @param Zend_Pdf_FileParserDataSource $dataSource
230 * @return Zend_Pdf_Resource_Image_Tiff May also return null if
231 * the data source does not appear to contain valid image data.
232 * @throws Zend_Pdf_Exception
234 protected static function _extractTiffImage($dataSource)
236 $imageParser = new Zend_Pdf_FileParser_Image_Tiff($dataSource);
237 $image = new Zend_Pdf_Resource_Image_Tiff($imageParser);