File indexing completed on 2024-12-22 05:36:56

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Pdf
0017  * @subpackage Images
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 
0024 /**
0025  * Abstract factory class which vends {@link Zend_Pdf_Resource_Image} objects.
0026  *
0027  * This class is also the home for image-related constants because the name of
0028  * the true base class ({@link Zend_Pdf_Resource_Image}) is not intuitive for the
0029  * end user.
0030  *
0031  * @package    Zend_Pdf
0032  * @subpackage Images
0033  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0034  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0035  */
0036 abstract class Zend_Pdf_Image
0037 {
0038   /**** Class Constants ****/
0039 
0040 
0041   /* Image Types */
0042 
0043     const TYPE_UNKNOWN = 0;
0044     const TYPE_JPEG = 1;
0045     const TYPE_PNG = 2;
0046     const TYPE_TIFF = 3;
0047 
0048   /* TIFF Constants */
0049 
0050     const TIFF_FIELD_TYPE_BYTE=1;
0051     const TIFF_FIELD_TYPE_ASCII=2;
0052     const TIFF_FIELD_TYPE_SHORT=3;
0053     const TIFF_FIELD_TYPE_LONG=4;
0054     const TIFF_FIELD_TYPE_RATIONAL=5;
0055 
0056     const TIFF_TAG_IMAGE_WIDTH=256;
0057     const TIFF_TAG_IMAGE_LENGTH=257; //Height
0058     const TIFF_TAG_BITS_PER_SAMPLE=258;
0059     const TIFF_TAG_COMPRESSION=259;
0060     const TIFF_TAG_PHOTOMETRIC_INTERPRETATION=262;
0061     const TIFF_TAG_STRIP_OFFSETS=273;
0062     const TIFF_TAG_SAMPLES_PER_PIXEL=277;
0063     const TIFF_TAG_STRIP_BYTE_COUNTS=279;
0064 
0065     const TIFF_COMPRESSION_UNCOMPRESSED = 1;
0066     const TIFF_COMPRESSION_CCITT1D = 2;
0067     const TIFF_COMPRESSION_GROUP_3_FAX = 3;
0068     const TIFF_COMPRESSION_GROUP_4_FAX  = 4;
0069     const TIFF_COMPRESSION_LZW = 5;
0070     const TIFF_COMPRESSION_JPEG = 6;
0071     const TIFF_COMPRESSION_FLATE = 8;
0072     const TIFF_COMPRESSION_FLATE_OBSOLETE_CODE = 32946;
0073     const TIFF_COMPRESSION_PACKBITS = 32773;
0074 
0075     const TIFF_PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO=0;
0076     const TIFF_PHOTOMETRIC_INTERPRETATION_BLACK_IS_ZERO=1;
0077     const TIFF_PHOTOMETRIC_INTERPRETATION_RGB=2;
0078     const TIFF_PHOTOMETRIC_INTERPRETATION_RGB_INDEXED=3;
0079     const TIFF_PHOTOMETRIC_INTERPRETATION_CMYK=5;
0080     const TIFF_PHOTOMETRIC_INTERPRETATION_YCBCR=6;
0081     const TIFF_PHOTOMETRIC_INTERPRETATION_CIELAB=8;
0082 
0083   /* PNG Constants */
0084 
0085     const PNG_COMPRESSION_DEFAULT_STRATEGY = 0;
0086     const PNG_COMPRESSION_FILTERED = 1;
0087     const PNG_COMPRESSION_HUFFMAN_ONLY = 2;
0088     const PNG_COMPRESSION_RLE = 3;
0089 
0090     const PNG_FILTER_NONE = 0;
0091     const PNG_FILTER_SUB = 1;
0092     const PNG_FILTER_UP = 2;
0093     const PNG_FILTER_AVERAGE = 3;
0094     const PNG_FILTER_PAETH = 4;
0095 
0096     const PNG_INTERLACING_DISABLED = 0;
0097     const PNG_INTERLACING_ENABLED = 1;
0098 
0099     const PNG_CHANNEL_GRAY = 0;
0100     const PNG_CHANNEL_RGB = 2;
0101     const PNG_CHANNEL_INDEXED = 3;
0102     const PNG_CHANNEL_GRAY_ALPHA = 4;
0103     const PNG_CHANNEL_RGB_ALPHA = 6;
0104 
0105   /**** Public Interface ****/
0106 
0107 
0108   /* Factory Methods */
0109 
0110     /**
0111      * Returns a {@link Zend_Pdf_Resource_Image} object by file path.
0112      *
0113      * @param string $filePath Full path to the image file.
0114      * @return Zend_Pdf_Resource_Image
0115      * @throws Zend_Pdf_Exception
0116      */
0117     public static function imageWithPath($filePath)
0118     {
0119         /**
0120          * use old implementation
0121          * @todo switch to new implementation
0122          */
0123         // require_once 'Zend/Pdf/Resource/ImageFactory.php';
0124         return Zend_Pdf_Resource_ImageFactory::factory($filePath);
0125 
0126 
0127         /* Create a file parser data source object for this file. File path and
0128          * access permission checks are handled here.
0129          */
0130         // require_once 'Zend/Pdf/FileParserDataSource/File.php';
0131         $dataSource = new Zend_Pdf_FileParserDataSource_File($filePath);
0132 
0133         /* Attempt to determine the type of image. We can't always trust file
0134          * extensions, but try that first since it's fastest.
0135          */
0136         $fileExtension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
0137 
0138         /* If it turns out that the file is named improperly and we guess the
0139          * wrong type, we'll get null instead of an image object.
0140          */
0141         switch ($fileExtension) {
0142             case 'tif':
0143                 //Fall through to next case;
0144             case 'tiff':
0145                 $image = Zend_Pdf_Image::_extractTiffImage($dataSource);
0146                 break;
0147             case 'png':
0148                 $image = Zend_Pdf_Image::_extractPngImage($dataSource);
0149                 break;
0150             case 'jpg':
0151                 //Fall through to next case;
0152             case 'jpe':
0153                 //Fall through to next case;
0154             case 'jpeg':
0155                 $image = Zend_Pdf_Image::_extractJpegImage($dataSource);
0156                 break;
0157             default:
0158                 // require_once 'Zend/Pdf/Exception.php';
0159                 throw new Zend_Pdf_Exception("Cannot create image resource. File extension not known or unsupported type.");
0160                 break;
0161         }
0162 
0163         /* Done with the data source object.
0164          */
0165         $dataSource = null;
0166 
0167         if ($image !== null) {
0168             return $image;
0169 
0170         } else {
0171             /* The type of image could not be determined. Give up.
0172              */
0173             // require_once 'Zend/Pdf/Exception.php';
0174             throw new Zend_Pdf_Exception("Cannot determine image type: $filePath",
0175                                          Zend_Pdf_Exception::CANT_DETERMINE_IMAGE_TYPE);
0176          }
0177     }
0178 
0179 
0180 
0181     /**** Internal Methods ****/
0182 
0183 
0184     /* Image Extraction Methods */
0185 
0186     /**
0187      * Attempts to extract a JPEG Image from the data source.
0188      *
0189      * @param Zend_Pdf_FileParserDataSource $dataSource
0190      * @return Zend_Pdf_Resource_Image_Jpeg May also return null if
0191      *   the data source does not appear to contain valid image data.
0192      * @throws Zend_Pdf_Exception
0193      */
0194     protected static function _extractJpegImage($dataSource)
0195     {
0196         // require_once 'Zend/Pdf/Exception.php';
0197         throw new Zend_Pdf_Exception('Jpeg image fileparser is not implemented. Old styly implementation has to be used.');
0198 
0199         // require_once 'Zend/Pdf/FileParser/Image/Jpeg.php';
0200         $imageParser = new Zend_Pdf_FileParser_Image_Jpeg($dataSource);
0201         // require_once 'Zend/Pdf/Resource/Image/Jpeg.php';
0202         $image = new Zend_Pdf_Resource_Image_Jpeg($imageParser);
0203         unset($imageParser);
0204 
0205         return $image;
0206     }
0207 
0208     /**
0209      * Attempts to extract a PNG Image from the data source.
0210      *
0211      * @param Zend_Pdf_FileParserDataSource $dataSource
0212      * @return Zend_Pdf_Resource_Image_Png May also return null if
0213      *   the data source does not appear to contain valid image data.
0214      */
0215     protected static function _extractPngImage($dataSource)
0216     {
0217         // require_once 'Zend/Pdf/FileParser/Image/Png.php';
0218         $imageParser = new Zend_Pdf_FileParser_Image_Png($dataSource);
0219         // require_once 'Zend/Pdf/Resource/Image/Png.php';
0220         $image = new Zend_Pdf_Resource_Image_Png($imageParser);
0221         unset($imageParser);
0222 
0223         return $image;
0224     }
0225 
0226     /**
0227      * Attempts to extract a TIFF Image from the data source.
0228      *
0229      * @param Zend_Pdf_FileParserDataSource $dataSource
0230      * @return Zend_Pdf_Resource_Image_Tiff May also return null if
0231      *   the data source does not appear to contain valid image data.
0232      * @throws Zend_Pdf_Exception
0233      */
0234     protected static function _extractTiffImage($dataSource)
0235     {
0236         // require_once 'Zend/Pdf/Exception.php';
0237         throw new Zend_Pdf_Exception('Tiff image fileparser is not implemented. Old styly implementation has to be used.');
0238 
0239         // require_once 'Zend/Pdf/FileParser/Image/Tiff.php';
0240         $imageParser = new Zend_Pdf_FileParser_Image_Tiff($dataSource);
0241         // require_once 'Zend/Pdf/Resource/Image/Tiff.php';
0242         $image = new Zend_Pdf_Resource_Image_Tiff($imageParser);
0243         unset($imageParser);
0244 
0245         return $image;
0246     }
0247 }