source: extensions/edit_gmaps/admin/include/EXIF.php @ 10060

Last change on this file since 10060 was 10060, checked in by cljosse, 13 years ago

[extensions] edit_gmaps compatibility with piwigo 2.2

File size: 119.6 KB
Line 
1<?php
2
3/******************************************************************************
4*
5* Filename:     EXIF.php
6*
7* Description:  Provides functions for reading and writing EXIF Information
8*               to/from an APP1 segment of a JPEG file
9*               Unfortunately, because EXIF data may be distributed anywhere
10*               throughout an image file, rather than just being in one block,
11*               it is impossible to pass just a string containing only the EXIF
12*               information. Hence it is neccessary to be able to seek to
13*               any point in the file. This causes the HTTP and FTP wrappers
14*               not to work - i.e. the EXIF functions will only work with local
15*               files.
16*               To work on an internet file, copy it locally to start with:
17*
18*               $newfilename = tempnam ( $dir, "tmpexif" );
19*               copy ( "http://whatever.com", $newfilename );
20*
21*
22* Author:      Evan Hunter
23*
24* Date:         30/7/2004
25*
26* Project:      PHP JPEG Metadata Toolkit
27*
28* Revision:     1.11
29*
30* Changes:      1.00 -> 1.10 : added function get_EXIF_TIFF to allow extracting EXIF from a TIFF file
31*               1.10 -> 1.11 : added functionality to allow decoding of XMP and Photoshop IRB information
32*                              embedded within the EXIF data
33*                              added checks for http and ftp wrappers, as these are not supported
34*                              changed interpret_IFD to allow thumbnail links to work when
35*                              toolkit is portable across directories
36*
37*
38* URL:          http://electronics.ozhiker.com
39*
40* Copyright:    Copyright " . $auteur . " 2004
41*
42* License:      This file is part of the PHP JPEG Metadata Toolkit.
43*
44*               The PHP JPEG Metadata Toolkit is free software; you can
45*               redistribute it and/or modify it under the terms of the
46*               GNU General Public License as published by the Free Software
47*               Foundation; either version 2 of the License, or (at your
48*               option) any later version.
49*
50*               The PHP JPEG Metadata Toolkit is distributed in the hope
51*               that it will be useful, but WITHOUT ANY WARRANTY; without
52*               even the implied warranty of MERCHANTABILITY or FITNESS
53*               FOR A PARTICULAR PURPOSE.  See the GNU General Public License
54*               for more details.
55*
56*               You should have received a copy of the GNU General Public
57*               License along with the PHP JPEG Metadata Toolkit; if not,
58*               write to the Free Software Foundation, Inc., 59 Temple
59*               Place, Suite 330, Boston, MA  02111-1307  USA
60*
61*               If you require a different license for commercial or other
62*               purposes, please contact the author: evan@ozhiker.com
63*
64******************************************************************************/
65
66
67// TODO : Thoroughly test the functions for writing EXIF segments
68// TODO : Figure out a way to allow EXIF to function normally with HTTP and FTP wrappers
69// TODO : Implement EXIF decoding of Device Setting Description field
70// TODO : Implement EXIF decoding of SpatialFrequencyResponse field
71// TODO : Implement EXIF decoding of OECF field
72// TODO : Implement EXIF decoding of SubjectArea field
73// TODO : Add a put_EXIF_TIFF function
74
75/******************************************************************************
76*
77* Initialisation
78*
79******************************************************************************/
80
81
82if ( !isset( $GLOBALS['HIDE_UNKNOWN_TAGS'] ) )     $GLOBALS['HIDE_UNKNOWN_TAGS']= FALSE;
83if ( !isset( $GLOBALS['SHOW_BINARY_DATA_HEX'] ) )  $GLOBALS['SHOW_BINARY_DATA_HEX'] = FALSE;
84if ( !isset( $GLOBALS['SHOW_BINARY_DATA_TEXT'] ) ) $GLOBALS['SHOW_BINARY_DATA_TEXT'] = FALSE;
85
86
87include_once INCLUDE_PATH.'EXIF_Tags.php';
88include_once INCLUDE_PATH.'EXIF_Makernote.php';
89include_once INCLUDE_PATH.'PIM.php';
90include_once INCLUDE_PATH.'Unicode.php';
91include_once INCLUDE_PATH.'JPEG.php';
92include_once INCLUDE_PATH.'IPTC.php';
93//include_once INCLUDE_PATH.'Photoshop_IRB.php';       // Change: as of version 1.11  - Required for TIFF with embedded IRB
94include_once INCLUDE_PATH.'XMP.php';                 // Change: as of version 1.11  - Required for TIFF with embedded XMP
95include_once INCLUDE_PATH.'pjmt_utils.php';          // Change: as of version 1.11  - Required for directory portability
96
97
98
99
100
101
102
103
104/******************************************************************************
105*
106* Function:     get_EXIF_JPEG
107*
108* Description:  Retrieves information from a Exchangeable Image File Format (EXIF)
109*               APP1 segment and returns it in an array.
110*
111* Parameters:   filename - the filename of the JPEG image to process
112*
113* Returns:      OutputArray - Array of EXIF records
114*               FALSE - If an error occured in decoding
115*
116******************************************************************************/
117
118function get_EXIF_JPEG( $filename )
119{
120global $error_message ;
121        // Change: Added as of version 1.11
122        // Check if a wrapper is being used - these are not currently supported (see notes at top of file)
123        if ( ( stristr ( $filename, "http://" ) != FALSE ) || ( stristr ( $filename, "ftp://" ) != FALSE ) )
124        {
125                // A HTTP or FTP wrapper is being used - show a warning and abort
126                $error_message .= "HTTP and FTP wrappers are currently not supported with EXIF - See EXIF functionality documentation - a local file must be specified<br>";
127                $error_message .= "To work on an internet file, copy it locally to start with:<br><br>\n";
128                $error_message .= "\$newfilename = tempnam ( \$dir, \"tmpexif\" );<br>\n";
129                $error_message .= "copy ( \"http://whatever.com\", \$newfilename );<br><br>\n";
130                return FALSE;
131        }
132
133        // get the JPEG headers
134        $jpeg_header_data = get_jpeg_header_data( $filename );
135
136
137        // Flag that an EXIF segment has not been found yet
138        $EXIF_Location = -1;
139
140        //Cycle through the header segments
141        for( $i = 0; $i < count( $jpeg_header_data ); $i++ )
142        {
143                // If we find an APP1 header,
144                if ( strcmp ( $jpeg_header_data[$i]['SegName'], "APP1" ) == 0 )
145                {
146                        // And if it has the EXIF label,
147                        if ( ( strncmp ( $jpeg_header_data[$i]['SegData'], "Exif\x00\x00", 6) == 0 ) ||
148                             ( strncmp ( $jpeg_header_data[$i]['SegData'], "Exif\x00\xFF", 6) == 0 ) )          // For some reason, some files have a faulty EXIF name which has a 0xFF in it
149                        {
150                                // Save the location of the EXIF segment
151                                $EXIF_Location = $i;
152                        }
153                }
154
155        }
156
157        // Check if an EXIF segment was found
158        if ( $EXIF_Location == -1 )
159        {
160                // Couldn't find any EXIF block to decode
161                return FALSE;
162        }
163
164        $filehnd = @fopen($filename, 'rb');
165
166        // Check if the file opened successfully
167        if ( ! $filehnd  )
168        {
169                // Could't open the file - exit
170                $error_message .= "<p>Could not open file $filename</p>\n";
171                return FALSE;
172        }
173
174        fseek( $filehnd, $jpeg_header_data[$EXIF_Location]['SegDataStart'] + 6  );
175
176        // Decode the Exif segment into an array and return it
177        $exif_data = process_TIFF_Header( $filehnd, "TIFF" );
178
179
180
181        // Close File
182        fclose($filehnd);
183        return $exif_data;
184}
185
186/******************************************************************************
187* End of Function:     get_EXIF_JPEG
188******************************************************************************/
189
190
191
192/******************************************************************************
193*
194* Function:     put_EXIF_JPEG
195*
196* Description:  Stores information into a Exchangeable Image File Format (EXIF)
197*               APP1 segment from an EXIF array.
198*
199*               WARNING: Because the EXIF standard allows pointers to data
200*               outside the APP1 segment, if there are any such pointers in
201*               a makernote, this function will DAMAGE them since it will not
202*               be aware that there is an external pointer. This will often
203*               happen with Makernotes that include an embedded thumbnail.
204*               This damage could be prevented where makernotes can be decoded,
205*               but currently this is not implemented.
206*
207*
208* Parameters:   exif_data - The array of EXIF data to insert into the JPEG header
209*               jpeg_header_data - The JPEG header into which the EXIF data
210*                                  should be stored, as from get_jpeg_header_data
211*
212* Returns:      jpeg_header_data - JPEG header array with the EXIF segment inserted
213*               FALSE - If an error occured
214*
215******************************************************************************/
216
217function put_EXIF_JPEG( $exif_data, $jpeg_header_data )
218{
219        // pack the EXIF data into its proper format for a JPEG file
220        $packed_data = get_TIFF_Packed_Data( $exif_data );
221        if ( $packed_data === FALSE )
222        {
223                return $jpeg_header_data;
224        }
225
226        $packed_data = "Exif\x00\x00$packed_data";
227
228        //Cycle through the header segments
229        for( $i = 0; $i < count( $jpeg_header_data ); $i++ )
230        {
231                // If we find an APP1 header,
232                if ( strcmp ( $jpeg_header_data[$i]['SegName'], "APP1" ) == 0 )
233                {
234                        // And if it has the EXIF label,
235                        if ( ( strncmp ( $jpeg_header_data[$i]['SegData'], "Exif\x00\x00", 6) == 0 ) ||
236                             ( strncmp ( $jpeg_header_data[$i]['SegData'], "Exif\x00\xFF", 6) == 0 ) )          // For some reason, some files have a faulty EXIF name which has a 0xFF in it
237                        {
238                                // Found a preexisting EXIF block - Replace it with the new one and return.
239                                $jpeg_header_data[$i]['SegData'] = $packed_data;
240                                return $jpeg_header_data;
241                        }
242                }
243        }
244
245        // No preexisting segment segment found, insert a new one at the start of the header data.
246
247        // Determine highest position of an APP segment at or below APP3, so we can put the
248        // new APP3 at this position
249
250
251        $highest_APP = -1;
252
253        //Cycle through the header segments
254        for( $i = 0; $i < count( $jpeg_header_data ); $i++ )
255        {
256                // Check if we have found an APP segment at or below APP3,
257                if ( ( $jpeg_header_data[$i]['SegType'] >= 0xE0 ) && ( $jpeg_header_data[$i]['SegType'] <= 0xE3 ) )
258                {
259                        // Found an APP segment at or below APP12
260                        $highest_APP = $i;
261                }
262        }
263
264        // No preexisting EXIF block found, insert a new one at the start of the header data.
265        array_splice($jpeg_header_data, $highest_APP + 1 , 0, array( array(   "SegType" => 0xE1,
266                                                                              "SegName" => "APP1",
267                                                                              "SegDesc" => $GLOBALS[ "JPEG_Segment_Descriptions" ][ 0xE1 ],
268                                                                              "SegData" => $packed_data ) ) );
269        return $jpeg_header_data;
270
271}
272
273/******************************************************************************
274* End of Function:     put_EXIF_JPEG
275******************************************************************************/
276
277
278
279
280/******************************************************************************
281*
282* Function:     get_Meta_JPEG
283*
284* Description:  Retrieves information from a Meta APP3 segment and returns it
285*               in an array. Uses information supplied by the
286*               get_jpeg_header_data function.
287*               The Meta segment has the same format as an EXIF segment, but
288*               uses different tags
289*
290* Parameters:   filename - the filename of the JPEG image to process
291*
292* Returns:      OutputArray - Array of Meta records
293*               FALSE - If an error occured in decoding
294*
295******************************************************************************/
296
297function get_Meta_JPEG( $filename )
298{
299global $error_message ;
300        // Change: Added as of version 1.11
301        // Check if a wrapper is being used - these are not currently supported (see notes at top of file)
302        if ( ( stristr ( $filename, "http://" ) != FALSE ) || ( stristr ( $filename, "ftp://" ) != FALSE ) )
303        {
304                // A HTTP or FTP wrapper is being used - show a warning and abort
305                $error_message .= "HTTP and FTP wrappers are currently not supported with Meta - See EXIF/Meta functionality documentation - a local file must be specified<br>";
306                $error_message .= "To work on an internet file, copy it locally to start with:<br><br>\n";
307                $error_message .= "\$newfilename = tempnam ( \$dir, \"tmpmeta\" );<br>\n";
308                $error_message .= "copy ( \"http://whatever.com\", \$newfilename );<br><br>\n";
309                return FALSE;
310        }
311
312        // get the JPEG headers
313        $jpeg_header_data = get_jpeg_header_data( $filename );
314
315
316        // Flag that an Meta segment has not been found yet
317        $Meta_Location = -1;
318
319        //Cycle through the header segments
320        for( $i = 0; $i < count( $jpeg_header_data ); $i++ )
321        {
322                // If we find an APP3 header,
323                if  ( strcmp ( $jpeg_header_data[$i]['SegName'], "APP3" ) == 0 )
324                {
325                        // And if it has the Meta label,
326                        if ( ( strncmp ( $jpeg_header_data[$i]['SegData'], "Meta\x00\x00", 6) == 0 ) ||
327                             ( strncmp ( $jpeg_header_data[$i]['SegData'], "META\x00\x00", 6) == 0 ) )
328                        {
329                                // Save the location of the Meta segment
330                                $Meta_Location = $i;
331                        }
332                }
333        }
334
335        // Check if an EXIF segment was found
336        if ( $Meta_Location == -1 )
337        {
338                // Couldn't find any Meta block to decode
339                return FALSE;
340        }
341
342
343        $filehnd = @fopen($filename, 'rb');
344
345        // Check if the file opened successfully
346        if ( ! $filehnd  )
347        {
348                // Could't open the file - exit
349                $error_message .= "<p>Could not open file $filename</p>\n";
350                return FALSE;
351        }
352
353        fseek( $filehnd, $jpeg_header_data[$Meta_Location]['SegDataStart'] + 6 );
354
355        // Decode the Meta segment into an array and return it
356        $meta = process_TIFF_Header( $filehnd, "Meta" );
357
358         // Close File
359        fclose($filehnd);
360
361        return $meta;
362}
363
364/******************************************************************************
365* End of Function:     get_Meta
366******************************************************************************/
367
368
369
370
371
372
373
374/******************************************************************************
375*
376* Function:     put_Meta_JPEG
377*
378* Description:  Stores information into a Meta APP3 segment from a Meta array.
379*
380*
381*               WARNING: Because the Meta (EXIF) standard allows pointers to data
382*               outside the APP1 segment, if there are any such pointers in
383*               a makernote, this function will DAMAGE them since it will not
384*               be aware that there is an external pointer. This will often
385*               happen with Makernotes that include an embedded thumbnail.
386*               This damage could be prevented where makernotes can be decoded,
387*               but currently this is not implemented.
388*
389*
390* Parameters:   meta_data - The array of Meta data to insert into the JPEG header
391*               jpeg_header_data - The JPEG header into which the Meta data
392*                                  should be stored, as from get_jpeg_header_data
393*
394* Returns:      jpeg_header_data - JPEG header array with the Meta segment inserted
395*               FALSE - If an error occured
396*
397******************************************************************************/
398
399function put_Meta_JPEG( $meta_data, $jpeg_header_data )
400{
401        // pack the Meta data into its proper format for a JPEG file
402        $packed_data = get_TIFF_Packed_Data( $meta_data );
403        if ( $packed_data === FALSE )
404        {
405                return $jpeg_header_data;
406        }
407
408        $packed_data = "Meta\x00\x00$packed_data";
409
410        //Cycle through the header segments
411        for( $i = 0; $i < count( $jpeg_header_data ); $i++ )
412        {
413                // If we find an APP1 header,
414                if ( strcmp ( $jpeg_header_data[$i]['SegName'], "APP3" ) == 0 )
415                {
416                        // And if it has the Meta label,
417                        if ( ( strncmp ( $jpeg_header_data[$i]['SegData'], "Meta\x00\x00", 6) == 0 ) ||
418                             ( strncmp ( $jpeg_header_data[$i]['SegData'], "META\x00\x00", 6) == 0 ) )
419                        {
420                                // Found a preexisting Meta block - Replace it with the new one and return.
421                                $jpeg_header_data[$i]['SegData'] = $packed_data;
422                                return $jpeg_header_data;
423                        }
424                }
425        }
426        // No preexisting segment segment found, insert a new one at the start of the header data.
427
428        // Determine highest position of an APP segment at or below APP3, so we can put the
429        // new APP3 at this position
430
431
432        $highest_APP = -1;
433
434        //Cycle through the header segments
435        for( $i = 0; $i < count( $jpeg_header_data ); $i++ )
436        {
437                // Check if we have found an APP segment at or below APP3,
438                if ( ( $jpeg_header_data[$i]['SegType'] >= 0xE0 ) && ( $jpeg_header_data[$i]['SegType'] <= 0xE3 ) )
439                {
440                        // Found an APP segment at or below APP12
441                        $highest_APP = $i;
442                }
443        }
444
445        // No preexisting Meta block found, insert a new one at the start of the header data.
446        array_splice($jpeg_header_data, $highest_APP + 1 , 0, array( array(     "SegType" => 0xE3,
447                                                                                "SegName" => "APP3",
448                                                                                "SegDesc" => $GLOBALS[ "JPEG_Segment_Descriptions" ][ 0xE1 ],
449                                                                                "SegData" => $packed_data ) ) );
450        return $jpeg_header_data;
451
452}
453
454/******************************************************************************
455* End of Function:     put_Meta_JPEG
456******************************************************************************/
457
458
459
460/******************************************************************************
461*
462* Function:     get_EXIF_TIFF
463*
464* Description:  Retrieves information from a Exchangeable Image File Format (EXIF)
465*               within a TIFF file and returns it in an array.
466*
467* Parameters:   filename - the filename of the TIFF image to process
468*
469* Returns:      OutputArray - Array of EXIF records
470*               FALSE - If an error occured in decoding
471*
472******************************************************************************/
473
474function get_EXIF_TIFF( $filename )
475{
476global $error_message ;
477        // Change: Added as of version 1.11
478        // Check if a wrapper is being used - these are not currently supported (see notes at top of file)
479        if ( ( stristr ( $filename, "http://" ) != FALSE ) || ( stristr ( $filename, "ftp://" ) != FALSE ) )
480        {
481                // A HTTP or FTP wrapper is being used - show a warning and abort
482                $error_message .= "HTTP and FTP wrappers are currently not supported with TIFF - See EXIF/TIFF functionality documentation - a local file must be specified<br>";
483                $error_message .= "To work on an internet file, copy it locally to start with:<br><br>\n";
484                $error_message .= "\$newfilename = tempnam ( \$dir, \"tmptiff\" );<br>\n";
485                $error_message .= "copy ( \"http://whatever.com\", \$newfilename );<br><br>\n";
486                return FALSE;
487        }
488
489
490        $filehnd = @fopen($filename, 'rb');
491
492        // Check if the file opened successfully
493        if ( ! $filehnd  )
494        {
495                // Could't open the file - exit
496                $error_message .= "<p>Could not open file $filename</p>\n";
497                return FALSE;
498        }
499
500        // Decode the Exif segment into an array and return it
501        $exif_data = process_TIFF_Header( $filehnd, "TIFF" );
502
503        // Close File
504        fclose($filehnd);
505        return $exif_data;
506}
507
508/******************************************************************************
509* End of Function:     get_EXIF_TIFF
510******************************************************************************/
511
512
513
514
515/******************************************************************************
516*
517* Function:     Interpret_EXIF_to_HTML
518*
519* Description:  Generates html detailing the contents an APP1 EXIF array
520*               which was retrieved with a get_EXIF_.... function.
521*               Can also be used for APP3 Meta arrays.
522*
523* Parameters:   Exif_array - the EXIF array,as read from get_EXIF_....
524*               filename - the name of the Image file being processed ( used
525*                          by scripts which displays EXIF thumbnails)
526*
527* Returns:      output_str - A string containing the HTML
528*
529******************************************************************************/
530
531function Interpret_EXIF_to_HTML( $Exif_array, $filename )
532{
533        // Create the string to receive the html output
534        $output_str = "";
535
536        // Check if the array to process is valid
537        if ( $Exif_array === FALSE )
538        {                // Exif Array is not valid - abort processing
539                return $output_str;
540        }
541      $output_str .= "<fieldset><legend>Contains " . $Exif_array[ 'Tags Name' ];
542        // Ouput the heading according to what type of tags were used in processing
543        if ( $Exif_array[ 'Tags Name' ] == "TIFF" ) {
544                $output_str .= " Exchangeable Image File Format (EXIF) " . $Exif_array[ 'Tags Name' ] . " Information</legend>";
545        } else if ( $Exif_array[ 'Tags Name' ] == "Meta" ) {
546                $output_str .= " Information (APP3)</legend>";
547        } else {
548                $output_str .= " Information </legend>";
549        }
550
551
552        // Check that there are actually items to process in the array
553        if ( count( $Exif_array ) < 1 )
554        {  // No items to process in array - abort processing
555                return  $output_str."</fieldset>" ;
556        }
557
558        // Output secondary heading
559         $output_str .="<fieldset id='".$Exif_array[ 'Tags Name' ]."_0' class='fieldset'> <legend>Main Image Information</legend>";
560        // Interpret the zeroth IFD to html
561         $output_str .= interpret_IFD( $Exif_array[0], $filename, $Exif_array['Byte_Align'] );
562 
563 
564        // Check if there is a first IFD to process
565        if ( array_key_exists( 1, $Exif_array ) )
566        {
567                // There is a first IFD for a thumbnail
568                // Add a heading for it to the output
569                $output_str .= "<fieldset id='".$Exif_array[ 'Tags Name' ]."_1' class='fieldset'> <legend>Thumbnail Information</legend>";
570                // Interpret the IFD to html and add it to the output
571                $output_str .= interpret_IFD( $Exif_array[1], $filename, $Exif_array['Byte_Align'] );           
572     
573        }
574 
575        // Cycle through any other IFD's
576        $i = 2;
577        while ( array_key_exists( $i, $Exif_array ) )
578        {
579                // Add a heading for the IFD
580                $output_str .= "<fieldset id='".$Exif_array[ 'Tags Name' ]."_".$i."' class='fieldset' > <legend>Image File Directory (IFD) $i Information</legend>";
581                // Interpret the IFD to html and add it to the output
582                $output_str .= interpret_IFD( $Exif_array[$i], $filename, $Exif_array['Byte_Align'] );
583                 
584                $i++;
585        }
586$output_str .="</fieldset>";
587        // Return the resulting HTML
588        return $output_str;
589}
590
591/******************************************************************************
592* End of Function:     Interpret_EXIF_to_HTML
593******************************************************************************/
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610/******************************************************************************
611*
612*         INTERNAL FUNCTIONS
613*
614******************************************************************************/
615
616
617
618
619
620
621
622
623
624
625
626/******************************************************************************
627*
628* Internal Function:     get_TIFF_Packed_Data
629*
630* Description:  Packs TIFF IFD data from EXIF or Meta into a form ready for
631*               either a JPEG EXIF/Meta segment or a TIFF file
632*               This function attempts to protect the contents of an EXIF makernote,
633*               by ensuring that it remains in the same position relative to the
634*               TIFF header
635*
636* Parameters:   tiff_data - the EXIF array,as read from get_EXIF_JPEG or get_Meta_JPEG
637*
638* Returns:      packed_data - A string containing packed segment
639*
640******************************************************************************/
641
642function get_TIFF_Packed_Data( $tiff_data )
643{
644        // Check that the segment is valid
645        if ( $tiff_data === FALSE )
646        {
647                return FALSE;
648        }
649
650        // Get the byte alignment
651        $Byte_Align = $tiff_data['Byte_Align'];
652
653        // Add the Byte Alignment to the Packed data
654        $packed_data = $Byte_Align;
655
656        // Add the TIFF ID to the Packed Data
657        $packed_data .= put_IFD_Data_Type( 42, 3, $Byte_Align );
658
659        // Create a string for the makernote
660        $makernote = "";
661
662        // Check if the makernote exists
663        if ( $tiff_data[ 'Makernote_Tag' ] !== FALSE )
664        {
665                // A makernote exists - We need to ensure that it stays in the same position as it was
666                // Put the Makernote before any of the IFD's by padding zeros to the correct offset
667                $makernote .= str_repeat("\x00",( $tiff_data[ 'Makernote_Tag' ][ 'Offset' ] - 8 ) );
668                $makernote .= $tiff_data[ 'Makernote_Tag' ]['Data'];
669        }
670
671        // Calculage where the zeroth ifd will be
672        $ifd_offset = strlen( $makernote ) + 8;
673
674        // Add the Zeroth IFD pointer to the packed data
675        $packed_data .= put_IFD_Data_Type( $ifd_offset, 4, $Byte_Align );
676
677        // Add the makernote to the packed data (if there was one)
678        $packed_data .= $makernote;
679
680        //Add the IFD's to the packed data
681        $packed_data .= get_IFD_Array_Packed_Data( $tiff_data, $ifd_offset, $Byte_Align );
682
683        // Return the result
684        return $packed_data;
685}
686
687/******************************************************************************
688* End of Function:     get_TIFF_Packed_Data
689******************************************************************************/
690
691
692
693
694/******************************************************************************
695*
696* Internal Function:     get_IFD_Array_Packed_Data
697*
698* Description:  Packs a chain of IFD's from EXIF or Meta segments into a form
699*               ready for either a JPEG EXIF/Meta segment or a TIFF file
700*
701* Parameters:   ifd_data - the IFD chain array, as read from get_EXIF_JPEG or get_Meta_JPEG
702*               Zero_IFD_offset - The offset to the first IFD from the start of the TIFF header
703*               Byte_Align - the Byte alignment to use - "MM" or "II"
704*
705* Returns:      packed_data - A string containing packed IFD's
706*
707******************************************************************************/
708
709function get_IFD_Array_Packed_Data( $ifd_data, $Zero_IFD_offset, $Byte_Align )
710{
711        // Create a string to receive the packed output
712        $packed_data = "";
713
714        // Count the IFDs
715        $ifd_count = 0;
716        foreach( $ifd_data as $key => $IFD )
717        {
718                // Make sure we only count the IFD's, not other information keys
719                if ( is_numeric( $key ) )
720                {
721                        $ifd_count++;
722                }
723        }
724
725
726        // Cycle through each IFD,
727        for ( $ifdno = 0; $ifdno < $ifd_count; $ifdno++ )
728        {
729                // Check if this IFD is the last one
730                if ( $ifdno == $ifd_count - 1 )
731                {
732                        // This IFD is the last one, get it's packed data
733                        $packed_data .= get_IFD_Packed_Data( $ifd_data[ $ifdno ], $Zero_IFD_offset +strlen($packed_data), $Byte_Align, FALSE );
734                }
735                else
736                {
737                        // This IFD is NOT the last one, get it's packed data
738                        $packed_data .= get_IFD_Packed_Data( $ifd_data[ $ifdno ], $Zero_IFD_offset +strlen($packed_data), $Byte_Align, TRUE );
739                }
740
741        }
742
743        // Return the packed output
744        return $packed_data;
745}
746
747/******************************************************************************
748* End of Function:     get_IFD_Array_Packed_Data
749******************************************************************************/
750
751
752
753/******************************************************************************
754*
755* Internal Function:     get_IFD_Packed_Data
756*
757* Description:  Packs an IFD from EXIF or Meta segments into a form
758*               ready for either a JPEG EXIF/Meta segment or a TIFF file
759*
760* Parameters:   ifd_data - the IFD chain array, as read from get_EXIF_JPEG or get_Meta_JPEG
761*               IFD_offset - The offset to the IFD from the start of the TIFF header
762*               Byte_Align - the Byte alignment to use - "MM" or "II"
763*               Another_IFD - boolean - false if this is the last IFD in the chain
764*                                     - true if it is not the last
765*
766* Returns:      packed_data - A string containing packed IFD's
767*
768******************************************************************************/
769
770function get_IFD_Packed_Data( $ifd_data, $IFD_offset, $Byte_Align, $Another_IFD )
771{
772
773        $ifd_body_str = "";
774        $ifd_data_str = "";
775
776        $Tag_Definitions_Name = $ifd_data[ 'Tags Name' ];
777
778
779        // Count the Tags in this IFD
780        $tag_count = 0;
781        foreach( $ifd_data as $key => $tag )
782        {
783                // Make sure we only count the Tags, not other information keys
784                if ( is_numeric( $key ) )
785                {
786                        $tag_count++;
787                }
788        }
789
790        // Add the Tag count to the packed data
791        $packed_data = put_IFD_Data_Type( $tag_count, 3, $Byte_Align );
792
793        // Calculate the total length of the IFD (without the offset data)
794        $IFD_len = 2 + $tag_count * 12 + 4;
795
796
797        // Cycle through each tag
798        foreach( $ifd_data as $key => $tag )
799        {
800                // Make sure this is a tag, not another information key
801                if ( is_numeric( $key ) )
802                {
803
804                        // Add the tag number to the packed data
805                        $ifd_body_str .= put_IFD_Data_Type( $tag[ 'Tag Number' ], 3, $Byte_Align );
806
807                        // Add the Data type to the packed data
808                        $ifd_body_str .= put_IFD_Data_Type( $tag['Data Type'], 3, $Byte_Align );
809
810                        // Check if this is a Print Image Matching entry
811                        if ( $tag['Type'] == "PIM" )
812                        {
813                                // This is a Print Image Matching entry,
814                                // encode it
815                                $data = Encode_PIM( $tag, $Byte_Align );
816                        }
817                                // Check if this is a IPTC/NAA Record within the EXIF IFD
818                        else if ( ( ( $Tag_Definitions_Name == "EXIF" ) || ( $Tag_Definitions_Name == "TIFF" ) ) &&
819                                  ( $tag[ 'Tag Number' ] == 33723 ) )
820                        {
821                                // This is a IPTC/NAA Record, encode it
822                                $data = put_IPTC( $tag['Data'] );
823                        }
824                                // Change: Check for embedded XMP as of version 1.11
825                                // Check if this is a XMP Record within the EXIF IFD
826                        else if ( ( ( $Tag_Definitions_Name == "EXIF" ) || ( $Tag_Definitions_Name == "TIFF" ) ) &&
827                                  ( $tag[ 'Tag Number' ] == 700 ) )
828                        {
829                                // This is a XMP Record, encode it
830                                $data = write_XMP_array_to_text( $tag['Data'] );
831                        }
832                                // Change: Check for embedded IRB as of version 1.11
833                                // Check if this is a Photoshop IRB Record within the EXIF IFD
834                        else if ( ( ( $Tag_Definitions_Name == "EXIF" ) || ( $Tag_Definitions_Name == "TIFF" ) ) &&
835                                  ( $tag[ 'Tag Number' ] == 34377 ) )
836                        {
837                                // This is a Photoshop IRB Record, encode it
838                                $data = pack_Photoshop_IRB_Data( $tag['Data'] );
839                        }
840                                // Exif Thumbnail Offset
841                        else if ( ( $tag[ 'Tag Number' ] == 513 ) && ( $Tag_Definitions_Name == "TIFF" ) )
842                        {
843                                        // The Exif Thumbnail Offset is a pointer but of type Long, not Unknown
844                                        // Hence we need to put the data into the packed string separately
845                                        // Calculate the thumbnail offset
846                                        $data_offset = $IFD_offset + $IFD_len + strlen($ifd_data_str);
847
848                                        // Create the Offset for the IFD
849                                        $data = put_IFD_Data_Type( $data_offset, 4, $Byte_Align );
850
851                                        // Store the thumbnail
852                                        $ifd_data_str .= $tag['Data'];
853                        }
854                                // Exif Thumbnail Length
855                        else if ( ( $tag[ 'Tag Number' ] == 514 ) && ( $Tag_Definitions_Name == "TIFF" ) )
856                        {
857                                        // Encode the Thumbnail Length
858                                        $data = put_IFD_Data_Type( strlen($ifd_data[513]['Data']), 4, $Byte_Align );
859                        }
860                                // Sub-IFD
861                        else if ( $tag['Type'] == "SubIFD" )
862                        {
863                                        // This is a Sub-IFD
864                                        // Calculate the offset to the start of the Sub-IFD
865                                        $data_offset = $IFD_offset + $IFD_len + strlen($ifd_data_str);
866                                        // Get the packed data for the IFD chain as the data for this tag
867                                        $data = get_IFD_Array_Packed_Data( $tag['Data'], $data_offset, $Byte_Align );
868                        }
869                        else
870                        {
871                                // Not a special tag
872
873                                // Create a string to receive the data
874                                $data = "";
875
876                                // Check if this is a type Unknown tag
877                                if ( $tag['Data Type'] != 7 )
878                                {
879                                        // NOT type Unknown
880                                        // Cycle through each data value and add it to the data string
881                                        foreach( $tag[ 'Data' ] as $data_val )
882                                        {
883                                                $data .= put_IFD_Data_Type( $data_val, $tag['Data Type'], $Byte_Align );
884                                        }
885                                }
886                                else
887                                {
888                                        // This is a type Unknown - just add the data as is to the data string
889                                        $data .= $tag[ 'Data' ];
890                                }
891                        }
892
893                        // Pad the data string out to at least 4 bytes
894                        $data = str_pad ( $data, 4, "\x00" );
895
896
897                        // Check if the data type is an ASCII String or type Unknown
898                        if ( ( $tag['Data Type'] == 2 ) || ( $tag['Data Type'] == 7 ) )
899                        {
900                                // This is an ASCII String or type Unknown
901                                // Add the Length of the string to the packed data as the Count
902                                $ifd_body_str .= put_IFD_Data_Type( strlen($data), 4, $Byte_Align );
903                        }
904                        else
905                        {
906                                // Add the array count to the packed data as the Count
907                                $ifd_body_str .= put_IFD_Data_Type( count($tag[ 'Data' ]), 4, $Byte_Align );
908                        }
909
910
911                        // Check if the data is over 4 bytes long
912                        if ( strlen( $data ) > 4 )
913                        {
914                                // Data is longer than 4 bytes - it needs to be offset
915                                // Check if this entry is the Maker Note
916                                if ( ( $Tag_Definitions_Name == "EXIF" ) && ( $tag[ 'Tag Number' ] == 37500 ) )
917                                {
918                                        // This is the makernote - It will have already been stored
919                                        // at its original offset to help preserve it
920                                        // all we need to do is add the Offset to the IFD packed data
921                                        $data_offset = $tag[ 'Offset' ];
922
923                                        $ifd_body_str .= put_IFD_Data_Type( $data_offset, 4, $Byte_Align );
924                                }
925                                else
926                                {
927                                        // This is NOT the makernote
928                                        // Calculate the data offset
929                                        $data_offset = $IFD_offset + $IFD_len + strlen($ifd_data_str);
930
931                                        // Add the offset to the IFD packed data
932                                        $ifd_body_str .= put_IFD_Data_Type( $data_offset, 4, $Byte_Align );
933
934                                        // Add the data to the offset packed data
935                                        $ifd_data_str .= $data;
936                                }
937                        }
938                        else
939                        {
940                                // Data is less than or equal to 4 bytes - Add it to the packed IFD data as is
941                                $ifd_body_str .= $data;
942                        }
943
944                }
945        }
946
947        // Assemble the IFD body onto the packed data
948        $packed_data .= $ifd_body_str;
949
950        // Check if there is another IFD after this one
951        if( $Another_IFD === TRUE )
952        {
953                // There is another IFD after this
954                // Calculate the Next-IFD offset so that it goes immediately after this IFD
955                $next_ifd_offset = $IFD_offset + $IFD_len + strlen($ifd_data_str);
956        }
957        else
958        {
959                // There is NO IFD after this - indicate with offset=0
960                $next_ifd_offset = 0;
961        }
962
963        // Add the Next-IFD offset to the packed data
964        $packed_data .= put_IFD_Data_Type( $next_ifd_offset, 4, $Byte_Align );
965
966        // Add the offset data to the packed data
967        $packed_data .= $ifd_data_str;
968
969        // Return the resulting packed data
970        return $packed_data;
971}
972
973/******************************************************************************
974* End of Function:     get_IFD_Packed_Data
975******************************************************************************/
976
977
978
979
980
981/******************************************************************************
982*
983* Internal Function:     process_TIFF_Header
984*
985* Description:  Decodes the information stored in a TIFF header and it's
986*               Image File Directories (IFD's). This information is returned
987*               in an array
988*
989* Parameters:   filehnd - The handle of a open image file, positioned at the
990*                          start of the TIFF header
991*               Tag_Definitions_Name - The name of the Tag Definitions group
992*                                      within the global array IFD_Tag_Definitions
993*
994*
995* Returns:      OutputArray - Array of IFD records
996*               FALSE - If an error occured in decoding
997*
998******************************************************************************/
999
1000function process_TIFF_Header( $filehnd, $Tag_Definitions_Name )
1001{
1002
1003
1004        // Save the file position where the TIFF header starts, as offsets are relative to this position
1005        $Tiff_start_pos = ftell( $filehnd );
1006
1007
1008
1009        // Read the eight bytes of the TIFF header
1010        $DataStr = network_safe_fread( $filehnd, 8 );
1011
1012        // Check that we did get all eight bytes
1013        if ( strlen( $DataStr ) != 8 )
1014        {
1015                return FALSE;   // Couldn't read the TIFF header properly
1016        }
1017
1018        $pos = 0;
1019        // First two bytes indicate the byte alignment - should be 'II' or 'MM'
1020        // II = Intel (LSB first, MSB last - Little Endian)
1021        // MM = Motorola (MSB first, LSB last - Big Endian)
1022        $Byte_Align = substr( $DataStr, $pos, 2 );
1023
1024
1025
1026        // Check the Byte Align Characters for validity
1027        if ( ( $Byte_Align != "II" ) && ( $Byte_Align != "MM" ) )
1028        {
1029                // Byte align field is invalid - we won't be able to decode file
1030                return FALSE;
1031        }
1032
1033        // Skip over the Byte Align field which was just read
1034        $pos += 2;
1035
1036        // Next two bytes are TIFF ID - should be value 42 with the appropriate byte alignment
1037        $TIFF_ID = substr( $DataStr, $pos, 2 );
1038
1039        if ( get_IFD_Data_Type( $TIFF_ID, 3, $Byte_Align ) != 42 )
1040        {
1041                // TIFF header ID not found
1042                return FALSE;
1043        }
1044
1045        // Skip over the TIFF ID field which was just read
1046        $pos += 2;
1047
1048
1049        // Next four bytes are the offset to the first IFD
1050        $offset_str = substr( $DataStr, $pos, 4 );
1051        $offset = get_IFD_Data_Type( $offset_str, 4, $Byte_Align );
1052
1053        // Done reading TIFF Header
1054
1055
1056        // Move to first IFD
1057
1058        if ( fseek( $filehnd, $Tiff_start_pos + $offset ) !== 0 )
1059        {
1060                // Error seeking to position of first IFD
1061                return FALSE;
1062        }
1063
1064
1065
1066        // Flag that a makernote has not been found yet
1067        $GLOBALS[ "Maker_Note_Tag" ] = FALSE;
1068
1069        // Read the IFD chain into an array
1070        $Output_Array = read_Multiple_IFDs( $filehnd, $Tiff_start_pos, $Byte_Align, $Tag_Definitions_Name );
1071
1072        // Check if a makernote was found
1073        if ( $GLOBALS[ "Maker_Note_Tag" ] != FALSE )
1074        {
1075                // Makernote was found - Process it
1076                // The makernote needs to be processed after all other
1077                // tags as it may require some of the other tags in order
1078                // to be processed properly
1079                $GLOBALS[ "Maker_Note_Tag" ] = Read_Makernote_Tag( $GLOBALS[ "Maker_Note_Tag" ], $Output_Array, $filehnd );
1080
1081        }
1082
1083        $Output_Array[ 'Makernote_Tag' ] = $GLOBALS[ "Maker_Note_Tag" ];
1084
1085        // Save the Name of the Tags used in the output array
1086        $Output_Array[ 'Tags Name' ] = $Tag_Definitions_Name;
1087
1088
1089
1090        // Save the Byte alignment
1091        $Output_Array['Byte_Align'] = $Byte_Align;
1092
1093
1094        // Return the output array
1095        return $Output_Array ;
1096}
1097
1098/******************************************************************************
1099* End of Function:     process_TIFF_Header
1100******************************************************************************/
1101
1102
1103
1104
1105
1106
1107/******************************************************************************
1108*
1109* Internal Function:     read_Multiple_IFDs
1110*
1111* Description:  Reads and interprets a chain of standard Image File Directories (IFD's),
1112*               and returns the entries in an array. This chain is made up from IFD's
1113*               which have a pointer to the next IFD. IFD's are read until the next
1114*               pointer indicates there are no more
1115*
1116* Parameters:   filehnd - a handle for the image file being read, positioned at the
1117*                         start of the IFD chain
1118*               Tiff_offset - The offset of the TIFF header from the start of the file
1119*               Byte_Align - either "MM" or "II" indicating Motorola or Intel Byte alignment
1120*               Tag_Definitions_Name - The name of the Tag Definitions group within the global array IFD_Tag_Definitions
1121*               local_offsets - True indicates that offset data should be interpreted as being relative to the start of the currrent entry
1122*                               False (normal) indicates offests are relative to start of Tiff header as per IFD standard
1123*               read_next_ptr - True (normal) indicates that a pointer to the next IFD should be read at the end of the IFD
1124*                               False indicates that no pointer follows the IFD
1125*
1126*
1127* Returns:      OutputArray - Array of IFD entries
1128*
1129******************************************************************************/
1130
1131function read_Multiple_IFDs( $filehnd, $Tiff_offset, $Byte_Align, $Tag_Definitions_Name, $local_offsets = FALSE, $read_next_ptr = TRUE )
1132{
1133global $error_message ;
1134        // Start at the offset of the first IFD
1135        $Next_Offset = 0;
1136
1137        do
1138        {
1139                // Read an IFD
1140                list($IFD_Array , $Next_Offset) = read_IFD_universal( $filehnd, $Tiff_offset, $Byte_Align, $Tag_Definitions_Name, $local_offsets, $read_next_ptr );
1141
1142                // Move to the position of the next IFD
1143                if ( fseek( $filehnd, $Tiff_offset + $Next_Offset ) !== 0 )
1144                {
1145                        // Error seeking to position of next IFD
1146                        $error_message .= "<p>Error: Corrupted EXIF</p>\n";
1147                        return FALSE;
1148                }
1149
1150                $Output_Array[] = $IFD_Array;
1151
1152
1153        } while ( $Next_Offset != 0 );      // Until the Next IFD Offset is zero
1154
1155
1156        // return resulting array
1157
1158        return $Output_Array ;
1159}
1160
1161/******************************************************************************
1162* End of Function:     read_Multiple_IFDs
1163******************************************************************************/
1164
1165
1166
1167
1168
1169
1170
1171/******************************************************************************
1172*
1173* Internal Function:     read_IFD_universal
1174*
1175* Description:  Reads and interprets a standard or Non-standard Image File
1176*               Directory (IFD), and returns the entries in an array
1177*
1178* Parameters:   filehnd - a handle for the image file being read, positioned at the start
1179*                         of the IFD
1180*               Tiff_offset - The offset of the TIFF header from the start of the file
1181*               Byte_Align - either "MM" or "II" indicating Motorola or Intel Byte alignment
1182*               Tag_Definitions_Name - The name of the Tag Definitions group within the global array IFD_Tag_Definitions
1183*               local_offsets - True indicates that offset data should be interpreted as being relative to the start of the currrent entry
1184*                               False (normal) indicates offests are relative to start of Tiff header as per IFD standard
1185*               read_next_ptr - True (normal) indicates that a pointer to the next IFD should be read at the end of the IFD
1186*                               False indicates that no pointer follows the IFD
1187*
1188* Returns:      OutputArray - Array of IFD entries
1189*               Next_Offset - Offset to next IFD (zero = no next IFD)
1190*
1191******************************************************************************/
1192
1193function read_IFD_universal( $filehnd, $Tiff_offset, $Byte_Align, $Tag_Definitions_Name, $local_offsets = FALSE, $read_next_ptr = TRUE )
1194{
1195global $error_message ;
1196
1197        if ( ( $filehnd == NULL ) || ( feof( $filehnd ) ) )
1198        {
1199                return array (FALSE , 0);
1200        }
1201
1202        // Record the Name of the Tag Group used for this IFD in the output array
1203        $OutputArray[ 'Tags Name' ] = $Tag_Definitions_Name;
1204
1205        // Record the offset of the TIFF header in the output array
1206        $OutputArray[ 'Tiff Offset' ] = $Tiff_offset;
1207
1208        // First 2 bytes of IFD are number of entries in the IFD
1209        $No_Entries_str = network_safe_fread( $filehnd, 2 );
1210        $No_Entries = get_IFD_Data_Type( $No_Entries_str, 3, $Byte_Align );
1211
1212
1213        // If the data is corrupt, the number of entries may be huge, which will cause errors
1214        // This is often caused by a lack of a Next-IFD pointer
1215        if ( $No_Entries> 10000 )
1216        {
1217                // Huge number of entries - abort
1218                $error_message .= "<p>".$No_Entries."Error: huge number of EXIF entries - EXIF is probably Corrupted</p>\n";
1219
1220                return array ( FALSE , 0);
1221        }
1222
1223        // If the data is corrupt or just stupid, the number of entries may zero,
1224        // Indicate this by returning false
1225        if ( $No_Entries === 0 )
1226        {
1227                // No entries - abort
1228                return array ( FALSE , 0);
1229        }
1230
1231        // Save the file position where first IFD record starts as non-standard offsets
1232        // need to know this to calculate an absolute offset
1233        $IFD_first_rec_pos = ftell( $filehnd );
1234
1235
1236        // Read in the IFD structure
1237        $IFD_Data = network_safe_fread( $filehnd, 12 * $No_Entries );
1238
1239        // Check if the entire IFD was able to be read
1240        if ( strlen( $IFD_Data ) != (12 * $No_Entries) )
1241        {
1242                // Couldn't read the IFD Data properly, Some Casio files have no Next IFD pointer, hence cause this error
1243                $error_message .= "<p>Error: EXIF Corrupted</p>\n";
1244                return array(FALSE, 0);
1245        }
1246
1247
1248        // Last 4 bytes of a standard IFD are the offset to the next IFD
1249        // Some NON-Standard IFD implementations do not have this, hence causing problems if it is read
1250
1251        // If the Next IFD pointer has been requested to be read,
1252        if ( $read_next_ptr )
1253        {
1254                // Read the pointer to the next IFD
1255
1256                $Next_Offset_str = network_safe_fread( $filehnd, 4 );
1257                $Next_Offset = get_IFD_Data_Type( $Next_Offset_str, 4, $Byte_Align );
1258        }
1259        else
1260        {
1261                // Otherwise set the pointer to zero ( no next IFD )
1262                $Next_Offset = 0;
1263        }
1264
1265
1266
1267        // Initialise current position to the start
1268        $pos = 0;
1269
1270
1271        // Loop for reading IFD entries
1272
1273        for ( $i = 0; $i < $No_Entries; $i++ )
1274        {
1275                // First 2 bytes of IFD entry are the tag number ( Unsigned Short )
1276                $Tag_No_str = substr( $IFD_Data, $pos, 2 );
1277                $Tag_No = get_IFD_Data_Type( $Tag_No_str, 3, $Byte_Align );
1278                $pos += 2;
1279
1280                // Next 2 bytes of IFD entry are the data format ( Unsigned Short )
1281                $Data_Type_str = substr( $IFD_Data, $pos, 2 );
1282                $Data_Type = get_IFD_Data_Type( $Data_Type_str, 3, $Byte_Align );
1283                $pos += 2;
1284
1285                // If Datatype is not between 1 and 12, then skip this entry, it is probably corrupted or custom
1286                if (( $Data_Type > 12 ) || ( $Data_Type < 1 ) )
1287                {
1288                        $pos += 8;
1289                        continue 1;  // Stop trying to process the tag any further and skip to the next one
1290                }
1291
1292                // Next 4 bytes of IFD entry are the data count ( Unsigned Long )
1293                $Data_Count_str = substr( $IFD_Data, $pos, 4 );
1294                $Data_Count = get_IFD_Data_Type( $Data_Count_str, 4, $Byte_Align );
1295                $pos += 4;
1296
1297                if ( $Data_Count > 100000 )
1298                {
1299                     //   $error_message .= "<p>Error: huge EXIF data count - EXIF is probably Corrupted</p>\n";
1300
1301                        // Some Casio files have no Next IFD pointer, hence cause errors
1302
1303                        return array ( FALSE , 0);
1304                }
1305
1306                // Total Data size is the Data Count multiplied by the size of the Data Type
1307                $Total_Data_Size = $GLOBALS['IFD_Data_Sizes'][ $Data_Type ] * $Data_Count;
1308
1309                $Data_Start_pos = -1;
1310
1311                // If the total data size is larger than 4 bytes, then the data part is the offset to the real data
1312                if ( $Total_Data_Size > 4 )
1313                {
1314                        // Not enough room for data - offset provided instead
1315                        $Data_Offset_str = substr( $IFD_Data, $pos, 4 );
1316                        $Data_Start_pos = get_IFD_Data_Type( $Data_Offset_str, 4, $Byte_Align );
1317
1318
1319                        // In some NON-STANDARD makernotes, the offset is relative to the start of the current IFD entry
1320                        if ( $local_offsets )
1321                        {
1322                                // This is a NON-Standard IFD, seek relative to the start of the current tag
1323                                fseek( $filehnd, $IFD_first_rec_pos +  $pos - 8 + $Data_Start_pos );
1324                        }
1325                        else
1326                        {
1327                                // This is a normal IFD, seek relative to the start of the TIFF header
1328                                fseek( $filehnd, $Tiff_offset + $Data_Start_pos );
1329                        }
1330
1331                        // Read the data block from the offset position
1332                        $DataStr = network_safe_fread( $filehnd, $Total_Data_Size );
1333                }
1334                else
1335                {
1336                        // The data block is less than 4 bytes, and is provided in the IFD entry, so read it
1337                        $DataStr = substr( $IFD_Data, $pos, $Total_Data_Size );
1338                }
1339
1340                // Increment the position past the data
1341                $pos += 4;
1342
1343
1344                // Now create the entry for output array
1345
1346                $Data_Array = array( );
1347
1348
1349                // Read the data items from the data block
1350
1351                if ( ( $Data_Type != 2 ) && ( $Data_Type != 7 ) )
1352                {
1353                        // The data type is Numerical, Read the data items from the data block
1354                        for ( $j = 0; $j < $Data_Count; $j++ )
1355                        {
1356                                $Part_Data_Str = substr( $DataStr, $j * $GLOBALS['IFD_Data_Sizes'][ $Data_Type ], $GLOBALS['IFD_Data_Sizes'][ $Data_Type ] );
1357                                $Data_Array[] = get_IFD_Data_Type( $Part_Data_Str, $Data_Type, $Byte_Align );
1358                        }
1359                }
1360                elseif ( $Data_Type == 2 )
1361                {
1362                        // The data type is String(s)   (type 2)
1363
1364                        // Strip the last terminating Null
1365                        $DataStr = substr( $DataStr, 0, strlen($DataStr)-1 );
1366
1367                        // Split the data block into multiple strings whereever there is a Null
1368                        $Data_Array = explode( "\x00", $DataStr );
1369                }
1370                else
1371                {
1372                        // The data type is Unknown (type 7)
1373                        // Do nothing to data
1374                        $Data_Array = $DataStr;
1375                }
1376
1377
1378                // If this is a Sub-IFD entry,
1379                if ( ( array_key_exists( $Tag_No, $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name] ) ) &&
1380                     ( "SubIFD" == $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag_No ]['Type'] ) )
1381                {
1382                        // This is a Sub-IFD entry, go and process the data forming Sub-IFD and use its output array as the new data for this entry
1383                        fseek( $filehnd, $Tiff_offset + $Data_Array[0] );
1384                        $Data_Array = read_Multiple_IFDs( $filehnd, $Tiff_offset, $Byte_Align, $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag_No ]['Tags Name'] );
1385                }
1386
1387                $desc = "";
1388                $units = "";
1389
1390                // Check if this tag exists in the list of tag definitions,
1391
1392                if ( array_key_exists ( $Tag_No, $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name]) )
1393                {
1394
1395                        if ( array_key_exists ( 'Description', $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag_No ] ) )
1396                        {
1397                                $desc = $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag_No ]['Description'];
1398                        }
1399
1400                        if ( array_key_exists ( 'Units', $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag_No ] ) )
1401                        {
1402                                $units = $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag_No ]['Units'];
1403                        }
1404
1405                        // Tag exists in definitions, append details to output array
1406                        $OutputArray[ $Tag_No ] = array (       "Tag Number"      => $Tag_No,
1407                                                                "Tag Name"        => $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag_No ]['Name'],
1408                                                                "Tag Description" => $desc,
1409                                                                "Data Type"       => $Data_Type,
1410                                                                "Type"            => $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag_No ]['Type'],
1411                                                                "Units"           => $units,
1412                                                                "Data"            => $Data_Array );
1413
1414                }
1415                else
1416                {
1417                        // Tag doesnt exist in definitions, append unknown details to output array
1418
1419                        $OutputArray[ $Tag_No ] = array (       "Tag Number"      => $Tag_No,
1420                                                                "Tag Name"        => "Unknown Tag #" . $Tag_No,
1421                                                                "Tag Description" => "",
1422                                                                "Data Type"       => $Data_Type,
1423                                                                "Type"            => "Unknown",
1424                                                                "Units"           => "",
1425                                                                "Data"            => $Data_Array );
1426                }
1427
1428
1429
1430                // Some information of type "Unknown" (type 7) might require information about
1431                // how it's position and byte alignment in order to be decoded
1432                if ( $Data_Type == 7 )
1433                {
1434                        $OutputArray[ $Tag_No ]['Offset'] = $Data_Start_pos;
1435                        $OutputArray[ $Tag_No ]['Byte Align'] = $Byte_Align;
1436                }
1437
1438
1439                ////////////////////////////////////////////////////////////////////////
1440                // Special Data handling
1441                ////////////////////////////////////////////////////////////////////////
1442
1443
1444                // Check if this is a Print Image Matching entry
1445                if ( $OutputArray[ $Tag_No ]['Type'] == "PIM" )
1446                {
1447                        // This is a Print Image Matching entry, decode it.
1448                        $OutputArray[ $Tag_No ] = Decode_PIM( $OutputArray[ $Tag_No ], $Tag_Definitions_Name );
1449                }
1450
1451
1452                // Interpret the entry into a text string using a custom interpreter
1453                $text_val = get_Tag_Text_Value( $OutputArray[ $Tag_No ], $Tag_Definitions_Name );
1454
1455                // Check if a text string was generated
1456                if ( $text_val !== FALSE )
1457                {
1458                        // A string was generated, append it to the output array entry
1459                        $OutputArray[ $Tag_No ]['Text Value'] = $text_val;
1460                        $OutputArray[ $Tag_No ]['Decoded'] = TRUE;
1461                }
1462                else
1463                {
1464                        // A string was NOT generated, append a generic string to the output array entry
1465                        $OutputArray[ $Tag_No ]['Text Value'] = get_IFD_value_as_text( $OutputArray[ $Tag_No ] )  . " " . $units;
1466                        $OutputArray[ $Tag_No ]['Decoded'] = FALSE;
1467                }
1468
1469
1470
1471
1472                // Check if this entry is the Maker Note
1473                if ( ( $Tag_Definitions_Name == "EXIF" ) && ( $Tag_No == 37500 ) )
1474                {
1475
1476                        // Save some extra information which will allow Makernote Decoding with the output array entry
1477                        $OutputArray[ $Tag_No ]['Offset'] = $Data_Start_pos;
1478                        $OutputArray[ $Tag_No ][ 'Tiff Offset' ] = $Tiff_offset;
1479                        $OutputArray[ $Tag_No ]['ByteAlign'] = $Byte_Align;
1480
1481                        // Save a pointer to this entry for Maker note processing later
1482                        $GLOBALS[ "Maker_Note_Tag" ] = & $OutputArray[ $Tag_No ];
1483                }
1484
1485
1486                // Check if this is a IPTC/NAA Record within the EXIF IFD
1487                if ( ( ( $Tag_Definitions_Name == "EXIF" ) || ( $Tag_Definitions_Name == "TIFF" ) ) &&
1488                     ( $Tag_No == 33723 ) )
1489                {
1490                        // This is a IPTC/NAA Record, interpret it and put result in the data for this entry
1491                        $OutputArray[ $Tag_No ]['Data'] = get_IPTC( $DataStr );
1492                        $OutputArray[ $Tag_No ]['Decoded'] = TRUE;
1493                }
1494                // Change: Check for embedded XMP as of version 1.11
1495                // Check if this is a XMP Record within the EXIF IFD
1496                if ( ( ( $Tag_Definitions_Name == "EXIF" ) || ( $Tag_Definitions_Name == "TIFF" ) ) &&
1497                     ( $Tag_No == 700 ) )
1498                {
1499                        // This is a XMP Record, interpret it and put result in the data for this entry
1500                        $OutputArray[ $Tag_No ]['Data'] =  read_XMP_array_from_text( $DataStr );
1501                        $OutputArray[ $Tag_No ]['Decoded'] = TRUE;
1502                }
1503
1504                // Change: Check for embedded IRB as of version 1.11
1505                // Check if this is a Photoshop IRB Record within the EXIF IFD
1506                if ( ( ( $Tag_Definitions_Name == "EXIF" ) || ( $Tag_Definitions_Name == "TIFF" ) ) &&
1507                     ( $Tag_No == 34377 ) )
1508                {
1509                        // This is a Photoshop IRB Record, interpret it and put result in the data for this entry
1510                        $OutputArray[ $Tag_No ]['Data'] = unpack_Photoshop_IRB_Data( $DataStr );
1511                        $OutputArray[ $Tag_No ]['Decoded'] = TRUE;
1512                }
1513
1514                // Exif Thumbnail
1515                // Check that both the thumbnail length and offset entries have been processed,
1516                // and that this is one of them
1517                if ( ( ( ( $Tag_No == 513 ) && ( array_key_exists( 514, $OutputArray ) ) ) ||
1518                       ( ( $Tag_No == 514 ) && ( array_key_exists( 513, $OutputArray ) ) ) )  &&
1519                     ( $Tag_Definitions_Name == "TIFF" ) )
1520                {
1521                        // Seek to the start of the thumbnail using the offset entry
1522                        fseek( $filehnd, $Tiff_offset + $OutputArray[513]['Data'][0] );
1523
1524                        // Read the thumbnail data, and replace the offset data with the thumbnail
1525                        $OutputArray[513]['Data'] = network_safe_fread( $filehnd, $OutputArray[514]['Data'][0] );
1526                }
1527
1528
1529                // Casio Thumbnail
1530                // Check that both the thumbnail length and offset entries have been processed,
1531                // and that this is one of them
1532                if ( ( ( ( $Tag_No == 0x0004 ) && ( array_key_exists( 0x0003, $OutputArray ) ) ) ||
1533                       ( ( $Tag_No == 0x0003 ) && ( array_key_exists( 0x0004, $OutputArray ) ) ) )  &&
1534                     ( $Tag_Definitions_Name == "Casio Type 2" ) )
1535                {
1536                        // Seek to the start of the thumbnail using the offset entry
1537                        fseek( $filehnd, $Tiff_offset + $OutputArray[0x0004]['Data'][0] );
1538
1539                        // Read the thumbnail data, and replace the offset data with the thumbnail
1540                        $OutputArray[0x0004]['Data'] = network_safe_fread( $filehnd, $OutputArray[0x0003]['Data'][0] );
1541                }
1542
1543                // Minolta Thumbnail
1544                // Check that both the thumbnail length and offset entries have been processed,
1545                // and that this is one of them
1546                if ( ( ( ( $Tag_No == 0x0088 ) && ( array_key_exists( 0x0089, $OutputArray ) ) ) ||
1547                       ( ( $Tag_No == 0x0089 ) && ( array_key_exists( 0x0088, $OutputArray ) ) ) )  &&
1548                     ( $Tag_Definitions_Name == "Olympus" ) )
1549                {
1550
1551                        // Seek to the start of the thumbnail using the offset entry
1552                        fseek( $filehnd, $Tiff_offset + $OutputArray[0x0088]['Data'][0] );
1553
1554                        // Read the thumbnail data, and replace the offset data with the thumbnail
1555                        $OutputArray[0x0088]['Data'] = network_safe_fread( $filehnd, $OutputArray[0x0089]['Data'][0] );
1556
1557                        // Sometimes the minolta thumbnail data is empty (or the offset is corrupt, which results in the same thing)
1558
1559                        // Check if the thumbnail data exists
1560                        if ( $OutputArray[0x0088]['Data'] != "" )
1561                        {
1562                                // Thumbnail exists
1563
1564                                // Minolta Thumbnails are missing their first 0xFF for some reason,
1565                                // which is replaced with some weird character, so fix this
1566                                $OutputArray[0x0088]['Data']{0} = "\xFF";
1567                        }
1568                        else
1569                        {
1570                                // Thumbnail doesnt exist - make it obvious
1571                                $OutputArray[0x0088]['Data'] = FALSE;
1572                        }
1573                }
1574
1575        }
1576
1577
1578
1579
1580
1581
1582
1583        // Return the array of IFD entries and the offset to the next IFD
1584
1585        return array ($OutputArray , $Next_Offset);
1586}
1587
1588
1589
1590/******************************************************************************
1591* End of Function:     read_IFD_universal
1592******************************************************************************/
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605/******************************************************************************
1606*
1607* Internal Function:     get_Tag_Text_Value
1608*
1609* Description:  Attempts to interpret an IFD entry into a text string using the
1610*               information in the IFD_Tag_Definitions global array.
1611*
1612* Parameters:   Tag - The IFD entry to process
1613*               Tag_Definitions_Name - The name of the tag definitions to use from within the IFD_Tag_Definitions global array
1614*
1615* Returns:      String - if the tag was successfully decoded into a text string
1616*               FALSE - if the tag could not be decoded using the information
1617*                       in the IFD_Tag_Definitions global array
1618*
1619******************************************************************************/
1620
1621function get_Tag_Text_Value( $Tag, $Tag_Definitions_Name )
1622{
1623        // Check what format the entry is specified as
1624
1625        if ( $Tag['Type'] == "String" )
1626        {
1627                // Format is Text String
1628
1629                // If "Unknown" (type 7) data type,
1630                if ( $Tag['Data Type'] == 7 )
1631                {
1632                        // Return data as is.
1633                        return $Tag['Data'];
1634                }
1635                else
1636                {
1637                        // Otherwise return the default string value of the datatype
1638                        return get_IFD_value_as_text( $Tag );
1639                }
1640        }
1641        else if ( $Tag['Type'] == "Character Coded String" )
1642        {
1643                // Format is Character Coded String (First 8 characters indicate coding scheme)
1644
1645                // Convert Data to a string
1646                if ( $Tag['Data Type'] == 7 )
1647                {
1648                        // If it is type "Unknown" (type 7) use data as is
1649                        $data =  $Tag['Data'];
1650                }
1651                else
1652                {
1653                        // Otherwise use the default string value of the datatype
1654                        $data = get_IFD_value_as_text( $Tag );
1655                }
1656
1657                // Some implementations allow completely data with no Coding Scheme Name,
1658                // so we need to handle this to avoid errors
1659                if ( trim( $data ) == "" )
1660                {
1661                        return "";
1662                }
1663
1664                // Extract the Coding Scheme Name from the first 8 characters
1665                $char_code = substr( $data, 0, 8 );
1666
1667                // Extract the Data part from after the first 8 characters
1668                $characters = substr( $data, 8 );
1669
1670                // Check coding scheme and interpret as neccessary
1671
1672                if ( $char_code === "ASCII\x00\x00\x00" )
1673                {
1674                        // ASCII coding - return data as is.
1675                        return $characters;
1676                }
1677                elseif ( ( $char_code === "UNICODE\x00" ) ||
1678                         ( $char_code === "Unicode\x00" ) )             // Note lowercase is non standard
1679                {
1680                        // Unicode coding - interpret and return result.
1681                        return xml_UTF16_clean( $characters, TRUE );
1682                }
1683                else
1684                {
1685                        // Unknown coding - return string indicating this
1686                        return "Unsupported character coding : \"$char_code\"\n\"" . trim($characters) . "\"";
1687                }
1688                break;
1689        }
1690        else if ( $Tag['Type'] == "Numeric" )
1691        {
1692                // Format is numeric - return default text value with any required units text appended
1693                if ( array_key_exists ( 'Units', $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag["Tag Number"] ] ) )
1694                {
1695                        $units = $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag["Tag Number"] ]['Units'];
1696                }
1697                else
1698                {
1699                        $units = "";
1700                }
1701                return get_IFD_value_as_text( $Tag )  . " " . $units;
1702        }
1703        else if  ( $Tag['Type'] == "Lookup" )
1704        {
1705                // Format is a Lookup Table
1706
1707                // Get a numeric value to use in lookup
1708
1709                if ( is_array( $Tag['Data'] ) )
1710                {
1711                        // If data is an array, use first element
1712                        $first_val = $Tag['Data'][0];
1713                }
1714                else if ( is_string( $Tag['Data'] ) )
1715                {
1716                        // If data is a string, use the first character
1717                        $first_val = ord($Tag['Data']{0});
1718                }
1719                else
1720                {
1721                        // Otherwise use the data as is
1722                        $first_val = $Tag['Data'];
1723                }
1724
1725                // Check if the data value exists in the lookup table for this IFD entry
1726                if ( array_key_exists( $first_val, $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag["Tag Number"] ] ) )
1727                {
1728                        // Data value exists in lookup table - return the matching string
1729                        return $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag["Tag Number"] ][ $first_val ];
1730                }
1731                else
1732                {
1733                        // Data value doesnt exist in lookup table - return explanation string
1734                        return "Unknown Reserved value $first_val ";
1735                }
1736        }
1737        else if  ( $Tag['Type'] == "Special" )
1738        {
1739                // Format is special - interpret to text with special handlers
1740                return get_Special_Tag_Text_Value( $Tag, $Tag_Definitions_Name );
1741        }
1742        else if  ( $Tag['Type'] == "PIM" )
1743        {
1744                // Format is Print Image Matching info - interpret with custom handler
1745                return get_PIM_Text_Value( $Tag, $Tag_Definitions_Name );
1746        }
1747        else if  ( $Tag['Type'] == "SubIFD" )
1748        {
1749                // Format is a Sub-IFD - this has no text value
1750                return "";
1751        }
1752        else
1753        {
1754                // Unknown Format - Couldn't interpret using the IFD_Tag_Definitions global array information
1755                return FALSE;
1756        }
1757}
1758
1759/******************************************************************************
1760* End of Function:     get_Tag_Text_Value
1761******************************************************************************/
1762
1763
1764
1765
1766
1767
1768/******************************************************************************
1769*
1770* Internal Function:     get_Special_Tag_Text_Value
1771*
1772* Description:  Interprets an IFD entry marked as "Special" in the IFD_Tag_Definitions
1773*               global array into a text string using custom handlers
1774*
1775* Parameters:   Tag - The IFD entry to process
1776*               Tag_Definitions_Name - The name of the tag definitions to use from within the IFD_Tag_Definitions global array
1777*
1778* Returns:      String - if the tag was successfully decoded into a text string
1779*               FALSE - if the tag could not be decoded
1780*
1781******************************************************************************/
1782
1783function get_Special_Tag_Text_Value( $Tag, $Tag_Definitions_Name )
1784{
1785        // Check what type of IFD is being decoded
1786
1787        if ( $Tag_Definitions_Name == "TIFF" )
1788        {
1789                // This is a TIFF IFD (bottom level)
1790
1791                // Check what tag number the IFD entry has.
1792                switch ( $Tag['Tag Number'] )
1793                {
1794                        case 530:  // YCbCr Sub Sampling Entry
1795
1796                                // Data contains two numerical values
1797
1798                                if ( ( $Tag['Data'][0] == 2 ) && ( $Tag['Data'][1] == 1 ) )
1799                                {
1800                                        // Values are 2,1 - hence YCbCr 4:2:2
1801                                        return "YCbCr 4:2:2 ratio of chrominance components to the luminance components";
1802                                }
1803                                elseif ( ( $Tag['Data'][0] == 2 ) && ( $Tag['Data'][1] == 2 ) )
1804                                {
1805                                        // Values are 2,2 - hence YCbCr 4:2:0
1806                                        return "YCbCr 4:2:0 ratio of chrominance components to the luminance components";
1807                                }
1808                                else
1809                                {
1810                                        // Other values are unknown
1811                                        return "Unknown Reserved value (" . $Tag['Data'][0] . ")";
1812                                }
1813                                break;
1814
1815                        default:
1816                                return FALSE;
1817                }
1818        }
1819        else if ( $Tag_Definitions_Name == "EXIF" )
1820        {
1821                // This is an EXIF IFD
1822
1823                // Check what tag number the IFD entry has.
1824                switch ( $Tag['Tag Number'] )
1825                {
1826
1827                        case 37121: // Components configuration
1828
1829                                // Data contains 4 numerical values indicating component type
1830
1831                                $output_str = "";
1832
1833                                // Cycle through each component
1834                                for ( $Num = 0; $Num < 4; $Num++ )
1835                                {
1836                                        // Construct first part of text string
1837                                        $output_str .= "Component " . ( $Num + 1 ) . ": ";
1838
1839                                        // Construct second part of text string via
1840                                        // lookup using numerical value
1841
1842                                        $value = ord( $Tag['Data']{$Num} );
1843                                        switch( $value )
1844                                        {
1845                                                case 0:
1846                                                        $output_str .= "Does not exist\n";
1847                                                        break;
1848                                                case 1:
1849                                                        $output_str .= "Y (Luminance)\n";
1850                                                        break;
1851                                                case 2:
1852                                                        $output_str .= "Cb (Chroma minus Blue)\n";
1853                                                        break;
1854                                                case 3:
1855                                                        $output_str .= "Cr (Chroma minus Red)\n";
1856                                                        break;
1857                                                case 4:
1858                                                        $output_str .= "Red\n";
1859                                                        break;
1860                                                case 5:
1861                                                        $output_str .= "Green\n";
1862                                                        break;
1863                                                case 6:
1864                                                        $output_str .= "Blue\n";
1865                                                        break;
1866                                                default:
1867                                                        $output_str .= "Unknown value $value\n";
1868                                        };
1869                                }
1870
1871                                // Return the completed string
1872
1873                                return $output_str;
1874                                break;
1875
1876
1877
1878                        case 41730: // Colour Filter Array Pattern
1879
1880                                // The first two characters are a SHORT for Horizontal repeat pixel unit -
1881                                $n_max = get_IFD_Data_Type( substr( $Tag['Data'], 0, 2 ), 3, $Tag['Byte Align'] );
1882
1883                                // The next two characters are a SHORT for Vertical repeat pixel unit -
1884                                $m_max = get_IFD_Data_Type( substr( $Tag['Data'], 2, 2 ), 3, $Tag['Byte Align'] );
1885
1886
1887                                // At least one camera type appears to have byte reversed values for N_Max and M_Max
1888                                // Check if they need reversing
1889                                if ( $n_max > 256 )
1890                                {
1891                                        $n_max = $n_max/256 + 256*($n_max%256);
1892                                }
1893
1894                                if ( $m_max > 256 )
1895                                {
1896                                        $m_max = $m_max/256 + 256*($m_max%256);
1897                                }
1898
1899
1900                                $output_str = "";
1901
1902
1903                                // Cycle through all the elements in the resulting 2 dimensional array,
1904                                for( $m = 1; $m <= $m_max; $m++ )
1905                                {
1906                                        for( $n = 1; $n <= $n_max; $n++ )
1907                                        {
1908
1909                                                // Append text from a lookup table according to
1910                                                // the value read for this element
1911
1912                                                switch ( ord($Tag['Data']{($n_max*($m-1)+$n+3)}) )
1913                                                {
1914                                                        case 0:
1915                                                                $output_str .= "RED     ";
1916                                                                break;
1917                                                        case 1:
1918                                                                $output_str .= "GREEN   ";
1919                                                                break;
1920                                                        case 2:
1921                                                                $output_str .= "BLUE    ";
1922                                                                break;
1923                                                        case 3:
1924                                                                $output_str .= "CYAN    ";
1925                                                                break;
1926                                                        case 4:
1927                                                                $output_str .= "MAGENTA ";
1928                                                                break;
1929                                                        case 5:
1930                                                                $output_str .= "YELLOW  ";
1931                                                                break;
1932                                                        case 6:
1933                                                                $output_str .= "WHITE   ";
1934                                                                break;
1935                                                        default:
1936                                                                $output_str .= "Unknown ";
1937                                                                break;
1938                                                };
1939                                        };
1940                                        $output_str .= "\n";
1941                                };
1942
1943                                // Return the resulting string
1944                                return $output_str;
1945                                break;
1946
1947                        default:
1948                                return FALSE;
1949                }
1950        }
1951        else
1952        {
1953                // Unknown IFD type, see if it is part of a makernote
1954                return get_Makernote_Text_Value( $Tag, $Tag_Definitions_Name );
1955        }
1956
1957
1958}
1959
1960/******************************************************************************
1961* End of Function:     get_Tag_Text_Value
1962******************************************************************************/
1963
1964
1965
1966
1967
1968
1969
1970
1971/******************************************************************************
1972*
1973* Function:     interpret_IFD
1974*
1975* Description:  Generates html detailing the contents a single IFD.
1976*
1977* Parameters:   IFD_array - the array containing an IFD
1978*               filename - the name of the Image file being processed ( used
1979*                          by scripts which displays EXIF thumbnails)
1980*
1981* Returns:      output_str - A string containing the HTML
1982*
1983******************************************************************************/
1984
1985function interpret_IFD( $IFD_array, $filename )
1986{
1987        // Create the output string with the table tag
1988        $output_str = "<table class=\"EXIF_Table\" border=1>\n";
1989
1990        // Create an extra output string to receive any supplementary html
1991        // which cannot go inside the table
1992        $extra_IFD_str = "";
1993
1994        // Check that the IFD array is valid
1995        if ( ( $IFD_array === FALSE ) || ( $IFD_array === NULL ) )
1996        {
1997                // the IFD array is NOT valid - exit
1998                return "";
1999        }
2000
2001        // Check if this is an EXIF IFD and if there is a makernote present
2002        if ( ( $IFD_array['Tags Name'] === "EXIF" ) &&
2003             ( ! array_key_exists( 37500, $IFD_array ) ) )
2004        {
2005
2006                // This is an EXIF IFD but NO makernote is present - Add a message to the output
2007              //  $extra_IFD_str .= "No Makernote Present";
2008        }
2009
2010        // Cycle through each tag in the IFD
2011
2012        foreach( $IFD_array as $Tag_ID => $Exif_Tag )
2013        {
2014            $Exif_Tag['Type_src']="exif";
2015                // Ignore the non numeric elements - they aren't tags
2016                if ( ! is_numeric ( $Tag_ID ) )
2017                {
2018                        // Skip Tags Name
2019                }
2020                        // Check if the Tag has been decoded successfully
2021                else if ( $Exif_Tag['Decoded'] == TRUE )
2022                {
2023                        // This tag has been successfully decoded
2024
2025                        // Table cells won't get drawn with nothing in them -
2026                        // Ensure that at least a non breaking space exists in them
2027
2028                        if ( trim($Exif_Tag['Text Value']) == "" )
2029                        {
2030                                $Exif_Tag['Text Value'] = "&nbsp;";
2031                        }
2032
2033                        // Check if the tag is a sub-IFD
2034                        if ( $Exif_Tag['Type'] == "SubIFD" )
2035                        {
2036                                // This is a sub-IFD tag
2037                                // Add a sub-heading for the sub-IFD
2038                                $recordname=str_replace(" ","_",$Exif_Tag['Tag Name']);
2039                                $recordname=str_replace("_(IFD)","", $recordname);
2040$entete = "<fieldset  id='" . $recordname . "' class='fieldset' ><legend>";
2041$extra_IFD_str .= $entete . $Exif_Tag['Tag Name'] . " contents</legend>";
2042                                // Cycle through each sub-IFD in the chain
2043                                foreach ( $Exif_Tag['Data'] as $subIFD )
2044                                {
2045                                        // Interpret this sub-IFD and add the html to the secondary output
2046                                        $extra_IFD_str .= interpret_IFD( $subIFD, $filename );
2047     
2048        }
2049                             
2050                        }
2051                                // Check if the tag is a makernote
2052                        else if ( $Exif_Tag['Type'] == "Maker Note" )
2053                        {
2054                                // This is a Makernote Tag
2055                                // Add a sub-heading for the Makernote
2056                                $extra_IFD_str .= "<h3 class=\"EXIF_Secondary_Heading\">Maker Note Contents</h3>";
2057
2058                                // Interpret the Makernote and add the html to the secondary output
2059                                $extra_IFD_str .= Interpret_Makernote_to_HTML( $Exif_Tag, $filename );
2060                        }
2061                                // Check if this is a IPTC/NAA Record within the EXIF IFD
2062                        else if ( $Exif_Tag['Type'] == "IPTC" )
2063                        {
2064                                // This is a IPTC/NAA Record, interpret it and output to the secondary html
2065                                $extra_IFD_str .= "<h3 class=\"EXIF_Secondary_Heading\">Contains IPTC/NAA Embedded in EXIF</h3>";
2066                                $extra_IFD_str .=Interpret_IPTC_to_HTML( $Exif_Tag['Data'] );
2067                        }
2068                                // Change: Check for embedded XMP as of version 1.11
2069                                // Check if this is a XMP Record within the EXIF IFD
2070                        else if ( $Exif_Tag['Type'] == "XMP" )
2071                        {
2072                                // This is a XMP Record, interpret it and output to the secondary html
2073                                $extra_IFD_str .= "<h3 class=\"EXIF_Secondary_Heading\">Contains XMP Embedded in EXIF</h3>";
2074                                $extra_IFD_str .= Interpret_XMP_to_HTML( $Exif_Tag['Data'] );
2075                        }
2076                                // Change: Check for embedded IRB as of version 1.11
2077                                // Check if this is a Photoshop IRB Record within the EXIF IFD
2078                        else if ( $Exif_Tag['Type'] == "IRB" )
2079                        {
2080                                // This is a Photoshop IRB Record, interpret it and output to the secondary html
2081                                $extra_IFD_str .= "<h3 class=\"EXIF_Secondary_Heading\">Contains Photoshop IRB Embedded in EXIF</h3>";
2082                                $extra_IFD_str .= Interpret_IRB_to_HTML( $Exif_Tag['Data'], $filename );
2083                        }
2084                                // Check if the tag is Numeric
2085                        else if ( $Exif_Tag['Type'] == "Numeric" )
2086                        {
2087                   
2088$output_str .= OutPut_exif($Exif_Tag);
2089                                // Numeric Tag - Output text value as is.
2090                        }
2091                        else
2092                        {
2093$output_str .= OutPut_exif($Exif_Tag);
2094                                // Other tag - Output text as preformatted
2095                         }
2096
2097                }
2098                else
2099                {
2100                        // Tag has NOT been decoded successfully
2101                        // Hence it is either an unknown tag, or one which
2102                        // requires processing at the time of html construction
2103
2104                        // Table cells won't get drawn with nothing in them -
2105                        // Ensure that at least a non breaking space exists in them
2106
2107                        if ( trim($Exif_Tag['Text Value']) == "" )
2108                        {
2109                                $Exif_Tag['Text Value'] = "&nbsp;";
2110                        }
2111
2112                        // Check if this tag is the first IFD Thumbnail
2113                        if ( ( $IFD_array['Tags Name'] == "TIFF" ) &&
2114                             ( $Tag_ID == 513 ) )
2115                        {
2116                                // This is the first IFD thumbnail - Add html to the output
2117
2118                                // Change: as of version 1.11 - Changed to make thumbnail link portable across directories
2119                                // Build the path of the thumbnail script and its filename parameter to put in a url
2120                                $link_str = get_relative_path( dirname(__FILE__) . "/get_exif_thumb.php" , getcwd ( ) );
2121                                $link_str .= "?filename=";
2122                                $link_str .= get_relative_path( $filename, dirname(__FILE__) );
2123
2124                                // Add thumbnail link to html
2125                                $output_str .= "<tr class=\"EXIF_Table_Row\"><td class=\"EXIF_Caption_Cell\">" . $Exif_Tag['Tag Name'] . "</td><td class=\"EXIF_Value_Cell\"><a class=\"EXIF_First_IFD_Thumb_Link\" href=\"$link_str\"><img class=\"EXIF_First_IFD_Thumb\" src=\"$link_str\"></a></td></tr>\n";
2126                        }
2127                                // Check if this is the Makernote
2128                        else if ( $Exif_Tag['Type'] == "Maker Note" )
2129                        {
2130                                // This is the makernote, but has not been decoded
2131                                // Add a message to the secondary output
2132                                $extra_IFD_str .= "<h3 class=\"EXIF_Secondary_Heading\">Makernote Coding Unknown</h3>\n";
2133                        }
2134                        else
2135                        {
2136                                // This is an Unknown Tag
2137
2138                                // Check if the user wants to hide unknown tags
2139                                if ( $GLOBALS['HIDE_UNKNOWN_TAGS'] === FALSE )
2140                                {
2141                                        // User wants to display unknown tags
2142                                    $output_str .= OutPut_exif($tabval);
2143
2144                                }
2145                        }
2146                }
2147        }
2148
2149        // Close the table in the output
2150        $output_str .= "</table>\n</fieldset>";
2151
2152        // Add the secondary output at the end of the main output
2153        $output_str .= "$extra_IFD_str";
2154
2155        // Return the resulting html
2156        return $output_str;
2157}
2158
2159/******************************************************************************
2160* End of Function:     interpret_IFD
2161******************************************************************************/
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177/******************************************************************************
2178*
2179* Function:     get_IFD_Data_Type
2180*
2181* Description:  Decodes an IFD field value from a binary data string, using
2182*               information supplied about the data type and byte alignment of
2183*               the stored data.
2184*               This function should be used for all datatypes except ASCII strings
2185*
2186* Parameters:   input_data - a binary data string containing the IFD value,
2187*                            must be exact length of the value
2188*               data_type - a number representing the IFD datatype as per the
2189*                           TIFF 6.0 specification:
2190*                               1 = Unsigned 8-bit Byte
2191*                               2 = ASCII String
2192*                               3 = Unsigned 16-bit Short
2193*                               4 = Unsigned 32-bit Long
2194*                               5 = Unsigned 2x32-bit Rational
2195*                               6 = Signed 8-bit Byte
2196*                               7 = Undefined
2197*                               8 = Signed 16-bit Short
2198*                               9 = Signed 32-bit Long
2199*                               10 = Signed 2x32-bit Rational
2200*                               11 = 32-bit Float
2201*                               12 = 64-bit Double
2202*               Byte_Align - Indicates the byte alignment of the data.
2203*                            MM = Motorola, MSB first, Big Endian
2204*                            II = Intel, LSB first, Little Endian
2205*
2206* Returns:      output - the value of the data (string or numeric)
2207*
2208******************************************************************************/
2209
2210function get_IFD_Data_Type( $input_data, $data_type, $Byte_Align ){
2211global $error_message ;
2212        // Check if this is a Unsigned Byte, Unsigned Short or Unsigned Long
2213        if (( $data_type == 1 ) || ( $data_type == 3 ) || ( $data_type == 4 ))
2214        {
2215                // This is a Unsigned Byte, Unsigned Short or Unsigned Long
2216
2217                // Check the byte alignment to see if the bytes need tp be reversed
2218                if ( $Byte_Align == "II" )
2219                {
2220                        // This is in Intel format, reverse it
2221                        $input_data = strrev ( $input_data );
2222                }
2223
2224                // Convert the binary string to a number and return it
2225                return hexdec( bin2hex( $input_data ) );
2226        }
2227                // Check if this is a ASCII string type
2228        elseif ( $data_type == 2 )
2229        {
2230                // Null terminated ASCII string(s)
2231                // The input data may represent multiple strings, as the
2232                // 'count' field represents the total bytes, not the number of strings
2233                // Hence this should not be processed here, as it would have
2234                // to return multiple values instead of a single value
2235
2236                $error_message .= "<p>Error - ASCII Strings should not be processed in get_IFD_Data_Type</p>\n";
2237                return "Error Should never get here"; //explode( "\x00", $input_data );
2238        }
2239                // Check if this is a Unsigned rational type
2240        elseif ( $data_type == 5 )
2241        {
2242                // This is a Unsigned rational type
2243
2244                // Check the byte alignment to see if the bytes need to be reversed
2245                if ( $Byte_Align == "MM" )
2246                {
2247                        // Motorola MSB first byte aligment
2248                        // Unpack the Numerator and denominator and return them
2249                        return unpack( 'NNumerator/NDenominator', $input_data );
2250                }
2251                else
2252                {
2253                        // Intel LSB first byte aligment
2254                        // Unpack the Numerator and denominator and return them
2255                        return unpack( 'VNumerator/VDenominator', $input_data );
2256                }
2257        }
2258                // Check if this is a Signed Byte, Signed Short or Signed Long
2259        elseif ( ( $data_type == 6 ) || ( $data_type == 8 ) || ( $data_type == 9 ) )
2260        {
2261                // This is a Signed Byte, Signed Short or Signed Long
2262
2263                // Check the byte alignment to see if the bytes need to be reversed
2264                if ( $Byte_Align == "II" )
2265                {
2266                        //Intel format, reverse the bytes
2267                        $input_data = strrev ( $input_data );
2268                }
2269
2270                // Convert the binary string to an Unsigned number
2271                $value = hexdec( bin2hex( $input_data ) );
2272
2273                // Convert to signed number
2274
2275                // Check if it is a Byte above 128 (i.e. a negative number)
2276                if ( ( $data_type == 6 ) && ( $value > 128 ) )
2277                {
2278                        // number should be negative - make it negative
2279                        return  $value - 256;
2280                }
2281
2282                // Check if it is a Short above 32767 (i.e. a negative number)
2283                if ( ( $data_type == 8 ) && ( $value > 32767 ) )
2284                {
2285                        // number should be negative - make it negative
2286                        return  $value - 65536;
2287                }
2288
2289                // Check if it is a Long above 2147483648 (i.e. a negative number)
2290                if ( ( $data_type == 9 ) && ( $value > 2147483648 ) )
2291                {
2292                        // number should be negative - make it negative
2293                        return  $value - 4294967296;
2294                }
2295
2296                // Return the signed number
2297                return $value;
2298        }
2299                // Check if this is Undefined type
2300        elseif ( $data_type == 7 )
2301        {
2302                // Custom Data - Do nothing
2303                return $input_data;
2304        }
2305                // Check if this is a Signed Rational type
2306        elseif ( $data_type == 10 )
2307        {
2308                // This is a Signed Rational type
2309
2310                // Signed Long not available with endian in unpack , use unsigned and convert
2311
2312                // Check the byte alignment to see if the bytes need to be reversed
2313                if ( $Byte_Align == "MM" )
2314                {
2315                        // Motorola MSB first byte aligment
2316                        // Unpack the Numerator and denominator
2317                        $value = unpack( 'NNumerator/NDenominator', $input_data );
2318                }
2319                else
2320                {
2321                        // Intel LSB first byte aligment
2322                        // Unpack the Numerator and denominator
2323                        $value = unpack( 'VNumerator/VDenominator', $input_data );
2324                }
2325
2326                // Convert the numerator to a signed number
2327                // Check if it is above 2147483648 (i.e. a negative number)
2328                if ( $value['Numerator'] > 2147483648 )
2329                {
2330                        // number is negative
2331                        $value['Numerator'] -= 4294967296;
2332                }
2333
2334                // Convert the denominator to a signed number
2335                // Check if it is above 2147483648 (i.e. a negative number)
2336                if ( $value['Denominator'] > 2147483648 )
2337                {
2338                        // number is negative
2339                        $value['Denominator'] -= 4294967296;
2340                }
2341
2342                // Return the Signed Rational value
2343                return $value;
2344        }
2345                // Check if this is a Float type
2346        elseif ( $data_type == 11 )
2347        {
2348                // IEEE 754 Float
2349                // TODO - EXIF - IFD datatype Float not implemented yet
2350                return "FLOAT NOT IMPLEMENTED YET";
2351        }
2352                // Check if this is a Double type
2353        elseif ( $data_type == 12 )
2354        {
2355                // IEEE 754 Double
2356                // TODO - EXIF - IFD datatype Double not implemented yet
2357                return "DOUBLE NOT IMPLEMENTED YET";
2358        }
2359        else
2360        {
2361                // Error - Invalid Datatype
2362                return "Invalid Datatype $data_type";
2363
2364        }
2365
2366}
2367
2368/******************************************************************************
2369* End of Function:     get_IFD_Data_Type
2370******************************************************************************/
2371
2372
2373
2374
2375
2376
2377/******************************************************************************
2378*
2379* Function:     put_IFD_Data_Type
2380*
2381* Description:  Encodes an IFD field from a value to a binary data string, using
2382*               information supplied about the data type and byte alignment of
2383*               the stored data.
2384*
2385* Parameters:   input_data - an IFD data value, numeric or string
2386*               data_type - a number representing the IFD datatype as per the
2387*                           TIFF 6.0 specification:
2388*                               1 = Unsigned 8-bit Byte
2389*                               2 = ASCII String
2390*                               3 = Unsigned 16-bit Short
2391*                               4 = Unsigned 32-bit Long
2392*                               5 = Unsigned 2x32-bit Rational
2393*                               6 = Signed 8-bit Byte
2394*                               7 = Undefined
2395*                               8 = Signed 16-bit Short
2396*                               9 = Signed 32-bit Long
2397*                               10 = Signed 2x32-bit Rational
2398*                               11 = 32-bit Float
2399*                               12 = 64-bit Double
2400*               Byte_Align - Indicates the byte alignment of the data.
2401*                            MM = Motorola, MSB first, Big Endian
2402*                            II = Intel, LSB first, Little Endian
2403*
2404* Returns:      output - the packed binary string of the data
2405*
2406******************************************************************************/
2407
2408function put_IFD_Data_Type( $input_data, $data_type, $Byte_Align )
2409{
2410        // Process according to the datatype
2411        switch ( $data_type )
2412        {
2413                case 1: // Unsigned Byte - return character as is
2414                        return chr($input_data);
2415                        break;
2416
2417                case 2: // ASCII String
2418                        // Return the string with terminating null
2419                        return $input_data . "\x00";
2420                        break;
2421
2422                case 3: // Unsigned Short
2423                        // Check byte alignment
2424                        if ( $Byte_Align == "II" )
2425                        {
2426                                // Intel/Little Endian - pack the short and return
2427                                return pack( "v", $input_data );
2428                        }
2429                        else
2430                        {
2431                                // Motorola/Big Endian - pack the short and return
2432                                return pack( "n", $input_data );
2433                        }
2434                        break;
2435
2436                case 4: // Unsigned Long
2437                        // Check byte alignment
2438                        if ( $Byte_Align == "II" )
2439                        {
2440                                // Intel/Little Endian - pack the long and return
2441                                return pack( "V", $input_data );
2442                        }
2443                        else
2444                        {
2445                                // Motorola/Big Endian - pack the long and return
2446                                return pack( "N", $input_data );
2447                        }
2448                        break;
2449
2450                case 5: // Unsigned Rational
2451                        // Check byte alignment
2452                        if ( $Byte_Align == "II" )
2453                        {
2454                                // Intel/Little Endian - pack the two longs and return
2455                                return pack( "VV", $input_data['Numerator'], $input_data['Denominator'] );
2456                        }
2457                        else
2458                        {
2459                                // Motorola/Big Endian - pack the two longs and return
2460                                return pack( "NN", $input_data['Numerator'], $input_data['Denominator'] );
2461                        }
2462                        break;
2463
2464                case 6: // Signed Byte
2465                        // Check if number is negative
2466                        if ( $input_data < 0 )
2467                        {
2468                                // Number is negative - return signed character
2469                                return chr( $input_data + 256 );
2470                        }
2471                        else
2472                        {
2473                                // Number is positive - return character
2474                                return chr( $input_data );
2475                        }
2476                        break;
2477
2478                case 7: // Unknown - return as is
2479                        return $input_data;
2480                        break;
2481
2482                case 8: // Signed Short
2483                        // Check if number is negative
2484                        if (  $input_data < 0 )
2485                        {
2486                                // Number is negative - make signed value
2487                                $input_data = $input_data + 65536;
2488                        }
2489                        // Check byte alignment
2490                        if ( $Byte_Align == "II" )
2491                        {
2492                                // Intel/Little Endian - pack the short and return
2493                                return pack( "v", $input_data );
2494                        }
2495                        else
2496                        {
2497                                // Motorola/Big Endian - pack the short and return
2498                                return pack( "n", $input_data );
2499                        }
2500                        break;
2501
2502                case 9: // Signed Long
2503                        // Check if number is negative
2504                        if (  $input_data < 0 )
2505                        {
2506                                // Number is negative - make signed value
2507                                $input_data = $input_data + 4294967296;
2508                        }
2509                        // Check byte alignment
2510                        if ( $Byte_Align == "II" )
2511                        {
2512                                // Intel/Little Endian - pack the long and return
2513                                return pack( "v", $input_data );
2514                        }
2515                        else
2516                        {
2517                                // Motorola/Big Endian - pack the long and return
2518                                return pack( "n", $input_data );
2519                        }
2520                        break;
2521
2522                case 10: // Signed Rational
2523                        // Check if numerator is negative
2524                        if (  $input_data['Numerator'] < 0 )
2525                        {
2526                                // Number is numerator - make signed value
2527                                $input_data['Numerator'] = $input_data['Numerator'] + 4294967296;
2528                        }
2529                        // Check if denominator is negative
2530                        if (  $input_data['Denominator'] < 0 )
2531                        {
2532                                // Number is denominator - make signed value
2533                                $input_data['Denominator'] = $input_data['Denominator'] + 4294967296;
2534                        }
2535                        // Check byte alignment
2536                        if ( $Byte_Align == "II" )
2537                        {
2538                                // Intel/Little Endian - pack the two longs and return
2539                                return pack( "VV", $input_data['Numerator'], $input_data['Denominator'] );
2540                        }
2541                        else
2542                        {
2543                                // Motorola/Big Endian - pack the two longs and return
2544                                return pack( "NN", $input_data['Numerator'], $input_data['Denominator'] );
2545                        }
2546                        break;
2547
2548                case 11: // Float
2549                        // IEEE 754 Float
2550                        // TODO - EXIF - IFD datatype Float not implemented yet
2551                        return "FLOAT NOT IMPLEMENTED YET";
2552                        break;
2553
2554                case 12: // Double
2555                        // IEEE 754 Double
2556                        // TODO - EXIF - IFD datatype Double not implemented yet
2557                        return "DOUBLE NOT IMPLEMENTED YET";
2558                        break;
2559
2560                default:
2561                        // Error - Invalid Datatype
2562                        return "Invalid Datatype $data_type";
2563                        break;
2564
2565        }
2566
2567        // Shouldn't get here
2568        return FALSE;
2569}
2570
2571/******************************************************************************
2572* End of Function:     put_IFD_Data_Type
2573******************************************************************************/
2574
2575
2576
2577
2578
2579/******************************************************************************
2580*
2581* Function:     get_IFD_value_as_text
2582*
2583* Description:  Decodes an IFD field value from a binary data string, using
2584*               information supplied about the data type and byte alignment of
2585*               the stored data.
2586*               This function should be used for all datatypes except ASCII strings
2587*
2588* Parameters:   input_data - a binary data string containing the IFD value,
2589*                            must be exact length of the value
2590*               data_type - a number representing the IFD datatype as per the
2591*                           TIFF 6.0 specification:
2592*                               1 = Unsigned 8-bit Byte
2593*                               2 = ASCII String
2594*                               3 = Unsigned 16-bit Short
2595*                               4 = Unsigned 32-bit Long
2596*                               5 = Unsigned 2x32-bit Rational
2597*                               6 = Signed 8-bit Byte
2598*                               7 = Undefined
2599*                               8 = Signed 16-bit Short
2600*                               9 = Signed 32-bit Long
2601*                               10 = Signed 2x32-bit Rational
2602*                               11 = 32-bit Float
2603*                               12 = 64-bit Double
2604*               Byte_Align - Indicates the byte alignment of the data.
2605*                            MM = Motorola, MSB first, Big Endian
2606*                            II = Intel, LSB first, Little Endian
2607*
2608* Returns:      output - the value of the data (string or numeric)
2609*
2610******************************************************************************/
2611
2612function get_IFD_value_as_text( $Exif_Tag )
2613{
2614        // Create a string to receive the output text
2615        $output_str = "";
2616
2617        // Select Processing method according to the datatype
2618        switch  ($Exif_Tag['Data Type'])
2619        {
2620                case 1 : // Unsigned Byte
2621                case 3 : // Unsigned Short
2622                case 4 : // Unsigned Long
2623                case 6 : // Signed Byte
2624                case 8 : // Signed Short
2625                case 9 : // Signed Long
2626
2627                        // Cycle through each of the values for this tag
2628                        foreach ( $Exif_Tag['Data'] as $val )
2629                        {
2630                                // Check that this isn't the first value,
2631                                if ( $output_str != "" )
2632                                {
2633                                        // This isn't the first value, Add a Comma and Newline to the output
2634                                        $output_str .= ",\n";
2635                                }
2636                                // Add the Value to the output
2637                                $output_str .= $val;
2638                        }
2639                        break;
2640
2641                case 2 : // ASCII
2642                        // Append all the strings together, separated by Newlines
2643                        $output_str .= implode ( "\n", $Exif_Tag['Data']);
2644                        break;
2645
2646                case 5 : // Unsigned Rational
2647                case 10: // Signed Rational
2648
2649                        // Cycle through each of the values for this tag
2650                        foreach ( $Exif_Tag['Data'] as $val )
2651                        {
2652                                // Check that this isn't the first value,
2653                                if ( $output_str != "" )
2654                                {
2655                                        // This isn't the first value, Add a Comma and Newline to the output
2656                                        $output_str .= ",\n";
2657                                }
2658
2659                                // Add the Full Value to the output
2660                                $output_str .= $val['Numerator'] ."/" . $val['Denominator'];
2661
2662                                // Check if division by zero might be a problem
2663                                if ( $val['Denominator'] != 0 )
2664                                {
2665                                        // Denominator is not zero, Add the Decimal Value to the output text
2666                                        $output_str .= " (" . ($val['Numerator'] / $val['Denominator']) . ")";
2667                                }
2668                        }
2669                        break;
2670
2671                case 11: // Float
2672                case 12: // Double
2673                        // TODO - EXIF - IFD datatype Double and Float not implemented yet
2674                        $output_str .= "Float and Double not implemented yet";
2675                        break;
2676
2677                case 7 : // Undefined
2678                        // Unless the User has asked to see the raw binary data, this
2679                        // type should not be displayed
2680
2681                        // Check if the user has requested to see the binary data in hex
2682                        if ( $GLOBALS['SHOW_BINARY_DATA_HEX'] == TRUE)
2683                        {
2684                                // User has requested to see the binary data in hex
2685                                // Add the value in hex
2686                                $output_str .= "( " . strlen( $Exif_Tag['Data'] ) . " bytes of binary data ): " . bin2hex( $Exif_Tag['Data'] )  ;
2687                        }
2688                                // Check if the user has requested to see the binary data as is
2689                        else if ( $GLOBALS['SHOW_BINARY_DATA_TEXT'] == TRUE)
2690                        {
2691                                // User has requested to see the binary data as is
2692                                // Add the value as is
2693                                $output_str .= "( " . strlen( $Exif_Tag['Data'] ) . " bytes of binary data ): " . $Exif_Tag['Data']  ;
2694                        }
2695                        else
2696                        {
2697                                // User has NOT requested to see binary data,
2698                                // Add a message indicating the number of bytes to the output
2699                                $output_str .= "( " . strlen( $Exif_Tag['Data'] ) . " bytes of binary data ) "  ;
2700                        }
2701                        break;
2702
2703                default :
2704                        // Error - Unknown IFD datatype
2705                        $output_str .= "Error - Exif tag data type (" . $Exif_Tag['Data Type'] .") is invalid";
2706                        break;
2707        }
2708
2709        // Return the resulting text string
2710        return $output_str;
2711}
2712
2713/******************************************************************************
2714* End of Function:     get_IFD_value_as_text
2715******************************************************************************/
2716
2717
2718
2719
2720/******************************************************************************
2721* Global Variable:      IFD_Data_Sizes
2722*
2723* Contents:     The sizes (in bytes) of each EXIF IFD Datatype, indexed by
2724*               their datatype number
2725*
2726******************************************************************************/
2727
2728$GLOBALS['IFD_Data_Sizes'] = array(     1 => 1,         // Unsigned Byte
2729                                        2 => 1,         // ASCII String
2730                                        3 => 2,         // Unsigned Short
2731                                        4 => 4,         // Unsigned Long
2732                                        5 => 8,         // Unsigned Rational
2733                                        6 => 1,         // Signed Byte
2734                                        7 => 1,         // Undefined
2735                                        8 => 2,         // Signed Short
2736                                        9 => 4,         // Signed Long
2737                                        10 => 8,        // Signed Rational
2738                                        11 => 4,        // Float
2739                                        12 => 8 );      // Double
2740
2741/******************************************************************************
2742* End of Global Variable:     IFD_Data_Sizes
2743******************************************************************************/
2744
2745
2746function OutPut_exif($tabval) { 
2747
2748 $retour = "<tr align='left' ><td>".$tabval['Type_src'] ."</td><td style='border:solid red 2px' >" .$tabval['Tag Name']. "</td><td><input size=90 name='" .$tabval['Tag Name']."' id='" .$tabval['Tag Name']. "' type='text' style='border:solid green 2px' value=' ". trim( $tabval['Text Value'] ) . "'/></td></tr>";
2749 
2750 return $retour;
2751  } 
2752?>
Note: See TracBrowser for help on using the repository browser.