-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTiffSplitter.phpclass
More file actions
executable file
·285 lines (211 loc) · 9.71 KB
/
TiffSplitter.phpclass
File metadata and controls
executable file
·285 lines (211 loc) · 9.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/**************************************************************************************************************
NAME
TiffSplitter.phpclass
DESCRIPTION
A class for splitting multipage TIFF files.
AUTHOR
Christian Vigh, 04/2017.
REFERENCES
- https://www.itu.int/itudoc/itu-t/com16/tiff-fx/docs/tiff6.pdf :
Specifications of the TIFF file format version 6.
- http://www.awaresystems.be/imaging/tiff.html :
Where to get a list of available TIFF IFD tags.
HISTORY
[Version : 1.0] [Date : 2017-04-08] [Author : CV]
Initial version.
[Version : 1.0.1] [Date : 2017-04-14] [Author : CV]
. Moved most of the code from TiffSplitter to TiffImage.
[Version : 1.0.2] [Date : 2017-04-16] [Author : CV]
. Completely rewrote the AsString() method to use the new TiffIFD and TiffIFDEntry classes.
[Version : 1.0.3] [Date : 2017-05-03] [Author : CV]
. Corrected an offset inconsistency in the output when the STRIP_BYTE_OFFSETS (and therefore the
STRIP_BYTE_COUNT) tag contains only one entry, directly contained in the IFD entry.
**************************************************************************************************************/
require_once ( dirname ( __FILE__ ) . '/TiffBase.phpclass' ) ;
/*==============================================================================================================
class TiffSplitter -
A class for splitting a mutipage TIFF file (or string) into individual TIFF files.
==============================================================================================================*/
class TiffSplitter extends TiffImage
implements \ArrayAccess, \Countable, \IteratorAggregate
{
/*--------------------------------------------------------------------------------------------------------------
Constructor -
Should not be called directly. Call the static Load() or LoadFromString() methods instead.
*-------------------------------------------------------------------------------------------------------------*/
public function __construct ( )
{
parent::__construct ( ) ;
}
/*--------------------------------------------------------------------------------------------------------------
GetPageInstance -
Returns an instance of a (splitted) TIFF page.
*-------------------------------------------------------------------------------------------------------------*/
protected function GetPageInstance ( $parent, $ifd_data )
{
return ( new TiffSplitterPage ( $parent, $ifd_data ) ) ;
}
}
/*==============================================================================================================
class TiffSplitterPage -
Allows to extract TIFF data from a page of a multipage TIFF file.
==============================================================================================================*/
class TiffSplitterPage extends TiffPage
{
/*--------------------------------------------------------------------------------------------------------------
Constructor -
Instantiates a TIFF page and retrieves the page number, if any.
*-------------------------------------------------------------------------------------------------------------*/
public function __construct ( $parent, $ifd_data )
{
parent::__construct ( $parent, $ifd_data ) ;
}
/*--------------------------------------------------------------------------------------------------------------
NAME
AsString - Converts the page to a TIFF or PDF file.
PROTOTYPE
$data = $page -> AsString ( $format = TiffImage::OUTPUT_FORMAT_TIFF ) ;
DESCRIPTION
Returns the contents of the TIFF page either as a TIFF or PDF file.
PARAMETERS
$format (integer) -
Either TiffImage::OUTPUT_FORMAT_TIFF or TiffImage::OUTPUT_FORMAT_PDF.
RETURN VALUE
A string value that can be directly used to create a TIFF or PDF file containing this page.
NOTES
The PDF file format is not yet implemented.
*-------------------------------------------------------------------------------------------------------------*/
public function __tostring ( )
{ return ( $this -> AsString ( ) ) ; }
public function AsString ( $format = TiffImage::OUTPUT_FORMAT_TIFF )
{
$tiff = $this -> Parent -> TiffData ;
$ifd = $this -> IFD ;
$ifd_count = $ifd -> GetRealCount ( ) ;
// Build the header (either the WORD16 0x4949 or 0x4D4D, followed by the WORD16 42)
$tiff_data = $tiff -> ToWord16 ( $this -> Parent -> Endianness, TiffImage::TIFF_IDENTIFIER ) ;
$offset = 4 ;
// Put the address of the first (and unique) IFD of this file. Naturally, it will come right after this entry
$tiff_data .= $tiff -> ToWord32 ( 8 ) ;
$offset += 4 ;
// Now, put the number of IFD entries
$tiff_data .= $tiff -> ToWord16 ( $ifd_count ) ;
$offset += 2 ;
// Compute the offset AFTER the IFD, including the pointer to the next IFD, which will be zero in our case
$post_offset = $offset + ( $ifd_count * TiffImage::IFD_ENTRY_SIZE ) + 4 ;
// Data after the IFD entries
$post_data = '' ;
// Loop through each IFD entry
foreach ( $ifd as $ifd_entry )
{
// Don't try to process unsupported tags
if ( ! $ifd_entry -> Supported )
continue ;
// Get the Tag, Type and Count values converted to their original endianness
$tag = $tiff -> ToWord16 ( $ifd_entry -> Tag ) ;
$type = $tiff -> ToWord16 ( $ifd_entry -> Type ) ;
$count = $tiff -> ToWord32 ( $ifd_entry -> Count ) ;
// Normal processing
if ( $ifd_entry -> Tag !== TiffImage::TAG_STRIP_OFFSETS )
{
// IFD entry is not an offset to some data : simply reuse the original Value
if ( ! $ifd_entry -> IsOffset )
$value = $ifd_entry -> Value ;
// Otherwise, retrieve the data it points to
else
{
// The original IFD value will be replaced with the current output offset
$value = $post_offset ;
// Get real data
$data = $ifd_entry -> UnderlyingValue ;
$post_data .= $data ;
$post_offset += strlen ( $data ) ;
}
}
// Special processing for StripOffsets
else
{
// The original IFD value will be replaced with the current output offset
$value = $post_offset ;
// Get real data
$data = $ifd_entry -> UnderlyingValue ;
// Get the StripByteCounts value
$byte_counts_entry = $this -> IFD -> GetTagById ( TiffImage::TAG_STRIP_BYTE_COUNTS ) ;
// When the Count field is > 1, the Value field is necessarily an offset
if ( $byte_counts_entry -> IsOffset )
{
// Get the StripByteCounts entries
$strip_byte_counts = $tiff -> Unpack32 ( $byte_counts_entry -> UnderlyingValue ) ;
// Then strip offset data
$strip_offsets = $tiff -> Unpack32 ( $data ) ;
$strip_count = count ( $strip_offsets ) ;
}
// Otherwise, the Value field gives the strip byte counts
else
{
$strip_byte_counts = array ( 1 => $byte_counts_entry -> Value ) ;
$strip_offsets = array ( 1 => $ifd_entry -> Value ) ;
$strip_count = 1 ;
}
// Rebuild the new offset table
$strip_data = '' ;
$strip_data_offset = $post_offset + ( $strip_count * 4 ) ;
$new_offsets = array ( ) ;
for ( $i = 1 ; $i <= $strip_count ; $i ++ )
{
$strip_data .= $tiff -> GetRawBytes ( $strip_offsets [$i], $strip_byte_counts [$i] ) ;
$new_offsets [] = $strip_data_offset ;
$strip_data_offset += $strip_byte_counts [$i] ;
}
// Catenate the new offset table + strip contents to the data to be added after the IFD structure
// but take into account the fact that there is only one byte count and strip data
if ( $byte_counts_entry -> IsOffset )
$extra_data = $tiff -> ToWord32 ( $new_offsets ) . $strip_data ;
else
$extra_data = $strip_data ;
$post_data .= $extra_data ;
$post_offset += strlen ( $extra_data ) ;
}
// Add this IFD entry tot he output data
$entry_value = $tiff -> ToWord32 ( $value ) ;
$tiff_data .= $tag . $type . $count . $entry_value ;
}
// Add the pointer to the next IFD, which is zero since there will be only one IFD here, then the data we extracted
// in the above loop
$tiff_data .= "\x00\x00\x00\x00" . $post_data ;
// All done, return
return ( $tiff_data ) ;
}
/*--------------------------------------------------------------------------------------------------------------
NAME
SaveTo - Saves the current page to the specified output file.
PROTOTYPE
$page -> SaveTo ( $filename, $format = TiffImage::OUTPUT_FORMAT_TIFF ) ;
DESCRIPTION
Saves the current page to the specified output file, in the specified format.
PARAMETERS
$filename (string) -
Output filename.
$format (integer) -
Either TiffImage::OUTPUT_FORMAT_TIFF or TiffImage::OUTPUT_FORMAT_PDF.
NOTES
The PDF file format is not yet implemented.
*-------------------------------------------------------------------------------------------------------------*/
public function SaveTo ( $filename, $format = TiffImage::OUTPUT_FORMAT_TIFF )
{
$data = $this -> AsString ( $format ) ;
switch ( $format )
{
//case TiffImage::OUTPUT_FORMAT_PDF :
// $expected_extensions = array ( 'pdf' ) ;
case TiffImage::OUTPUT_FORMAT_TIFF :
default :
$expected_extensions = array ( 'tiff', 'tif' ) ;
}
$extension = strtolower ( pathinfo ( $filename, PATHINFO_EXTENSION ) ) ;
if ( ! in_array ( $extension, $expected_extensions ) )
$filename .= '.' . $expected_extensions [0] ;
file_put_contents ( $filename, $data ) ;
}
}