Recommend this page to a friend! |
Classes of Christian Vigh | PHP TIFF Split and Merge | README.md | Download |
|
DownloadINTRODUCTIONThe TiffTools package contains the following classes :
The TiffSplitter classThe TiffSplitter class takes a multi-page TIFF file and splits it into separate TIFF files containing one image at a time. It can operate on files that are larger than the available memory, or simply on strings containing TIFF data already loaded into memory from an existing TIFF file (or generated on-the-fly). Using it is fairly simple :
(note that you won't instantiate a TiffSplitter object directly : you will have to call either the Load() or LoadFromString() methods to do that). Once an instance has been created, you can use the array access or iterator methods to loop through each page of your input file :
or :
Each TIFF page, of type TiffSplitterPage contained in a TiffSplitter object has two interesting methods :
So, a basic example to save multi-page TIFF files as single-page ones would be :
The TiffMerger classThe TiffMerger class does the opposite of TiffSplitter : it takes several input TIFF files and combines them into a single one. The supplied TIFF files can of course in turn contain multiple pages. Special care has been taken about memory consumption :
Given the above features, you should be able to merge thousands of TIFF files together. Merging multiple TIFF files is fairly simple ; first, instantiate a TiffSplitter object :
Then add the files you want to merge :
Finally, merge the supplied input files and save the result :
There is also an AsString()* method, which returns the output tiff contents as a string :
but beware because, this time, you will need as much memory as needed to hold the TIFF contents as a string into memory ! A note about endiannessEndianness describes how the bytes of 16-, 32- or 64-bits values are physically stored in the file (this also applies for RAM). It can be of two types :
TIFF files can be generated either in little- or big-endian format. This information is given by the first two bytes of the file (0x4949 for little endian, 0x4D4D for big endian). The TiffSplitter class generates its output files using the endianness of the supplied input file. The TiffMerger class generates its output file using the endianness specified to its constructor (by default, little endian). Note that it supports any kind of endianness in the input files you supply. KNOWN LIMITATIONS AND ISSUESThe TiffSplitter and TiffMerger classes currently have the following limitations :
DOCUMENTATION REFERENCESThe following links provide useful information about the TIFF file format :
CLASS REFERENCETiffSplitter classThe TiffSplitter class is used to open a multi-page TIFF file and provides a way to save each image into separate output TIFF files. The TiffSplitter class cannot be instantiated directly : you have to use the Load() or LoadFromString() method instead. The TiffSplitter class inherits from TiffImage. MethodsLoad
Creates an instance of the TiffSplitter class and loads from the specified file primary information about its contents. This mainly concerns Image File Directory (IFD) information. Only the necessary parts of the specified file are loaded into memory, the rest of the file being cached on demand. Two parameters affect this behavior :
Caching information is the best way to handle files that are greater than the size specified by your memory\_limit PHP setting. Smaller cache sizes will mean more disk accesses, greater cache sizes will consume more memory. It's up to you to chose the right balance, depending on your processing needs. Note that greater buffer sizes will not necessarily improve performance. A size of 8Kb is in a mjority of case well suited for Linux systems. LoadFromString
Creates a TiffSplitter instance from the specified string, which can contain TIFF data loaded either from an existing TIFF file, or generated on-the-fly. Note that no caching mechanism will apply in this case. Properties$DEBUG (boolean)Setting this static property to true will show information about the internal structure of the TIFF file. EndiannessSpecifies the endianness (byte order) of the supplied TIFF data ; it can take the following values :
You cannot change the endianness of the generated output TIFF files. This property is informational only. $Filename (string)Input filename. This property will be set to false if the object has been created with the LoadFromString() method. TiffSplitterPage classThis class encapsulates one single page from the supplied multi-page TIFF file. It inherits from the TiffPage class (which is not documented here). It provides the following : MethodsAsString
Returns TIFF data corresponding to the page. This data can be directly saved on disk. The $format parameter can have one of the following values :
SaveTo
Saves the page to the specified file. The endianness (little-endian or big-endian for 16- and 32-bits values) is preserved. PropertiesActualPageNumberActual page number, as given by the PAGE_NUMBER tag in the IFD. This value will be set to false if no PAGE_NUMBER tag is present in the IFD. Beware that some TIFF files always have a PAGE_NUMBER entry, with the same value of zero for all the pages, so relying on this property value is uncertain ; use the PageNumber property instead. PageHeightPage height in lines. PageNumberCorresponding page number. Starts from zero. PageWidthPage width in pixels. TiffMergerClassThe TiffMerger class combines multiple TIFF files (either multipage or not) into a single output file. MethodsConstructor
Creates an instance of a merger object, specifying the endianness of the output file that will be generated. The $output\_endianness parameter can either be TiffImage::LITTLE\_ENDIAN (the default) or TiffImage::BIG\_ENDIAN. Add
Adds the specified files to the list of files to be merged. The arguments can be any number of strings or arrays of strings specifying the TIFF files to be added. AsString
Returns the contents of the merged input TIFF files as a single string. This string can later be directly saved to an output TIFF file using the file\_put\_contents() function. You should be aware that the size of the returned string should be more or less the total size of all the supplied input TIFF files, so beware of your current PHP memory limit. The $format parameter specifies the output format. Currently, only TiffImage::OUTPUT\_FORMAT\_TIFF is supported. SaveTo
Merges the supplied input TIFF files and saves the result to the file specified by $filename. The $format parameter specifies the output format. Currently, only TiffImage::OUTPUT\_FORMAT\_TIFF is supported. |