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