PageRenderTime 31ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/vendor/zend/Zend/Pdf/Resource/Image/Jpeg.php

http://zoop.googlecode.com/
PHP | 141 lines | 80 code | 18 blank | 43 comment | 22 complexity | a9963662d921d2e98ce3c762b98c69e9 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  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 20096 2010-01-06 02:05:09Z bkarwin $
  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 && ($nextBlock = fread($imageFile, $byteCount)) != false ) {
  93. $this->_resource->value .= $nextBlock;
  94. $byteCount -= strlen($nextBlock);
  95. }
  96. fclose($imageFile);
  97. $this->_resource->skipFilters();
  98. $this->_width = $imageInfo[0];
  99. $this->_height = $imageInfo[1];
  100. $this->_imageProperties = array();
  101. $this->_imageProperties['bitDepth'] = $imageInfo['bits'];
  102. $this->_imageProperties['jpegImageType'] = $imageInfo[2];
  103. $this->_imageProperties['jpegColorType'] = $imageInfo['channels'];
  104. }
  105. /**
  106. * Image width
  107. */
  108. public function getPixelWidth() {
  109. return $this->_width;
  110. }
  111. /**
  112. * Image height
  113. */
  114. public function getPixelHeight() {
  115. return $this->_height;
  116. }
  117. /**
  118. * Image properties
  119. */
  120. public function getProperties() {
  121. return $this->_imageProperties;
  122. }
  123. }