PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/website/library/Zend/Pdf/Resource/Image/Png.php

https://bitbucket.org/efdac/e-forest_platform
PHP | 374 lines | 242 code | 51 blank | 81 comment | 21 complexity | 8052cbf7c403ac1400589b935e195279 MD5 | raw file
  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. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Png.php 20096 2010-01-06 02:05:09Z bkarwin $
  20. */
  21. /** Internally used classes */
  22. require_once 'Zend/Pdf/Element/Array.php';
  23. require_once 'Zend/Pdf/Element/Dictionary.php';
  24. require_once 'Zend/Pdf/Element/Name.php';
  25. require_once 'Zend/Pdf/Element/Numeric.php';
  26. require_once 'Zend/Pdf/Element/String/Binary.php';
  27. /** Zend_Pdf_Resource_Image */
  28. require_once 'Zend/Pdf/Resource/Image.php';
  29. /**
  30. * PNG image
  31. *
  32. * @package Zend_Pdf
  33. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Pdf_Resource_Image_Png extends Zend_Pdf_Resource_Image
  37. {
  38. const PNG_COMPRESSION_DEFAULT_STRATEGY = 0;
  39. const PNG_COMPRESSION_FILTERED = 1;
  40. const PNG_COMPRESSION_HUFFMAN_ONLY = 2;
  41. const PNG_COMPRESSION_RLE = 3;
  42. const PNG_FILTER_NONE = 0;
  43. const PNG_FILTER_SUB = 1;
  44. const PNG_FILTER_UP = 2;
  45. const PNG_FILTER_AVERAGE = 3;
  46. const PNG_FILTER_PAETH = 4;
  47. const PNG_INTERLACING_DISABLED = 0;
  48. const PNG_INTERLACING_ENABLED = 1;
  49. const PNG_CHANNEL_GRAY = 0;
  50. const PNG_CHANNEL_RGB = 2;
  51. const PNG_CHANNEL_INDEXED = 3;
  52. const PNG_CHANNEL_GRAY_ALPHA = 4;
  53. const PNG_CHANNEL_RGB_ALPHA = 6;
  54. protected $_width;
  55. protected $_height;
  56. protected $_imageProperties;
  57. /**
  58. * Object constructor
  59. *
  60. * @param string $imageFileName
  61. * @throws Zend_Pdf_Exception
  62. * @todo Add compression conversions to support compression strategys other than PNG_COMPRESSION_DEFAULT_STRATEGY.
  63. * @todo Add pre-compression filtering.
  64. * @todo Add interlaced image handling.
  65. * @todo Add support for 16-bit images. Requires PDF version bump to 1.5 at least.
  66. * @todo Add processing for all PNG chunks defined in the spec. gAMA etc.
  67. * @todo Fix tRNS chunk support for Indexed Images to a SMask.
  68. */
  69. public function __construct($imageFileName)
  70. {
  71. if (($imageFile = @fopen($imageFileName, 'rb')) === false ) {
  72. require_once 'Zend/Pdf/Exception.php';
  73. throw new Zend_Pdf_Exception( "Can not open '$imageFileName' file for reading." );
  74. }
  75. parent::__construct();
  76. //Check if the file is a PNG
  77. fseek($imageFile, 1, SEEK_CUR); //First signature byte (%)
  78. if ('PNG' != fread($imageFile, 3)) {
  79. require_once 'Zend/Pdf/Exception.php';
  80. throw new Zend_Pdf_Exception('Image is not a PNG');
  81. }
  82. fseek($imageFile, 12, SEEK_CUR); //Signature bytes (Includes the IHDR chunk) IHDR processed linerarly because it doesnt contain a variable chunk length
  83. $wtmp = unpack('Ni',fread($imageFile, 4)); //Unpack a 4-Byte Long
  84. $width = $wtmp['i'];
  85. $htmp = unpack('Ni',fread($imageFile, 4));
  86. $height = $htmp['i'];
  87. $bits = ord(fread($imageFile, 1)); //Higher than 8 bit depths are only supported in later versions of PDF.
  88. $color = ord(fread($imageFile, 1));
  89. $compression = ord(fread($imageFile, 1));
  90. $prefilter = ord(fread($imageFile,1));
  91. if (($interlacing = ord(fread($imageFile,1))) != Zend_Pdf_Resource_Image_Png::PNG_INTERLACING_DISABLED) {
  92. require_once 'Zend/Pdf/Exception.php';
  93. throw new Zend_Pdf_Exception( "Only non-interlaced images are currently supported." );
  94. }
  95. $this->_width = $width;
  96. $this->_height = $height;
  97. $this->_imageProperties = array();
  98. $this->_imageProperties['bitDepth'] = $bits;
  99. $this->_imageProperties['pngColorType'] = $color;
  100. $this->_imageProperties['pngFilterType'] = $prefilter;
  101. $this->_imageProperties['pngCompressionType'] = $compression;
  102. $this->_imageProperties['pngInterlacingType'] = $interlacing;
  103. fseek($imageFile, 4, SEEK_CUR); //4 Byte Ending Sequence
  104. $imageData = '';
  105. /*
  106. * The following loop processes PNG chunks. 4 Byte Longs are packed first give the chunk length
  107. * followed by the chunk signature, a four byte code. IDAT and IEND are manditory in any PNG.
  108. */
  109. while(($chunkLengthBytes = fread($imageFile, 4)) !== false) {
  110. $chunkLengthtmp = unpack('Ni', $chunkLengthBytes);
  111. $chunkLength = $chunkLengthtmp['i'];
  112. $chunkType = fread($imageFile, 4);
  113. switch($chunkType) {
  114. case 'IDAT': //Image Data
  115. /*
  116. * Reads the actual image data from the PNG file. Since we know at this point that the compression
  117. * strategy is the default strategy, we also know that this data is Zip compressed. We will either copy
  118. * the data directly to the PDF and provide the correct FlateDecode predictor, or decompress the data
  119. * decode the filters and output the data as a raw pixel map.
  120. */
  121. $imageData .= fread($imageFile, $chunkLength);
  122. fseek($imageFile, 4, SEEK_CUR);
  123. break;
  124. case 'PLTE': //Palette
  125. $paletteData = fread($imageFile, $chunkLength);
  126. fseek($imageFile, 4, SEEK_CUR);
  127. break;
  128. case 'tRNS': //Basic (non-alpha channel) transparency.
  129. $trnsData = fread($imageFile, $chunkLength);
  130. switch ($color) {
  131. case Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_GRAY:
  132. $baseColor = ord(substr($trnsData, 1, 1));
  133. $transparencyData = array(new Zend_Pdf_Element_Numeric($baseColor),
  134. new Zend_Pdf_Element_Numeric($baseColor));
  135. break;
  136. case Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_RGB:
  137. $red = ord(substr($trnsData,1,1));
  138. $green = ord(substr($trnsData,3,1));
  139. $blue = ord(substr($trnsData,5,1));
  140. $transparencyData = array(new Zend_Pdf_Element_Numeric($red),
  141. new Zend_Pdf_Element_Numeric($red),
  142. new Zend_Pdf_Element_Numeric($green),
  143. new Zend_Pdf_Element_Numeric($green),
  144. new Zend_Pdf_Element_Numeric($blue),
  145. new Zend_Pdf_Element_Numeric($blue));
  146. break;
  147. case Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_INDEXED:
  148. //Find the first transparent color in the index, we will mask that. (This is a bit of a hack. This should be a SMask and mask all entries values).
  149. if(($trnsIdx = strpos($trnsData, chr(0))) !== false) {
  150. $transparencyData = array(new Zend_Pdf_Element_Numeric($trnsIdx),
  151. new Zend_Pdf_Element_Numeric($trnsIdx));
  152. }
  153. break;
  154. case Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_GRAY_ALPHA:
  155. // Fall through to the next case
  156. case Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_RGB_ALPHA:
  157. require_once 'Zend/Pdf/Exception.php';
  158. throw new Zend_Pdf_Exception( "tRNS chunk illegal for Alpha Channel Images" );
  159. break;
  160. }
  161. fseek($imageFile, 4, SEEK_CUR); //4 Byte Ending Sequence
  162. break;
  163. case 'IEND';
  164. break 2; //End the loop too
  165. default:
  166. fseek($imageFile, $chunkLength + 4, SEEK_CUR); //Skip the section
  167. break;
  168. }
  169. }
  170. fclose($imageFile);
  171. $compressed = true;
  172. $imageDataTmp = '';
  173. $smaskData = '';
  174. switch ($color) {
  175. case Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_RGB:
  176. $colorSpace = new Zend_Pdf_Element_Name('DeviceRGB');
  177. break;
  178. case Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_GRAY:
  179. $colorSpace = new Zend_Pdf_Element_Name('DeviceGray');
  180. break;
  181. case Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_INDEXED:
  182. if(empty($paletteData)) {
  183. require_once 'Zend/Pdf/Exception.php';
  184. throw new Zend_Pdf_Exception( "PNG Corruption: No palette data read for indexed type PNG." );
  185. }
  186. $colorSpace = new Zend_Pdf_Element_Array();
  187. $colorSpace->items[] = new Zend_Pdf_Element_Name('Indexed');
  188. $colorSpace->items[] = new Zend_Pdf_Element_Name('DeviceRGB');
  189. $colorSpace->items[] = new Zend_Pdf_Element_Numeric((strlen($paletteData)/3-1));
  190. $paletteObject = $this->_objectFactory->newObject(new Zend_Pdf_Element_String_Binary($paletteData));
  191. $colorSpace->items[] = $paletteObject;
  192. break;
  193. case Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_GRAY_ALPHA:
  194. /*
  195. * To decode PNG's with alpha data we must create two images from one. One image will contain the Gray data
  196. * the other will contain the Gray transparency overlay data. The former will become the object data and the latter
  197. * will become the Shadow Mask (SMask).
  198. */
  199. if($bits > 8) {
  200. require_once 'Zend/Pdf/Exception.php';
  201. throw new Zend_Pdf_Exception("Alpha PNGs with bit depth > 8 are not yet supported");
  202. }
  203. $colorSpace = new Zend_Pdf_Element_Name('DeviceGray');
  204. require_once 'Zend/Pdf/ElementFactory.php';
  205. $decodingObjFactory = Zend_Pdf_ElementFactory::createFactory(1);
  206. $decodingStream = $decodingObjFactory->newStreamObject($imageData);
  207. $decodingStream->dictionary->Filter = new Zend_Pdf_Element_Name('FlateDecode');
  208. $decodingStream->dictionary->DecodeParms = new Zend_Pdf_Element_Dictionary();
  209. $decodingStream->dictionary->DecodeParms->Predictor = new Zend_Pdf_Element_Numeric(15);
  210. $decodingStream->dictionary->DecodeParms->Columns = new Zend_Pdf_Element_Numeric($width);
  211. $decodingStream->dictionary->DecodeParms->Colors = new Zend_Pdf_Element_Numeric(2); //GreyAlpha
  212. $decodingStream->dictionary->DecodeParms->BitsPerComponent = new Zend_Pdf_Element_Numeric($bits);
  213. $decodingStream->skipFilters();
  214. $pngDataRawDecoded = $decodingStream->value;
  215. //Iterate every pixel and copy out gray data and alpha channel (this will be slow)
  216. for($pixel = 0, $pixelcount = ($width * $height); $pixel < $pixelcount; $pixel++) {
  217. $imageDataTmp .= $pngDataRawDecoded[($pixel*2)];
  218. $smaskData .= $pngDataRawDecoded[($pixel*2)+1];
  219. }
  220. $compressed = false;
  221. $imageData = $imageDataTmp; //Overwrite image data with the gray channel without alpha
  222. break;
  223. case Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_RGB_ALPHA:
  224. /*
  225. * To decode PNG's with alpha data we must create two images from one. One image will contain the RGB data
  226. * the other will contain the Gray transparency overlay data. The former will become the object data and the latter
  227. * will become the Shadow Mask (SMask).
  228. */
  229. if($bits > 8) {
  230. require_once 'Zend/Pdf/Exception.php';
  231. throw new Zend_Pdf_Exception("Alpha PNGs with bit depth > 8 are not yet supported");
  232. }
  233. $colorSpace = new Zend_Pdf_Element_Name('DeviceRGB');
  234. require_once 'Zend/Pdf/ElementFactory.php';
  235. $decodingObjFactory = Zend_Pdf_ElementFactory::createFactory(1);
  236. $decodingStream = $decodingObjFactory->newStreamObject($imageData);
  237. $decodingStream->dictionary->Filter = new Zend_Pdf_Element_Name('FlateDecode');
  238. $decodingStream->dictionary->DecodeParms = new Zend_Pdf_Element_Dictionary();
  239. $decodingStream->dictionary->DecodeParms->Predictor = new Zend_Pdf_Element_Numeric(15);
  240. $decodingStream->dictionary->DecodeParms->Columns = new Zend_Pdf_Element_Numeric($width);
  241. $decodingStream->dictionary->DecodeParms->Colors = new Zend_Pdf_Element_Numeric(4); //RGBA
  242. $decodingStream->dictionary->DecodeParms->BitsPerComponent = new Zend_Pdf_Element_Numeric($bits);
  243. $decodingStream->skipFilters();
  244. $pngDataRawDecoded = $decodingStream->value;
  245. //Iterate every pixel and copy out rgb data and alpha channel (this will be slow)
  246. for($pixel = 0, $pixelcount = ($width * $height); $pixel < $pixelcount; $pixel++) {
  247. $imageDataTmp .= $pngDataRawDecoded[($pixel*4)+0] . $pngDataRawDecoded[($pixel*4)+1] . $pngDataRawDecoded[($pixel*4)+2];
  248. $smaskData .= $pngDataRawDecoded[($pixel*4)+3];
  249. }
  250. $compressed = false;
  251. $imageData = $imageDataTmp; //Overwrite image data with the RGB channel without alpha
  252. break;
  253. default:
  254. require_once 'Zend/Pdf/Exception.php';
  255. throw new Zend_Pdf_Exception( "PNG Corruption: Invalid color space." );
  256. }
  257. if(empty($imageData)) {
  258. require_once 'Zend/Pdf/Exception.php';
  259. throw new Zend_Pdf_Exception( "Corrupt PNG Image. Mandatory IDAT chunk not found." );
  260. }
  261. $imageDictionary = $this->_resource->dictionary;
  262. if(!empty($smaskData)) {
  263. /*
  264. * Includes the Alpha transparency data as a Gray Image, then assigns the image as the Shadow Mask for the main image data.
  265. */
  266. $smaskStream = $this->_objectFactory->newStreamObject($smaskData);
  267. $smaskStream->dictionary->Type = new Zend_Pdf_Element_Name('XObject');
  268. $smaskStream->dictionary->Subtype = new Zend_Pdf_Element_Name('Image');
  269. $smaskStream->dictionary->Width = new Zend_Pdf_Element_Numeric($width);
  270. $smaskStream->dictionary->Height = new Zend_Pdf_Element_Numeric($height);
  271. $smaskStream->dictionary->ColorSpace = new Zend_Pdf_Element_Name('DeviceGray');
  272. $smaskStream->dictionary->BitsPerComponent = new Zend_Pdf_Element_Numeric($bits);
  273. $imageDictionary->SMask = $smaskStream;
  274. // Encode stream with FlateDecode filter
  275. $smaskStreamDecodeParms = array();
  276. $smaskStreamDecodeParms['Predictor'] = new Zend_Pdf_Element_Numeric(15);
  277. $smaskStreamDecodeParms['Columns'] = new Zend_Pdf_Element_Numeric($width);
  278. $smaskStreamDecodeParms['Colors'] = new Zend_Pdf_Element_Numeric(1);
  279. $smaskStreamDecodeParms['BitsPerComponent'] = new Zend_Pdf_Element_Numeric(8);
  280. $smaskStream->dictionary->DecodeParms = new Zend_Pdf_Element_Dictionary($smaskStreamDecodeParms);
  281. $smaskStream->dictionary->Filter = new Zend_Pdf_Element_Name('FlateDecode');
  282. }
  283. if(!empty($transparencyData)) {
  284. //This is experimental and not properly tested.
  285. $imageDictionary->Mask = new Zend_Pdf_Element_Array($transparencyData);
  286. }
  287. $imageDictionary->Width = new Zend_Pdf_Element_Numeric($width);
  288. $imageDictionary->Height = new Zend_Pdf_Element_Numeric($height);
  289. $imageDictionary->ColorSpace = $colorSpace;
  290. $imageDictionary->BitsPerComponent = new Zend_Pdf_Element_Numeric($bits);
  291. $imageDictionary->Filter = new Zend_Pdf_Element_Name('FlateDecode');
  292. $decodeParms = array();
  293. $decodeParms['Predictor'] = new Zend_Pdf_Element_Numeric(15); // Optimal prediction
  294. $decodeParms['Columns'] = new Zend_Pdf_Element_Numeric($width);
  295. $decodeParms['Colors'] = new Zend_Pdf_Element_Numeric((($color==Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_RGB || $color==Zend_Pdf_Resource_Image_Png::PNG_CHANNEL_RGB_ALPHA)?(3):(1)));
  296. $decodeParms['BitsPerComponent'] = new Zend_Pdf_Element_Numeric($bits);
  297. $imageDictionary->DecodeParms = new Zend_Pdf_Element_Dictionary($decodeParms);
  298. //Include only the image IDAT section data.
  299. $this->_resource->value = $imageData;
  300. //Skip double compression
  301. if ($compressed) {
  302. $this->_resource->skipFilters();
  303. }
  304. }
  305. /**
  306. * Image width
  307. */
  308. public function getPixelWidth() {
  309. return $this->_width;
  310. }
  311. /**
  312. * Image height
  313. */
  314. public function getPixelHeight() {
  315. return $this->_height;
  316. }
  317. /**
  318. * Image properties
  319. */
  320. public function getProperties() {
  321. return $this->_imageProperties;
  322. }
  323. }