PageRenderTime 72ms CodeModel.GetById 61ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://zoop.googlecode.com/
PHP | 247 lines | 118 code | 39 blank | 90 comment | 3 complexity | c8c58ee8db0869ca27a046aa52f72ec6 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 * @subpackage Images
 18 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 * @version    $Id: Image.php 20096 2010-01-06 02:05:09Z bkarwin $
 21 */
 22
 23
 24/**
 25 * Abstract factory class which vends {@link Zend_Pdf_Resource_Image} objects.
 26 *
 27 * This class is also the home for image-related constants because the name of
 28 * the true base class ({@link Zend_Pdf_Resource_Image}) is not intuitive for the
 29 * end user.
 30 *
 31 * @package    Zend_Pdf
 32 * @subpackage Images
 33 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 34 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 35 */
 36abstract class Zend_Pdf_Image
 37{
 38  /**** Class Constants ****/
 39
 40
 41  /* Image Types */
 42
 43    const TYPE_UNKNOWN = 0;
 44    const TYPE_JPEG = 1;
 45    const TYPE_PNG = 2;
 46    const TYPE_TIFF = 3;
 47
 48  /* TIFF Constants */
 49
 50    const TIFF_FIELD_TYPE_BYTE=1;
 51    const TIFF_FIELD_TYPE_ASCII=2;
 52    const TIFF_FIELD_TYPE_SHORT=3;
 53    const TIFF_FIELD_TYPE_LONG=4;
 54    const TIFF_FIELD_TYPE_RATIONAL=5;
 55
 56    const TIFF_TAG_IMAGE_WIDTH=256;
 57    const TIFF_TAG_IMAGE_LENGTH=257; //Height
 58    const TIFF_TAG_BITS_PER_SAMPLE=258;
 59    const TIFF_TAG_COMPRESSION=259;
 60    const TIFF_TAG_PHOTOMETRIC_INTERPRETATION=262;
 61    const TIFF_TAG_STRIP_OFFSETS=273;
 62    const TIFF_TAG_SAMPLES_PER_PIXEL=277;
 63    const TIFF_TAG_STRIP_BYTE_COUNTS=279;
 64
 65    const TIFF_COMPRESSION_UNCOMPRESSED = 1;
 66    const TIFF_COMPRESSION_CCITT1D = 2;
 67    const TIFF_COMPRESSION_GROUP_3_FAX = 3;
 68    const TIFF_COMPRESSION_GROUP_4_FAX  = 4;
 69    const TIFF_COMPRESSION_LZW = 5;
 70    const TIFF_COMPRESSION_JPEG = 6;
 71    const TIFF_COMPRESSION_FLATE = 8;
 72    const TIFF_COMPRESSION_FLATE_OBSOLETE_CODE = 32946;
 73    const TIFF_COMPRESSION_PACKBITS = 32773;
 74
 75    const TIFF_PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO=0;
 76    const TIFF_PHOTOMETRIC_INTERPRETATION_BLACK_IS_ZERO=1;
 77    const TIFF_PHOTOMETRIC_INTERPRETATION_RGB=2;
 78    const TIFF_PHOTOMETRIC_INTERPRETATION_RGB_INDEXED=3;
 79    const TIFF_PHOTOMETRIC_INTERPRETATION_CMYK=5;
 80    const TIFF_PHOTOMETRIC_INTERPRETATION_YCBCR=6;
 81    const TIFF_PHOTOMETRIC_INTERPRETATION_CIELAB=8;
 82
 83  /* PNG Constants */
 84
 85    const PNG_COMPRESSION_DEFAULT_STRATEGY = 0;
 86    const PNG_COMPRESSION_FILTERED = 1;
 87    const PNG_COMPRESSION_HUFFMAN_ONLY = 2;
 88    const PNG_COMPRESSION_RLE = 3;
 89
 90    const PNG_FILTER_NONE = 0;
 91    const PNG_FILTER_SUB = 1;
 92    const PNG_FILTER_UP = 2;
 93    const PNG_FILTER_AVERAGE = 3;
 94    const PNG_FILTER_PAETH = 4;
 95
 96    const PNG_INTERLACING_DISABLED = 0;
 97    const PNG_INTERLACING_ENABLED = 1;
 98
 99    const PNG_CHANNEL_GRAY = 0;
100    const PNG_CHANNEL_RGB = 2;
101    const PNG_CHANNEL_INDEXED = 3;
102    const PNG_CHANNEL_GRAY_ALPHA = 4;
103    const PNG_CHANNEL_RGB_ALPHA = 6;
104
105  /**** Public Interface ****/
106
107
108  /* Factory Methods */
109
110    /**
111     * Returns a {@link Zend_Pdf_Resource_Image} object by file path.
112     *
113     * @param string $filePath Full path to the image file.
114     * @return Zend_Pdf_Resource_Image
115     * @throws Zend_Pdf_Exception
116     */
117    public static function imageWithPath($filePath)
118    {
119        /**
120         * use old implementation
121         * @todo switch to new implementation
122         */
123        require_once 'Zend/Pdf/Resource/ImageFactory.php';
124        return Zend_Pdf_Resource_ImageFactory::factory($filePath);
125
126
127        /* Create a file parser data source object for this file. File path and
128         * access permission checks are handled here.
129         */
130        require_once 'Zend/Pdf/FileParserDataSource/File.php';
131        $dataSource = new Zend_Pdf_FileParserDataSource_File($filePath);
132
133        /* Attempt to determine the type of image. We can't always trust file
134         * extensions, but try that first since it's fastest.
135         */
136        $fileExtension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
137
138        /* If it turns out that the file is named improperly and we guess the
139         * wrong type, we'll get null instead of an image object.
140         */
141        switch ($fileExtension) {
142            case 'tif':
143                //Fall through to next case;
144            case 'tiff':
145                $image = Zend_Pdf_Image::_extractTiffImage($dataSource);
146                break;
147            case 'png':
148                $image = Zend_Pdf_Image::_extractPngImage($dataSource);
149                break;
150            case 'jpg':
151                //Fall through to next case;
152            case 'jpe':
153                //Fall through to next case;
154            case 'jpeg':
155                $image = Zend_Pdf_Image::_extractJpegImage($dataSource);
156                break;
157            default:
158                require_once 'Zend/Pdf/Exception.php';
159                throw new Zend_Pdf_Exception("Cannot create image resource. File extension not known or unsupported type.");
160                break;
161        }
162
163        /* Done with the data source object.
164         */
165        $dataSource = null;
166
167        if ($image !== null) {
168            return $image;
169
170        } else {
171            /* The type of image could not be determined. Give up.
172             */
173            require_once 'Zend/Pdf/Exception.php';
174            throw new Zend_Pdf_Exception("Cannot determine image type: $filePath",
175                                         Zend_Pdf_Exception::CANT_DETERMINE_IMAGE_TYPE);
176         }
177    }
178
179
180
181    /**** Internal Methods ****/
182
183
184    /* Image Extraction Methods */
185
186    /**
187     * Attempts to extract a JPEG Image from the data source.
188     *
189     * @param Zend_Pdf_FileParserDataSource $dataSource
190     * @return Zend_Pdf_Resource_Image_Jpeg May also return null if
191     *   the data source does not appear to contain valid image data.
192     * @throws Zend_Pdf_Exception
193     */
194    protected static function _extractJpegImage($dataSource)
195    {
196        require_once 'Zend/Pdf/Exception.php';
197        throw new Zend_Pdf_Exception('Jpeg image fileparser is not implemented. Old styly implementation has to be used.');
198
199        require_once 'Zend/Pdf/FileParser/Image/Jpeg.php';
200        $imageParser = new Zend_Pdf_FileParser_Image_Jpeg($dataSource);
201        require_once 'Zend/Pdf/Resource/Image/Jpeg.php';
202        $image = new Zend_Pdf_Resource_Image_Jpeg($imageParser);
203        unset($imageParser);
204
205        return $image;
206    }
207
208    /**
209     * Attempts to extract a PNG Image from the data source.
210     *
211     * @param Zend_Pdf_FileParserDataSource $dataSource
212     * @return Zend_Pdf_Resource_Image_Png May also return null if
213     *   the data source does not appear to contain valid image data.
214     */
215    protected static function _extractPngImage($dataSource)
216    {
217        require_once 'Zend/Pdf/FileParser/Image/Png.php';
218        $imageParser = new Zend_Pdf_FileParser_Image_Png($dataSource);
219        require_once 'Zend/Pdf/Resource/Image/Png.php';
220        $image = new Zend_Pdf_Resource_Image_Png($imageParser);
221        unset($imageParser);
222
223        return $image;
224    }
225
226    /**
227     * Attempts to extract a TIFF Image from the data source.
228     *
229     * @param Zend_Pdf_FileParserDataSource $dataSource
230     * @return Zend_Pdf_Resource_Image_Tiff May also return null if
231     *   the data source does not appear to contain valid image data.
232     * @throws Zend_Pdf_Exception
233     */
234    protected static function _extractTiffImage($dataSource)
235    {
236        require_once 'Zend/Pdf/Exception.php';
237        throw new Zend_Pdf_Exception('Tiff image fileparser is not implemented. Old styly implementation has to be used.');
238
239        require_once 'Zend/Pdf/FileParser/Image/Tiff.php';
240        $imageParser = new Zend_Pdf_FileParser_Image_Tiff($dataSource);
241        require_once 'Zend/Pdf/Resource/Image/Tiff.php';
242        $image = new Zend_Pdf_Resource_Image_Tiff($imageParser);
243        unset($imageParser);
244
245        return $image;
246    }
247}