PageRenderTime 21ms CodeModel.GetById 10ms app.highlight 8ms 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
  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
 22
 23/** Internally used classes */
 24require_once 'Zend/Pdf/Element/Name.php';
 25require_once 'Zend/Pdf/Element/Numeric.php';
 26
 27
 28/** Zend_Pdf_Resource_Image */
 29require_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 */
 38class 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        while ( $byteCount > 0 && ($nextBlock = fread($imageFile, $byteCount)) != false ) {
106            $this->_resource->value .= $nextBlock;
107            $byteCount -= strlen($nextBlock);
108        }
109        fclose($imageFile);
110        $this->_resource->skipFilters();
111
112    $this->_width = $imageInfo[0];
113    $this->_height = $imageInfo[1];
114    $this->_imageProperties = array();
115    $this->_imageProperties['bitDepth'] = $imageInfo['bits'];
116    $this->_imageProperties['jpegImageType'] = $imageInfo[2];
117    $this->_imageProperties['jpegColorType'] = $imageInfo['channels'];
118    }
119
120    /**
121     * Image width
122     */
123    public function getPixelWidth() {
124        return $this->_width;
125    }
126
127    /**
128     * Image height
129     */
130    public function getPixelHeight() {
131        return $this->_height;
132    }
133
134    /**
135     * Image properties
136     */
137    public function getProperties() {
138        return $this->_imageProperties;
139    }
140}
141