Recommend this page to a friend! |
Download |
Info | Example | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
Not enough user ratings | Total: 156 | All time: 9,017 This week: 67 |
Version | License | PHP version | Categories | |||
tifftools 1.0 | BSD License | 5.1 | PHP 5, Graphics |
<?php
|
The TiffTools package contains the following classes :
The 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 :
include ( 'TiffSplitter.phpclass' ) ;
$tiff = TiffSplitter::Load ( 'sample.tif' ) ;
(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 :
for ( $i = 0 ; $i < count ( $tiff ) ; $i ++ )
{
$tiff_page = $tiff [$i] ;
... do something with $tiff_page ...
}
or :
foreach ( $tiff as $tiff_page )
{
... do something with $tiff_page ...
}
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 :
include ( 'TiffSplitter.phpclass' ) ;
$tiff = TiffSplitter::Load ( 'sample.tif' ) ;
foreach ( $tiff as $tiff_page )
{
$tiff_page -> SaveTo ( "sample.page.{$tiff_page -> PageNumber}.tif" ) ;
}
The 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 :
$merger = new TiffMerger ( ) ;
Then add the files you want to merge :
$merger -> Add ( 'file1.tif', 'file2.tif', ..., 'filen.tif' ) ;
Finally, merge the supplied input files and save the result :
$merger -> SaveTo ( 'output.tif' ) ;
There is also an AsString()* method, which returns the output tiff contents as a string :
$tiff_data = $merger -> AsString ( ) ;
but beware because, this time, you will need as much memory as needed to hold the TIFF contents as a string into memory !
Endianness 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 :
Little endian : the least significant byte (LSB) is stored first, then the second one, up to the most significant byte (MSB). This is typical to Intel architectures. For example, the 16-bits value 0xFF00 will be stored as :
00 FF
Big endian : the MSB will be stored first, up to the LSB. The value of the above example will then be stored as :
FF 00
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.
The TiffSplitter and TiffMerger classes currently have the following limitations :
The following links provide useful information about the TIFF file format :
The 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.
$tiff = TiffSplitter::Load ( $filename, $buffer_size = 8192, $cache_size = 512 )
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.
$tiff = TiffSplitter::LoadFromString ( $tiff_data ) ;
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.
Setting this static property to true will show information about the internal structure of the TIFF file.
Specifies 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.
Input filename. This property will be set to false if the object has been created with the LoadFromString() method.
This 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 :
$data = $tiff_page -> AsString ( $format = TiffImage::OUTPUT_FORMAT_TIFF ) ;
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 :
$tiff_page -> SaveTo ( $filename, $format = TiffImage::OUTPUT_FORMAT_TIFF ) ;
Saves the page to the specified file.
The endianness (little-endian or big-endian for 16- and 32-bits values) is preserved.
Actual 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.
Page height in lines.
Corresponding page number. Starts from zero.
Page width in pixels.
The TiffMerger class combines multiple TIFF files (either multipage or not) into a single output file.
$merger = new TiffMerger ( $output_endianness = TiffImage::LITLLE\_ENDIAN ) ;
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.
$merger -> 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.
$tiff_data = $merger -> AsString ( $format = TiffImage::OUTPUT_FORMAT_TIFF ) ;
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.
$merger -> SaveTo ( $filename, $format = TiffImage::OUTPUT_FORMAT_TIFF ) ;
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.
Files (6) |
File | Role | Description |
---|---|---|
example.merge.php | Example | Example script |
example.split.php | Example | Example script |
README.md | Doc. | Documentation |
TiffBase.phpclass | Class | Class source |
TiffMerger.phpclass | Class | Class source |
TiffSplitter.phpclass | Class | Class source |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
Install with Composer |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.