Image.php 7.5 KB

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