You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
8.1 KiB

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