the new api looks like this..

$image = Zend_Pdf_Image::imageWithPath($pathtofile);

It will use the new parsers and the filename (should be mime detecting) to determine what Zend_Pdf_FileParser_Image derived object to use on a Zend_Pdf_FileParserDataSource_File resource.. It was done this way such that an imageFromBinaryString() or imageFromUrl() static methods could be added and not care if the data is in a file or a stream or whatever.

The other two old parsers jpeg + tiff require that they know the filename (jpeg because it passes it off to gd) and the tiff because I haven't gotten around to rewriting the parser to work with the new architecture.

It would likely be harder to get the old stuff to fit in the new architecture than to just rewrite the file parsers to the new format (though jpeg might be hard because no one actually ever wrote a proper jpeg parser)....

In the patch i passed along, the new classes of importance are..

/Pdf/Resource/Image/PNG.php (note uppercase)
class Zend_Pdf_Resource_Image_PNG extends Zend_Pdf_Resource_Image
   public function __construct(Zend_Pdf_FileParser_Image_PNG $imageParser)
...

/Pdf/Image.php
abstract class Zend_Pdf_Image
   public static function imageWithPath($filePath)
   protected static function _extractJpegImage($dataSource)
   protected static function _extractPngImage($dataSource)
   protected static function _extractTiffImage($dataSource)

/Pdf/FileParser.php
abstract class Zend_Pdf_FileParser
   abstract public function screen();
   abstract public function parse();
   public function __construct(Zend_Pdf_FileParserDataSource $dataSource)

/Pdf/FileParserDataSource/File.php
/Pdf/FileParserDataSource/String.php
//Todo: url.php, stream.php

class Zend_Pdf_FileParserDataSource_File extends Zend_Pdf_FileParserDataSource
   public function __construct($filePath)
class Zend_Pdf_FileParserDataSource_String extends Zend_Pdf_FileParserDataSource
   public function __construct($string)


/Pdf/FileParser/Image.php
abstract class Zend_Pdf_FileParser_Image extends Zend_Pdf_FileParser
   public function __construct(Zend_Pdf_FileParserDataSource $dataSource)


/Pdf/FileParser/Image/PNG.php
class Zend_Pdf_FileParser_Image_PNG extends Zend_Pdf_FileParser_Image

   public function parse()
   public function getWidth()
   public function getHeight()
   public function getBitDepth()
   etc.


What needs to be done is

/Pdf/FileParser/Image/Jpeg.php
/Pdf/FileParser/Image/Tiff.php
/Pdf/Resource/Image/Jpeg.php
/Pdf/Resource/Image/Tiff.php

Following the png example already created.

The old Resource files must die.

There should also probably be a Zend_Pdf_FileParser_Image_Interface created to handle the common methods created in Zend_Pdf_FileParser_Image_PNG

Its a fairly labourous peice of work, but isn't highly technical (except maybe the jpeg parser to factor out gd)

If this gets translated to a common api, everything except for the Resource* classes would get shifted out of /Pdf/ entirely

Meaning, Zend_Pdf_FileParser* would need to be relocated and re-mapped elsewhere -- as for PDF using the new parsers, its fairly trivial because they operate over a standard api...calling things like $this->_width = $imageParser->getWidth(); and such. The new resource classes dont parse any binary data, but instead only handle the imagedata->pdf translation of the image.

Hope that helps..


Kevin












----- Original Message ----- From: "Alexander Veremyev" <[EMAIL PROTECTED]>
To: "Kevin McArthur" <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>; "Zend Framework General" <fw-general@lists.zend.com>
Sent: Tuesday, April 03, 2007 2:44 PM
Subject: Re: [fw-general] Zend_Parser


Kevin McArthur wrote:
The factory is going, but the other parsers arent ready so changing the api right now would break jpeg/tiff support and leave only png.

Could we add only an API and use old implementation through this API? (So it will work as wrapper)

I assume matt's GIF parser can extract the bitmap data from the gif, which should then be embeddable in the doc (similar how i had to parse png data)

Yes, that what should be done.


With best regards,
   Alexander Veremyev.


K
----- Original Message ----- From: "Alexander Veremyev" <[EMAIL PROTECTED]>
To: "Kevin McArthur" <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>; "Zend Framework General" <fw-general@lists.zend.com>
Sent: Tuesday, April 03, 2007 6:41 AM
Subject: Re: [fw-general] Zend_Parser


Hi Kevin,

GIF support would be good feature. The difficulty is that GIF is not supported by PDF as internal format. So it needs a little bit more processing.


There was no any activities in Zend_Pdf for some time. So your patch could be applied to current SVN version. (I have some strange problems with applying this patch with 'patch' utility, but I checked it manually).


I agree ZF-11 can be postponed up to generalized parser support.

The important thing is to fix user API.
So if we are planing to use something else than 'Zend_Pdf_ImageFactory::factory()', it would be good to add it now.
Even if it works only as wrapper to current implementation.


With best regards,
   Alexander Veremyev.

Kevin McArthur wrote:
Matthew,

Feel like interfacing that with zend_pdf?

We have JPEG/PNG/TIFF supprt though only PNG has been translated to the new parser format. Gif support would be cool.

Alex

Can you explain what you've updated in ZPDF lately, my patch is out of date. Seems like its been reorganized and the parsers moved to Pdf/Resource/Image but they havnt been modernized at the same time.....

Here's my patch so far (just png support) this is the current status of ZF-11 http://framework.zend.com/issues/browse/ZF-11

Maybe we should end ZF-11 and start thinking about a generalized file parser, and then integrating that into pdf.

I hope the patch below will apply cleanly to a recent version but it was written long ago. (Should be applied in /Zend/Library/Pdf/)

Kevin
---- SNIP ----
Index: FileParser.php
===================================================================
--- FileParser.php      (revision 4305)
+++ FileParser.php      (working copy)
@@ -178,6 +178,14 @@
        $this->_dataSource->moveToOffset($offset);
    }

+    public function getOffset() {
+       return $this->_dataSource->getOffset();
+    }
+
+    public function getSize() {
+       return $this->_dataSource->getSize();
+    }
+
    /**
* Convenience wrapper for the data source object's readBytes() method.
     *
Index: Exception.php
===================================================================
--- Exception.php       (revision 4305)
+++ Exception.php       (working copy)
@@ -321,7 +321,6 @@
    const CANT_DETERMINE_FONT_TYPE = 0x0602;


-
  /* Text Layout System */

    /**
@@ -330,4 +329,12 @@
    const BAD_ATTRIBUTE_VALUE = 0x0701;


+  /* Zend_Pdf_Image and Subclasses */
+
+    const CANT_DETERMINE_IMAGE_TYPE = 0x0801;
+    const WRONG_IMAGE_TYPE = 0x0802;
+    const UNSUPPORTED_IMAGE_ENCODING_OPTIONS = 0x0803;
+    const IMAGE_FILE_CORRUPT = 0x0804;
+
+
}
Index: Resource/ImageFactory.php
===================================================================
--- Resource/ImageFactory.php   (revision 4305)
+++ Resource/ImageFactory.php   (working copy)
@@ -1,68 +0,0 @@
-<?php
-/**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to [EMAIL PROTECTED] so we can send you a copy immediately.
- *
- * @package    Zend_Pdf
- * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com) - * @license http://framework.zend.com/license/new-bsd New BSD License
- */
-
-
-/** Zend_Pdf */
-require_once 'Zend/Pdf.php';
-
-
-/**
- * Zend_Pdf_ImageFactory
- *
- * Helps manage the diverse set of supported image file types.
- *
- * @package    Zend_Pdf
- * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com) - * @license http://framework.zend.com/license/new-bsd New BSD License - * @todo Use Zend_Mime not file extension for type determination.
- */
-class Zend_Pdf_ImageFactory
-{
-    public static function factory($filename) {
-        if(!is_file($filename)) {
- throw new Zend_Pdf_Exception("Cannot create image resource. File not found.");
-        }
-        $extension = pathinfo($filename, PATHINFO_EXTENSION);
-        /*
- * There are plans to use Zend_Mime and not file extension. In the mean time, if you need to - * use an alternate file extension just spin up the right processor directly.
-         */
-        switch (strtolower($extension)) {
-            case 'tif':
-                //Fall through to next case;
-            case 'tiff':
-                return new Zend_Pdf_Image_Tiff($filename);
-                break;
-            case 'png':
-                return new Zend_Pdf_Image_Png($filename);
-                break;
-            case 'jpg':
-                //Fall through to next case;
-            case 'jpe':
-                //Fall through to next case;
-            case 'jpeg':
-                return new Zend_Pdf_Image_Jpeg($filename);
-                break;
-            default:
- throw new Zend_Pdf_Exception("Cannot create image resource. File extension not known or unsupported type.");
-                break;
-        }
-    }
-}
-?>
Index: Resource/Image.php
===================================================================
--- Resource/Image.php  (revision 4305)
+++ Resource/Image.php  (working copy)
@@ -45,7 +45,7 @@
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License
 */
-abstract class Zend_Pdf_Image extends Zend_Pdf_Resource
+abstract class Zend_Pdf_Resource_Image extends Zend_Pdf_Resource
{
    /**
     * Object constructor.
Index: FileParser/Image/PNG.php
===================================================================
--- FileParser/Image/PNG.php    (revision 0)
+++ FileParser/Image/PNG.php    (revision 0)
@@ -0,0 +1,322 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to [EMAIL PROTECTED] so we can send you a copy immediately.
+ *
+ * @package    Zend_Pdf
+ * @subpackage FileParser
+ * @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+/** Zend_Pdf_FileParser_Image */
+require_once 'Zend/Pdf/FileParser/Image.php';
+
+
+/**
+ * Abstract base class for Image file parsers.
+ *
+ * @package    Zend_Pdf
+ * @subpackage FileParser
+ * @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class Zend_Pdf_FileParser_Image_PNG extends Zend_Pdf_FileParser_Image
+{
+     protected $_isPNG;
+     protected $_width;
+     protected $_height;
+     protected $_bits;
+     protected $_color;
+     protected $_compression;
+     protected $_preFilter;
+     protected $_interlacing;
+
+     protected $_imageData;
+     protected $_paletteData;
+     protected $_transparencyData;
+
+  /**** Public Interface ****/
+
+     public function getWidth() {
+          if(!$this->_isParsed) {
+               $this->parse();
+          }
+          return $this->_width;
+     }
+
+     public function getHeight() {
+          if(!$this->_isParsed) {
+               $this->parse();
+          }
+          return $this->_width;
+     }
+
+     public function getBitDepth() {
+          if(!$this->_isParsed) {
+               $this->parse();
+          }
+          return $this->_bits;
+     }
+
+     public function getColorSpace() {
+          if(!$this->_isParsed) {
+               $this->parse();
+          }
+          return $this->_color;
+     }
+
+     public function getCompressionStrategy() {
+          if(!$this->_isParsed) {
+               $this->parse();
+          }
+          return $this->_compression;
+     }
+
+     public function getPaethFilter() {
+          if(!$this->_isParsed) {
+               $this->parse();
+          }
+          return $this->_preFilter;
+     }
+
+     public function getInterlacingMode() {
+          if(!$this->_isParsed) {
+               $this->parse();
+          }
+          return $this->_interlacing;
+     }
+
+     public function getRawImageData() {
+          if(!$this->_isParsed) {
+               $this->parse();
+          }
+          return $this->_imageData;
+     }
+
+     public function getRawPaletteData() {
+          if(!$this->_isParsed) {
+               $this->parse();
+          }
+          return $this->_paletteData;
+     }
+
+     public function getRawTransparencyData() {
+          if(!$this->_isParsed) {
+               $this->parse();
+          }
+          return $this->_transparencyData;
+     }
+
+  /* Semi-Concrete Class Implementation */
+
+    /**
+     * Verifies that the image file is in the expected format.
+     *
+     * @throws Zend_Pdf_Exception
+     */
+    public function screen()
+    {
+         if ($this->_isScreened) {
+             return;
+         }
+         return $this->_checkSignature();
+    }
+
+    /**
+     * Reads and parses the image data from the file on disk.
+     *
+     * @throws Zend_Pdf_Exception
+     */
+    public function parse()
+    {
+        if ($this->_isParsed) {
+            return;
+        }
+
+        /* Screen the font file first, if it hasn't been done yet.
+        */
+        $this->screen();
+
+        $this->_parseIHDRChunk();
+        $this->_parseChunks();
+    }
+
+
+    protected function _parseSignature() {
+         $this->moveToOffset(1); //Skip the first byte (%)
+         if('PNG' != $this->readBytes(3)) {
+               $this->_isPNG = false;
+         } else {
+               $this->_isPNG = true;
+         }
+    }
+
+    protected function _checkSignature() {
+         if(!isset($this->_isPNG)) {
+              $this->_parseSignature();
+         }
+         return $this->_isPNG;
+    }
+
+    protected function _parseChunks() {
+ $this->moveToOffset(33); //Variable chunks start at the end of IHDR
+
+ //Start processing chunks. If there are no more bytes to read parsing is complete.
+         $size = $this->getSize();
+         while($size - $this->getOffset() >= 8) {
+              $chunkLength = $this->readUInt(4);
+ if($chunkLength < 0 || ($chunkLength + $this->getOffset() + 4) > $size) { + throw new Zend_Pdf_Exception("PNG Corrupt: Invalid Chunk Size In File.");
+              }
+
+              $chunkType = $this->readBytes(4);
+              $offset = $this->getOffset();
+
+ //If we know how to process the chunk, do it here, else ignore the chunk and move on to the next
+              switch($chunkType) {
+ case 'IDAT': // This chunk may appear more than once. It contains the actual image data.
+                       $this->_parseIDATChunk($offset, $chunkLength);
+                       break;
+
+ case 'PLTE': // This chunk contains the image palette.
+                       $this->_parsePLTEChunk($offset, $chunkLength);
+                       break;
+
+ case 'tRNS': // This chunk contains non-alpha channel transparency data
+                       $this->_parseTRNSChunk($offset, $chunkLength);
+                       break;
+
+                   case 'IEND':
+                       break 2; //End the loop too
+
+ //@TODO Implement the rest of the PNG chunks. (There are many not implemented here)
+              }
+              if($offset + $chunkLength + 4 < $size) {
+ $this->moveToOffset($offset + $chunkLength + 4); //Skip past the data finalizer. (Don't rely on the parse to leave the offsets correct)
+              }
+         }
+         if(empty($this->_imageData)) {
+ throw new Zend_Pdf_Exception ( "This PNG is corrupt. All png must contain IDAT chunks." );
+         }
+    }
+
+    protected function _parseIHDRChunk() {
+ $this->moveToOffset(12); //IHDR must always start at offset 12 and run for 17 bytes
+         if(!$this->readBytes(4) == 'IHDR') {
+ throw new Zend_Pdf_Exception( "This PNG is corrupt. The first chunk in a PNG file must be IHDR." );
+         }
+         $this->_width = $this->readUInt(4);
+         $this->_height = $this->readUInt(4);
+         $this->_bits = $this->readInt(1);
+         $this->_color = $this->readInt(1);
+         $this->_compression = $this->readInt(1);
+         $this->_preFilter = $this->readInt(1);
+         $this->_interlacing = $this->readInt(1);
+ if($this->_interlacing != Zend_Pdf_Image::PNG_INTERLACING_DISABLED) { + throw new Zend_Pdf_Exception( "Only non-interlaced images are currently supported." );
+         }
+    }
+
+    protected function _parseIDATChunk($chunkOffset, $chunkLength) {
+         $this->moveToOffset($chunkOffset);
+         if(!isset($this->_imageData)) {
+              $this->_imageData = $this->readBytes($chunkLength);
+         } else {
+              $this->_imageData .= $this->readBytes($chunkLength);
+         }
+    }
+
+    protected function _parsePLTEChunk($chunkOffset, $chunkLength) {
+         $this->moveToOffset($chunkOffset);
+         $this->_paletteData = $this->readBytes($chunkLength);
+    }
+
+    protected function _parseTRNSChunk($chunkOffset, $chunkLength) {
+         $this->moveToOffset($chunkOffset);
+
+ //Processing of tRNS data varies dependending on the color depth
+
+         switch($this->_color) {
+             case Zend_Pdf_Image::PNG_CHANNEL_GRAY:
+                  $baseColor = $this->readInt(1);
+ $this->_transparencyData = array($baseColor, $baseColor);
+                  break;
+
+             case Zend_Pdf_Image::PNG_CHANNEL_RGB:
+
+                  //@TODO Fix this hack.
+ //This parser cheats and only uses the lsb's (and only works with < 16 bit depth images)
+
+                  /*
+                       From the standard:
+
+ For color type 2 (truecolor), the tRNS chunk contains a single RGB color value, stored in the format:
+
+                       Red:   2 bytes, range 0 .. (2^bitdepth)-1
+                       Green: 2 bytes, range 0 .. (2^bitdepth)-1
+                       Blue:  2 bytes, range 0 .. (2^bitdepth)-1
+
+ (If the image bit depth is less than 16, the least significant bits are used and the others are 0.) + Pixels of the specified color value are to be treated as transparent (equivalent to alpha value 0); + all other pixels are to be treated as fully opaque (alpha value 2bitdepth-1).
+
+                  */
+
+                  $red = $this->readInt(1);
+                  $this->skipBytes(1);
+                  $green = $this->readInt(1);
+                  $this->skipBytes(1);
+                  $blue = $this->readInt(1);
+
+ $this->_transparencyData = array($red, $red, $green, $green, $blue, $blue);
+
+                  break;
+
+             case Zend_Pdf_Image::PNG_CHANNEL_INDEXED:
+
+                  //@TODO Fix this hack.
+ //This parser cheats too. It only masks the first color in the palette.
+
+                  /*
+                       From the standard:
+
+ For color type 3 (indexed color), the tRNS chunk contains a series of one-byte alpha values, corresponding to entries in the PLTE chu
nk:
+
+                          Alpha for palette index 0:  1 byte
+                          Alpha for palette index 1:  1 byte
+                          ...etc...
+
+ Each entry indicates that pixels of the corresponding palette index must be treated as having the specified alpha value. + Alpha values have the same interpretation as in an 8-bit full alpha channel: 0 is fully transparent, 255 is fully opaque, + regardless of image bit depth. The tRNS chunk must not contain more alpha values than there are palette entries, + but tRNS can contain fewer values than there are palette entries. In this case, the alpha value for all remaining palette + entries is assumed to be 255. In the common case in which only palette index 0 need be made transparent, only a one-byte
+                       tRNS chunk is needed.
+
+                  */
+
+                  $tmpData = $this->readBytes($chunkLength);
+ if(($trnsIdx = strpos($tmpData, chr(0))) !== false) { + $this->_transparencyData = array($trnsIdx, $trnsIdx);
+                  }
+
+                  break;
+
+             case Zend_Pdf_Image::PNG_CHANNEL_GRAY_ALPHA:
+                  //Fall through to the next case
+             case Zend_Pdf_Image::PING_CHANNEL_RGB_ALPHA:
+ throw new Zend_Pdf_Exception( "tRNS chunk illegal for Alpha Channel Images" );
+                  break;
+         }
+    }
+}
Index: FileParser/Image.php
===================================================================
--- FileParser/Image.php        (revision 0)
+++ FileParser/Image.php        (revision 0)
@@ -0,0 +1,54 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to [EMAIL PROTECTED] so we can send you a copy immediately.
+ *
+ * @package    Zend_Pdf
+ * @subpackage FileParser
+ * @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+/** Zend_Pdf_FileParser */
+require_once 'Zend/Pdf/FileParser.php';
+
+/** Zend_Log */
+require_once 'Zend/Log.php';
+
+
+/**
+ * FileParser for Zend_Pdf_Image subclasses.
+ *
+ * @package    Zend_Pdf
+ * @subpackage FileParser
+ * @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+abstract class Zend_Pdf_FileParser_Image extends Zend_Pdf_FileParser
+{
+    protected $imageType;
+
+    /**
+     * Object constructor.
+     *
+ * Validates the data source and enables debug logging if so configured.
+     *
+     * @param Zend_Pdf_FileParserDataSource $dataSource
+     * @throws Zend_Pdf_Exception
+     */
+ public function __construct(Zend_Pdf_FileParserDataSource $dataSource)
+    {
+        parent::__construct($dataSource);
+       $this->imageType = Zend_Pdf_Image::TYPE_UNKNOWN;
+    }
+
+}
Index: Page.php
===================================================================
--- Page.php    (revision 4305)
+++ Page.php    (working copy)
@@ -935,13 +935,13 @@
    /**
     * Draw an image at the specified position on the page.
     *
-     * @param Zend_Pdf_Image $image
+     * @param Zend_Pdf_Resource_Image $image
     * @param float $x1
     * @param float $y1
     * @param float $x2
     * @param float $y2
     */
- public function drawImage(Zend_Pdf_Image $image, $x1, $y1, $x2, $y2) + public function drawImage(Zend_Pdf_Resource_Image $image, $x1, $y1, $x2, $y2)
    {
        $this->_addProcSet('PDF');

Index: Image.php
===================================================================
--- Image.php   (revision 0)
+++ Image.php   (revision 0)
@@ -0,0 +1,233 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to [EMAIL PROTECTED] so we can send you a copy immediately.
+ *
+ * @package    Zend_Pdf
+ * @subpackage Images
+ * @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+/** Zend_Pdf_FileParserDataSource */
+require_once 'Zend/Pdf/FileParserDataSource.php';
+
+/** Zend_Pdf_FileParserDataSource_File */
+require_once 'Zend/Pdf/FileParserDataSource/File.php';
+
+/** Zend_Pdf_FileParserDataSource_String */
+require_once 'Zend/Pdf/FileParserDataSource/String.php';
+
+/**
+ * Abstract factory class which vends [EMAIL PROTECTED] Zend_Pdf_Resource_Image} objects.
+ *
+ * This class is also the home for image-related constants because the name of + * the true base class ([EMAIL PROTECTED] Zend_Pdf_Resource_Image}) is not intuitive for the
+ * end user.
+ *
+ * @package    Zend_Pdf
+ * @subpackage Images
+ * @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+abstract class Zend_Pdf_Image
+{
+  /**** Class Constants ****/
+
+
+  /* Image Types */
+
+    const TYPE_UNKNOWN = 0;
+    const TYPE_JPEG = 1;
+    const TYPE_PNG = 2;
+    const TYPE_TIFF = 3;
+
+  /* TIFF Constants */
+
+    const TIFF_FIELD_TYPE_BYTE=1;
+    const TIFF_FIELD_TYPE_ASCII=2;
+    const TIFF_FIELD_TYPE_SHORT=3;
+    const TIFF_FIELD_TYPE_LONG=4;
+    const TIFF_FIELD_TYPE_RATIONAL=5;
+
+    const TIFF_TAG_IMAGE_WIDTH=256;
+    const TIFF_TAG_IMAGE_LENGTH=257; //Height
+    const TIFF_TAG_BITS_PER_SAMPLE=258;
+    const TIFF_TAG_COMPRESSION=259;
+    const TIFF_TAG_PHOTOMETRIC_INTERPRETATION=262;
+    const TIFF_TAG_STRIP_OFFSETS=273;
+    const TIFF_TAG_SAMPLES_PER_PIXEL=277;
+    const TIFF_TAG_STRIP_BYTE_COUNTS=279;
+
+    const TIFF_COMPRESSION_UNCOMPRESSED = 1;
+    const TIFF_COMPRESSION_CCITT1D = 2;
+    const TIFF_COMPRESSION_GROUP_3_FAX = 3;
+    const TIFF_COMPRESSION_GROUP_4_FAX  = 4;
+    const TIFF_COMPRESSION_LZW = 5;
+    const TIFF_COMPRESSION_JPEG = 6;
+    const TIFF_COMPRESSION_FLATE = 8;
+    const TIFF_COMPRESSION_FLATE_OBSOLETE_CODE = 32946;
+    const TIFF_COMPRESSION_PACKBITS = 32773;
+
+    const TIFF_PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO=0;
+    const TIFF_PHOTOMETRIC_INTERPRETATION_BLACK_IS_ZERO=1;
+    const TIFF_PHOTOMETRIC_INTERPRETATION_RGB=2;
+    const TIFF_PHOTOMETRIC_INTERPRETATION_RGB_INDEXED=3;
+    const TIFF_PHOTOMETRIC_INTERPRETATION_CMYK=5;
+    const TIFF_PHOTOMETRIC_INTERPRETATION_YCBCR=6;
+    const TIFF_PHOTOMETRIC_INTERPRETATION_CIELAB=8;
+
+  /* PNG Constants */
+
+    const PNG_COMPRESSION_DEFAULT_STRATEGY = 0;
+    const PNG_COMPRESSION_FILTERED = 1;
+    const PNG_COMPRESSION_HUFFMAN_ONLY = 2;
+    const PNG_COMPRESSION_RLE = 3;
+
+    const PNG_FILTER_NONE = 0;
+    const PNG_FILTER_SUB = 1;
+    const PNG_FILTER_UP = 2;
+    const PNG_FILTER_AVERAGE = 3;
+    const PNG_FILTER_PAETH = 4;
+
+    const PNG_INTERLACING_DISABLED = 0;
+    const PNG_INTERLACING_ENABLED = 1;
+
+    const PNG_CHANNEL_GRAY = 0;
+    const PNG_CHANNEL_RGB = 2;
+    const PNG_CHANNEL_INDEXED = 3;
+    const PNG_CHANNEL_GRAY_ALPHA = 4;
+    const PNG_CHANNEL_RGB_ALPHA = 6;
+
+  /**** Public Interface ****/
+
+
+  /* Factory Methods */
+
+    /**
+     * Returns a [EMAIL PROTECTED] Zend_Pdf_Resource_Image} object by file 
path.
+     *
+     * @param string $filePath Full path to the image file.
+     * @return Zend_Pdf_Resource_Image
+     * @throws Zend_Pdf_Exception
+     */
+    public static function imageWithPath($filePath)
+    {
+
+ /* Create a file parser data source object for this file. File path and
+         * access permission checks are handled here.
+         */
+ $dataSource = new Zend_Pdf_FileParserDataSource_File($filePath);
+
+ /* Attempt to determine the type of image. We can't always trust file
+         * extensions, but try that first since it's fastest.
+         */
+ $fileExtension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
+
+ /* If it turns out that the file is named improperly and we guess the
+         * wrong type, we'll get null instead of an image object.
+         */
+        switch ($fileExtension) {
+            case 'tif':
+                //Fall through to next case;
+            case 'tiff':
+ $image = Zend_Pdf_Image::_extractTiffImage($dataSource);
+                break;
+            case 'png':
+ $image = Zend_Pdf_Image::_extractPngImage($dataSource);
+                break;
+            case 'jpg':
+                //Fall through to next case;
+            case 'jpe':
+                //Fall through to next case;
+            case 'jpeg':
+ $image = Zend_Pdf_Image::_extractJpegImage($dataSource);
+                break;
+            default:
+ throw new Zend_Pdf_Exception("Cannot create image resource. File extension not known or unsupported type.");
+                break;
+        }
+
+        /* Done with the data source object.
+         */
+        $dataSource = null;
+
+        if (! is_null($image)) {
+            return $image;
+
+        } else {
+            /* The type of image could not be determined. Give up.
+             */
+ throw new Zend_Pdf_Exception("Cannot determine image type: $filePath",
+ Zend_Pdf_Exception::CANT_DETERMINE_IMAGE_TYPE);
+         }
+    }
+
+
+
+  /**** Internal Methods ****/
+
+
+  /* Image Extraction Methods */
+
+    /**
+     * Attempts to extract a JPEG Image from the data source.
+     *
+     * @param Zend_Pdf_FileParserDataSource $dataSource
+     * @return Zend_Pdf_Resource_Image_JPEG May also return null if
+     *   the data source does not appear to contain valid image data.
+     * @throws Zend_Pdf_Exception
+     */
+    protected static function _extractJpegImage($dataSource)
+    {
+ $imageParser = new Zend_Pdf_FileParser_Image_JPEG($dataSource);
+        $image = new Zend_Pdf_Resource_Image_JPEG($imageParser);
+        unset($imageParser);
+
+        return $image;
+    }
+
+    /**
+     * Attempts to extract a PNG Image from the data source.
+     *
+     * @param Zend_Pdf_FileParserDataSource $dataSource
+     * @return Zend_Pdf_Resource_Image_PNG May also return null if
+     *   the data source does not appear to contain valid image data.
+     * @throws Zend_Pdf_Exception
+     */
+    protected static function _extractPngImage($dataSource)
+    {
+        $imageParser = new Zend_Pdf_FileParser_Image_PNG($dataSource);
+        $image = new Zend_Pdf_Resource_Image_PNG($imageParser);
+        unset($imageParser);
+
+        return $image;
+    }
+
+    /**
+     * Attempts to extract a PNG Image from the data source.
+     *

+     * @param Zend_Pdf_FileParserDataSource $dataSource
+     * @return Zend_Pdf_Resource_Image_PNG May also return null if
+     *   the data source does not appear to contain valid image data.
+     * @throws Zend_Pdf_Exception
+     */
+    protected static function _extractPngImage($dataSource)
+    {
+        $imageParser = new Zend_Pdf_FileParser_Image_PNG($dataSource);
+        $image = new Zend_Pdf_Resource_Image_PNG($imageParser);
+        unset($imageParser);
+
+        return $image;
+    }
+
+    /**
+     * Attempts to extract a PNG Image from the data source.
+     *
+     * @param Zend_Pdf_FileParserDataSource $dataSource
+     * @return Zend_Pdf_Resource_Image_PNG May also return null if
+     *   the data source does not appear to contain valid image data.
+     * @throws Zend_Pdf_Exception
+     */
+    protected static function _extractTiffImage($dataSource)
+    {
+ $imageParser = new Zend_Pdf_FileParser_Image_TIFF($dataSource);
+        $image = new Zend_Pdf_Resource_Image_TIFF($imageParser);
+        unset($imageParser);
+
+        return $image;
+    }
+
+}


----- Original Message ----- From: "Matthew Ratzloff" <[EMAIL PROTECTED]>
To: "Zend Framework General" <fw-general@lists.zend.com>
Sent: Monday, April 02, 2007 9:57 AM
Subject: Re: [fw-general] Zend_Parser


This weekend I wrote a GIF parser, and refactored it a bit to use these classes (I do have a life--I did other things, too!). I've renamed some things and added some things (like setByteOrder() and BYTE_ORDER_MACHINE) and spent a couple hours writing an invisible buffering mechanism before
determining that it actually made performance worse.  :-D

Currently, and this is by no means finalized, but this is the structure:

Zend_Parser_Interface
Zend_Parser_DataSource_Interface
Zend_File implements Zend_Parser_DataSource_Interface
Zend_File_Parser_Abstract implements Zend_Parser_Interface
My_Image_Gif extends Zend_File
My_Image_Gif_Parser extends Zend_File_Parser_Abstract

Theoretically, if there were something like Zend_String, it could
implement Zend_Parser_DataSource_Interface and Zend_String_Parser could
implement Zend_Parser_Interface.

If you have suggestions on a better structure, I'd like to hear your
thoughts.

-Matt

On Mon, April 2, 2007 3:26 am, Pádraic Brady wrote:
Spent part of the weekend implementing a file buffer class and parser for yet another format (string, not binary). So I'm reading this email topic
with a lot of interest.

It would be good to see some general interface/abstract for file/string reading and parsing. I just looked at the StringParser and other files briefly (would just note the lack of Unicode string parsing ;)) and they all bear similarities to a parser I spent some hours mocking a partial
implementation of.

There's a lot of re-useable code under the Zend/Pdf location that could be generalised, moved as suggested, and improved and I'd love to see that happen. I have a chunk of ideas that could use it! As it stands I have to give some though to using the current code (even if it's another non-PDF file, it's all plain text) in favour of what I might write - general or not it's better than reinventing something already in the framework. If nothing else it might help cut down development time even if I have to
make minor changes via subclasses.

++1 to a proposal. :)

Pádraic Brady
http://blog.astrumfutura.com
http://www.patternsforphp.com


----- Original Message ----
From: Matthew Ratzloff <[EMAIL PROTECTED]>
To: Kevin McArthur <[EMAIL PROTECTED]>
Cc: fw-general@lists.zend.com
Sent: Saturday, March 31, 2007 11:39:13 PM
Subject: Re: [fw-general] Zend_File_Parser, Zend_String, etc.
(was: Operating with file system: OO approach)

On second thought, it makes more sense to have

Zend_Parser_Interface
Zend_Parser_DataSource_Interface*

Neither with concrete classes (like SPL). Then Zend_File can implement
Zend_Parser_DataSource_Interface and Zend_File_Parser can implement
Zend_Parser_Interface.

I'll look into this more thoroughly tonight.  I can see all kinds of
applications for it.

-Matt

* There's probably a better name for this.

On Sat, March 31, 2007 2:42 pm, Kevin McArthur wrote:
We suggested this when it was written (and I'm yet to have fully
translated
the image parsers to use the new one, open issue yadayada) so if you're going to create a proper fileparser class, do it soon and we can update
the
outstanding changes to use the new class.

Willie wrote this class when getting the font support for zpdf going and
its
quite robust.

We suggested a generalized parser, but we've barely got the time to
handle
our own backyard let alone push through a full proposal etc. However,
that
was our intent for this stuff; after we wrote 4 or 5 specific format
parsers
and said *wtf* =P

So ya, proposal sounds good, push it through n such.

Kevin



----- Original Message -----
From: "Matthew Ratzloff" <[EMAIL PROTECTED]>
To: "Kevin McArthur" <[EMAIL PROTECTED]>
Cc: <fw-general@lists.zend.com>
Sent: Saturday, March 31, 2007 2:15 PM
Subject: Re: [fw-general] Zend_File_Parser, Zend_String, etc. (was:
Operating with file system: OO approach)


LOL

Naturally, you post this after I spend part of my weekend writing a
byte
parser with similar functionality.  :-D

This needs to split off into its own component ASAP.  Additional
functionality can get filled in as it comes. Some really useful stuff
in
there for file parsing.

The current files:

Zend_Pdf_FileParser
Zend_Pdf_FileParserDataSource (abstract)
Zend_Pdf_FileParserDataSource_File extends
Zend_Pdf_FileParserDataSource
Zend_Pdf_FileParserDataSource_String extends
Zend_Pdf_FileParserDataSource

Zend_Pdf_FileParser_DataSource can pretty easily become an interface
instead.  Perhaps:

Zend_File_Parser
Zend_File_Interface
Zend_File implements Zend_File_Interface
Zend_String implements Zend_File_Interface

I have one or two ideas for a Zend_String class. The first is a method for converting "stringified" string literals (for instance, '\x00', as
opposed to "\x00") to their literal value.  This is necessary when
parsing
magic files (see my Zend_Mime_Magic proposal).  I'm sure others can
think
of ideas for that as well.

-Matt

On Sat, March 31, 2007 1:33 pm, Kevin McArthur wrote:
There is also an excellent binary file parser in Zend_Pdf if you need
to
parse a binary file format.

Kevin
----- Original Message -----
From: "Ralph Schindler" <[EMAIL PROTECTED]>
To: "Ivan Ruiz Gallego" <[EMAIL PROTECTED]>
Cc: <fw-general@lists.zend.com>
Sent: Saturday, March 31, 2007 1:25 PM
Subject: Re: [fw-general] Operating with file system: OO approach


Have you looked into the SPL?

http://www.php.net/~helly/php/ext/spl/

Specifically: DirectoryIterator, SPLFileInfo, SplFileObject

You might find some useful information and File/Directory patterns in
there.

-ralph

Ivan Ruiz Gallego wrote:
Hello Matt,

Well. In first instance I am thinking about the basic file system
related
operations like: create, remove, rename, move, copy or find
directories
and files. Basically, what we can already do, but in an
object-oriented
manner. In addition to this, I have been also thinking about a
something
like a recycle bin.

Regards,
Ivan.

Matthew Ratzloff schrieb:
There has been some light discussion in this area. Do you have any
suggestions for what a component like this should be capable of
doing?

-Matt

On Sat, March 31, 2007 9:31 am, Ivan Ruiz Gallego wrote:

Hello,

I am looking for a class or a set of classes that allow an
object-oriented operation with the file system. I am working now
with
SPL and I have also taken a look to PEAR, but I haven't so far
found
a
comprehensive solution. As far as I know is such a library not
included
in Zend Framework.

Here my questions:
- Is the inclusion of such functionality planed within Zend
Framework?
- Does anyone know about such a library?

Thanks.

Best regards,
Ivan.

--
Loglan GmbH
Ivan Ruiz Gallego

Binzmühlestrasse 210
8050 Zürich
Switzerland

Office +41 44 310 19 20
Mobile +41 76 321 23 68
Net www.loglan.net











--
Loglan GmbH
Ivan Ruiz Gallego

Binzmühlestrasse 210
8050 Zürich
Switzerland

Office +41 44 310 19 20
Mobile +41 76 321 23 68
Net www.loglan.net


















____________________________________________________________________________________
Finding fabulous fares is fun.
Let Yahoo! FareChase search your favorite travel sites to find flight and
hotel bargains.
http://farechase.yahoo.com/promo-generic-14795097









Reply via email to