PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Pdf/Resource/Image/Jpeg.php

https://github.com/kervin/kyzstudio
PHP | 152 lines | 79 code | 20 blank | 53 comment | 26 complexity | 5c733863b397860e4844f91b95aba02f 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: Jpeg.php 23395 2010-11-19 15:30:47Z alexander $
  20. */
  21. /** Internally used classes */
  22. #require_once 'Zend/Pdf/Element/Name.php';
  23. #require_once 'Zend/Pdf/Element/Numeric.php';
  24. /** Zend_Pdf_Resource_Image */
  25. #require_once 'Zend/Pdf/Resource/Image.php';
  26. /**
  27. * JPEG image
  28. *
  29. * @package Zend_Pdf
  30. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Pdf_Resource_Image_Jpeg extends Zend_Pdf_Resource_Image
  34. {
  35. protected $_width;
  36. protected $_height;
  37. protected $_imageProperties;
  38. /**
  39. * Object constructor
  40. *
  41. * @param string $imageFileName
  42. * @throws Zend_Pdf_Exception
  43. */
  44. public function __construct($imageFileName)
  45. {
  46. if (!function_exists('gd_info')) {
  47. #require_once 'Zend/Pdf/Exception.php';
  48. throw new Zend_Pdf_Exception('Image extension is not installed.');
  49. }
  50. $gd_options = gd_info();
  51. if ( (!isset($gd_options['JPG Support']) || $gd_options['JPG Support'] != true) &&
  52. (!isset($gd_options['JPEG Support']) || $gd_options['JPEG Support'] != true) ) {
  53. #require_once 'Zend/Pdf/Exception.php';
  54. throw new Zend_Pdf_Exception('JPG support is not configured properly.');
  55. }
  56. if (($imageInfo = getimagesize($imageFileName)) === false) {
  57. #require_once 'Zend/Pdf/Exception.php';
  58. throw new Zend_Pdf_Exception('Corrupted image or image doesn\'t exist.');
  59. }
  60. if ($imageInfo[2] != IMAGETYPE_JPEG && $imageInfo[2] != IMAGETYPE_JPEG2000) {
  61. #require_once 'Zend/Pdf/Exception.php';
  62. throw new Zend_Pdf_Exception('ImageType is not JPG');
  63. }
  64. parent::__construct();
  65. switch ($imageInfo['channels']) {
  66. case 3:
  67. $colorSpace = 'DeviceRGB';
  68. break;
  69. case 4:
  70. $colorSpace = 'DeviceCMYK';
  71. break;
  72. default:
  73. $colorSpace = 'DeviceGray';
  74. break;
  75. }
  76. $imageDictionary = $this->_resource->dictionary;
  77. $imageDictionary->Width = new Zend_Pdf_Element_Numeric($imageInfo[0]);
  78. $imageDictionary->Height = new Zend_Pdf_Element_Numeric($imageInfo[1]);
  79. $imageDictionary->ColorSpace = new Zend_Pdf_Element_Name($colorSpace);
  80. $imageDictionary->BitsPerComponent = new Zend_Pdf_Element_Numeric($imageInfo['bits']);
  81. if ($imageInfo[2] == IMAGETYPE_JPEG) {
  82. $imageDictionary->Filter = new Zend_Pdf_Element_Name('DCTDecode');
  83. } else if ($imageInfo[2] == IMAGETYPE_JPEG2000){
  84. $imageDictionary->Filter = new Zend_Pdf_Element_Name('JPXDecode');
  85. }
  86. if (($imageFile = @fopen($imageFileName, 'rb')) === false ) {
  87. #require_once 'Zend/Pdf/Exception.php';
  88. throw new Zend_Pdf_Exception( "Can not open '$imageFileName' file for reading." );
  89. }
  90. $byteCount = filesize($imageFileName);
  91. $this->_resource->value = '';
  92. while ($byteCount > 0 && !feof($imageFile)) {
  93. $nextBlock = fread($imageFile, $byteCount);
  94. if ($nextBlock === false) {
  95. #require_once 'Zend/Pdf/Exception.php';
  96. throw new Zend_Pdf_Exception( "Error occured while '$imageFileName' file reading." );
  97. }
  98. $this->_resource->value .= $nextBlock;
  99. $byteCount -= strlen($nextBlock);
  100. }
  101. if ($byteCount != 0) {
  102. #require_once 'Zend/Pdf/Exception.php';
  103. throw new Zend_Pdf_Exception( "Error occured while '$imageFileName' file reading." );
  104. }
  105. fclose($imageFile);
  106. $this->_resource->skipFilters();
  107. $this->_width = $imageInfo[0];
  108. $this->_height = $imageInfo[1];
  109. $this->_imageProperties = array();
  110. $this->_imageProperties['bitDepth'] = $imageInfo['bits'];
  111. $this->_imageProperties['jpegImageType'] = $imageInfo[2];
  112. $this->_imageProperties['jpegColorType'] = $imageInfo['channels'];
  113. }
  114. /**
  115. * Image width
  116. */
  117. public function getPixelWidth() {
  118. return $this->_width;
  119. }
  120. /**
  121. * Image height
  122. */
  123. public function getPixelHeight() {
  124. return $this->_height;
  125. }
  126. /**
  127. * Image properties
  128. */
  129. public function getProperties() {
  130. return $this->_imageProperties;
  131. }
  132. }