Image.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Pdf
  17. * @subpackage Images
  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$
  21. */
  22. /** Zend_Pdf_FileParserDataSource */
  23. require_once 'Zend/Pdf/FileParserDataSource.php';
  24. /** Zend_Pdf_FileParserDataSource_File */
  25. require_once 'Zend/Pdf/FileParserDataSource/File.php';
  26. /** Zend_Pdf_FileParserDataSource_String */
  27. require_once 'Zend/Pdf/FileParserDataSource/String.php';
  28. /**
  29. * Abstract factory class which vends {@link Zend_Pdf_Resource_Image} objects.
  30. *
  31. * This class is also the home for image-related constants because the name of
  32. * the true base class ({@link Zend_Pdf_Resource_Image}) is not intuitive for the
  33. * end user.
  34. *
  35. * @package Zend_Pdf
  36. * @subpackage Images
  37. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. abstract class Zend_Pdf_Image
  41. {
  42. /**** Class Constants ****/
  43. /* Image Types */
  44. const TYPE_UNKNOWN = 0;
  45. const TYPE_JPEG = 1;
  46. const TYPE_PNG = 2;
  47. const TYPE_TIFF = 3;
  48. /* TIFF Constants */
  49. const TIFF_FIELD_TYPE_BYTE=1;
  50. const TIFF_FIELD_TYPE_ASCII=2;
  51. const TIFF_FIELD_TYPE_SHORT=3;
  52. const TIFF_FIELD_TYPE_LONG=4;
  53. const TIFF_FIELD_TYPE_RATIONAL=5;
  54. const TIFF_TAG_IMAGE_WIDTH=256;
  55. const TIFF_TAG_IMAGE_LENGTH=257; //Height
  56. const TIFF_TAG_BITS_PER_SAMPLE=258;
  57. const TIFF_TAG_COMPRESSION=259;
  58. const TIFF_TAG_PHOTOMETRIC_INTERPRETATION=262;
  59. const TIFF_TAG_STRIP_OFFSETS=273;
  60. const TIFF_TAG_SAMPLES_PER_PIXEL=277;
  61. const TIFF_TAG_STRIP_BYTE_COUNTS=279;
  62. const TIFF_COMPRESSION_UNCOMPRESSED = 1;
  63. const TIFF_COMPRESSION_CCITT1D = 2;
  64. const TIFF_COMPRESSION_GROUP_3_FAX = 3;
  65. const TIFF_COMPRESSION_GROUP_4_FAX = 4;
  66. const TIFF_COMPRESSION_LZW = 5;
  67. const TIFF_COMPRESSION_JPEG = 6;
  68. const TIFF_COMPRESSION_FLATE = 8;
  69. const TIFF_COMPRESSION_FLATE_OBSOLETE_CODE = 32946;
  70. const TIFF_COMPRESSION_PACKBITS = 32773;
  71. const TIFF_PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO=0;
  72. const TIFF_PHOTOMETRIC_INTERPRETATION_BLACK_IS_ZERO=1;
  73. const TIFF_PHOTOMETRIC_INTERPRETATION_RGB=2;
  74. const TIFF_PHOTOMETRIC_INTERPRETATION_RGB_INDEXED=3;
  75. const TIFF_PHOTOMETRIC_INTERPRETATION_CMYK=5;
  76. const TIFF_PHOTOMETRIC_INTERPRETATION_YCBCR=6;
  77. const TIFF_PHOTOMETRIC_INTERPRETATION_CIELAB=8;
  78. /* PNG Constants */
  79. const PNG_COMPRESSION_DEFAULT_STRATEGY = 0;
  80. const PNG_COMPRESSION_FILTERED = 1;
  81. const PNG_COMPRESSION_HUFFMAN_ONLY = 2;
  82. const PNG_COMPRESSION_RLE = 3;
  83. const PNG_FILTER_NONE = 0;
  84. const PNG_FILTER_SUB = 1;
  85. const PNG_FILTER_UP = 2;
  86. const PNG_FILTER_AVERAGE = 3;
  87. const PNG_FILTER_PAETH = 4;
  88. const PNG_INTERLACING_DISABLED = 0;
  89. const PNG_INTERLACING_ENABLED = 1;
  90. const PNG_CHANNEL_GRAY = 0;
  91. const PNG_CHANNEL_RGB = 2;
  92. const PNG_CHANNEL_INDEXED = 3;
  93. const PNG_CHANNEL_GRAY_ALPHA = 4;
  94. const PNG_CHANNEL_RGB_ALPHA = 6;
  95. /**** Public Interface ****/
  96. /* Factory Methods */
  97. /**
  98. * Returns a {@link Zend_Pdf_Resource_Image} object by file path.
  99. *
  100. * @param string $filePath Full path to the image file.
  101. * @return Zend_Pdf_Resource_Image
  102. * @throws Zend_Pdf_Exception
  103. */
  104. public static function imageWithPath($filePath)
  105. {
  106. /**
  107. * use old implementation
  108. * @todo switch to new implementation
  109. */
  110. require_once 'Zend/Pdf/Resource/ImageFactory.php';
  111. return Zend_Pdf_Resource_ImageFactory::factory($filePath);
  112. /* Create a file parser data source object for this file. File path and
  113. * access permission checks are handled here.
  114. */
  115. $dataSource = new Zend_Pdf_FileParserDataSource_File($filePath);
  116. /* Attempt to determine the type of image. We can't always trust file
  117. * extensions, but try that first since it's fastest.
  118. */
  119. $fileExtension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
  120. /* If it turns out that the file is named improperly and we guess the
  121. * wrong type, we'll get null instead of an image object.
  122. */
  123. switch ($fileExtension) {
  124. case 'tif':
  125. //Fall through to next case;
  126. case 'tiff':
  127. $image = Zend_Pdf_Image::_extractTiffImage($dataSource);
  128. break;
  129. case 'png':
  130. $image = Zend_Pdf_Image::_extractPngImage($dataSource);
  131. break;
  132. case 'jpg':
  133. //Fall through to next case;
  134. case 'jpe':
  135. //Fall through to next case;
  136. case 'jpeg':
  137. $image = Zend_Pdf_Image::_extractJpegImage($dataSource);
  138. break;
  139. default:
  140. throw new Zend_Pdf_Exception("Cannot create image resource. File extension not known or unsupported type.");
  141. break;
  142. }
  143. /* Done with the data source object.
  144. */
  145. $dataSource = null;
  146. if ($image !== null) {
  147. return $image;
  148. } else {
  149. /* The type of image could not be determined. Give up.
  150. */
  151. throw new Zend_Pdf_Exception("Cannot determine image type: $filePath",
  152. Zend_Pdf_Exception::CANT_DETERMINE_IMAGE_TYPE);
  153. }
  154. }
  155. /**** Internal Methods ****/
  156. /* Image Extraction Methods */
  157. /**
  158. * Attempts to extract a JPEG Image from the data source.
  159. *
  160. * @param Zend_Pdf_FileParserDataSource $dataSource
  161. * @return Zend_Pdf_Resource_Image_Jpeg May also return null if
  162. * the data source does not appear to contain valid image data.
  163. * @throws Zend_Pdf_Exception
  164. */
  165. protected static function _extractJpegImage($dataSource)
  166. {
  167. $imageParser = new Zend_Pdf_FileParser_Image_Jpeg($dataSource);
  168. $image = new Zend_Pdf_Resource_Image_Jpeg($imageParser);
  169. unset($imageParser);
  170. return $image;
  171. }
  172. /**
  173. * Attempts to extract a PNG Image from the data source.
  174. *
  175. * @param Zend_Pdf_FileParserDataSource $dataSource
  176. * @return Zend_Pdf_Resource_Image_Png May also return null if
  177. * the data source does not appear to contain valid image data.
  178. * @throws Zend_Pdf_Exception
  179. */
  180. protected static function _extractPngImage($dataSource)
  181. {
  182. $imageParser = new Zend_Pdf_FileParser_Image_PNG($dataSource);
  183. $image = new Zend_Pdf_Resource_Image_PNG($imageParser);
  184. unset($imageParser);
  185. return $image;
  186. }
  187. /**
  188. * Attempts to extract a TIFF Image from the data source.
  189. *
  190. * @param Zend_Pdf_FileParserDataSource $dataSource
  191. * @return Zend_Pdf_Resource_Image_Tiff May also return null if
  192. * the data source does not appear to contain valid image data.
  193. * @throws Zend_Pdf_Exception
  194. */
  195. protected static function _extractTiffImage($dataSource)
  196. {
  197. $imageParser = new Zend_Pdf_FileParser_Image_Tiff($dataSource);
  198. $image = new Zend_Pdf_Resource_Image_Tiff($imageParser);
  199. unset($imageParser);
  200. return $image;
  201. }
  202. }