2
0

Jpeg.php 4.4 KB

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