[Linpha-cvs] SF.net SVN: linpha: [4743] trunk/linpha2/lib
Status: Inactive
Brought to you by:
bzrudi
From: <bz...@us...> - 2007-07-11 14:36:23
|
Revision: 4743 http://svn.sourceforge.net/linpha/?rev=4743&view=rev Author: bzrudi Date: 2007-07-11 07:36:12 -0700 (Wed, 11 Jul 2007) Log Message: ----------- moved pjmt Added Paths: ----------- trunk/linpha2/lib/pjmt/ trunk/linpha2/lib/pjmt/COPYING.txt trunk/linpha2/lib/pjmt/EXIF.php trunk/linpha2/lib/pjmt/EXIF_Makernote.php trunk/linpha2/lib/pjmt/EXIF_Tags.php trunk/linpha2/lib/pjmt/IPTC.php trunk/linpha2/lib/pjmt/JFIF.php trunk/linpha2/lib/pjmt/JPEG.php trunk/linpha2/lib/pjmt/Makernotes/ trunk/linpha2/lib/pjmt/Makernotes/Pentax.php trunk/linpha2/lib/pjmt/Makernotes/agfa.php trunk/linpha2/lib/pjmt/Makernotes/canon.php trunk/linpha2/lib/pjmt/Makernotes/casio.php trunk/linpha2/lib/pjmt/Makernotes/epson.php trunk/linpha2/lib/pjmt/Makernotes/fujifilm.php trunk/linpha2/lib/pjmt/Makernotes/konica_minolta.php trunk/linpha2/lib/pjmt/Makernotes/kyocera.php trunk/linpha2/lib/pjmt/Makernotes/nikon.php trunk/linpha2/lib/pjmt/Makernotes/olympus.php trunk/linpha2/lib/pjmt/Makernotes/panasonic.php trunk/linpha2/lib/pjmt/Makernotes/ricoh.php trunk/linpha2/lib/pjmt/Makernotes/sony.php trunk/linpha2/lib/pjmt/PIM.php trunk/linpha2/lib/pjmt/Photoshop_File_Info.php trunk/linpha2/lib/pjmt/Photoshop_IRB.php trunk/linpha2/lib/pjmt/PictureInfo.php trunk/linpha2/lib/pjmt/Toolkit_Version.php trunk/linpha2/lib/pjmt/Unicode.php trunk/linpha2/lib/pjmt/XML.php trunk/linpha2/lib/pjmt/XMP.php trunk/linpha2/lib/pjmt/pjmt_utils.php Added: trunk/linpha2/lib/pjmt/COPYING.txt =================================================================== --- trunk/linpha2/lib/pjmt/COPYING.txt (rev 0) +++ trunk/linpha2/lib/pjmt/COPYING.txt 2007-07-11 14:36:12 UTC (rev 4743) @@ -0,0 +1,340 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + <signature of Ty Coon>, 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. Added: trunk/linpha2/lib/pjmt/EXIF.php =================================================================== --- trunk/linpha2/lib/pjmt/EXIF.php (rev 0) +++ trunk/linpha2/lib/pjmt/EXIF.php 2007-07-11 14:36:12 UTC (rev 4743) @@ -0,0 +1,2757 @@ +<?php + +/****************************************************************************** +* +* Filename: EXIF.php +* +* Description: Provides functions for reading and writing EXIF Information +* to/from an APP1 segment of a JPEG file +* Unfortunately, because EXIF data may be distributed anywhere +* throughout an image file, rather than just being in one block, +* it is impossible to pass just a string containing only the EXIF +* information. Hence it is neccessary to be able to seek to +* any point in the file. This causes the HTTP and FTP wrappers +* not to work - i.e. the EXIF functions will only work with local +* files. +* To work on an internet file, copy it locally to start with: +* +* $newfilename = tempnam ( $dir, "tmpexif" ); +* copy ( "http://whatever.com", $newfilename ); +* +* +* Author: Evan Hunter +* +* Date: 30/7/2004 +* +* Project: PHP JPEG Metadata Toolkit +* +* Revision: 1.11 +* +* Changes: 1.00 -> 1.10 : added function get_EXIF_TIFF to allow extracting EXIF from a TIFF file +* 1.10 -> 1.11 : added functionality to allow decoding of XMP and Photoshop IRB information +* embedded within the EXIF data +* added checks for http and ftp wrappers, as these are not supported +* changed interpret_IFD to allow thumbnail links to work when +* toolkit is portable across directories +* +* +* URL: http://electronics.ozhiker.com +* +* Copyright: Copyright Evan Hunter 2004 +* +* License: This file is part of the PHP JPEG Metadata Toolkit. +* +* The PHP JPEG Metadata Toolkit is free software; you can +* redistribute it and/or modify it under the terms of the +* GNU General Public License as published by the Free Software +* Foundation; either version 2 of the License, or (at your +* option) any later version. +* +* The PHP JPEG Metadata Toolkit is distributed in the hope +* that it will be useful, but WITHOUT ANY WARRANTY; without +* even the implied warranty of MERCHANTABILITY or FITNESS +* FOR A PARTICULAR PURPOSE. See the GNU General Public License +* for more details. +* +* You should have received a copy of the GNU General Public +* License along with the PHP JPEG Metadata Toolkit; if not, +* write to the Free Software Foundation, Inc., 59 Temple +* Place, Suite 330, Boston, MA 02111-1307 USA +* +* If you require a different license for commercial or other +* purposes, please contact the author: ev...@oz... +* +******************************************************************************/ + + +// TODO : Thoroughly test the functions for writing EXIF segments +// TODO : Figure out a way to allow EXIF to function normally with HTTP and FTP wrappers +// TODO : Implement EXIF decoding of Device Setting Description field +// TODO : Implement EXIF decoding of SpatialFrequencyResponse field +// TODO : Implement EXIF decoding of OECF field +// TODO : Implement EXIF decoding of SubjectArea field +// TODO : Add a put_EXIF_TIFF function + +/****************************************************************************** +* +* Initialisation +* +******************************************************************************/ + + +if ( !isset( $GLOBALS['HIDE_UNKNOWN_TAGS'] ) ) $GLOBALS['HIDE_UNKNOWN_TAGS']= FALSE; +if ( !isset( $GLOBALS['SHOW_BINARY_DATA_HEX'] ) ) $GLOBALS['SHOW_BINARY_DATA_HEX'] = FALSE; +if ( !isset( $GLOBALS['SHOW_BINARY_DATA_TEXT'] ) ) $GLOBALS['SHOW_BINARY_DATA_TEXT'] = FALSE; + + +include_once 'EXIF_Tags.php'; +include_once 'EXIF_Makernote.php'; +include_once 'PIM.php'; +include_once 'Unicode.php'; +include_once 'JPEG.php'; +include_once 'IPTC.php'; +include_once 'Photoshop_IRB.php'; // Change: as of version 1.11 - Required for TIFF with embedded IRB +include_once 'XMP.php'; // Change: as of version 1.11 - Required for TIFF with embedded XMP +include_once 'pjmt_utils.php'; // Change: as of version 1.11 - Required for directory portability + + + + + + + + +/****************************************************************************** +* +* Function: get_EXIF_JPEG +* +* Description: Retrieves information from a Exchangeable Image File Format (EXIF) +* APP1 segment and returns it in an array. +* +* Parameters: filename - the filename of the JPEG image to process +* +* Returns: OutputArray - Array of EXIF records +* FALSE - If an error occured in decoding +* +******************************************************************************/ + +function get_EXIF_JPEG( $filename ) +{ + // Change: Added as of version 1.11 + // Check if a wrapper is being used - these are not currently supported (see notes at top of file) + if ( ( stristr ( $filename, "http://" ) != FALSE ) || ( stristr ( $filename, "ftp://" ) != FALSE ) ) + { + // A HTTP or FTP wrapper is being used - show a warning and abort + echo "HTTP and FTP wrappers are currently not supported with EXIF - See EXIF functionality documentation - a local file must be specified<br>"; + echo "To work on an internet file, copy it locally to start with:<br><br>\n"; + echo "\$newfilename = tempnam ( \$dir, \"tmpexif\" );<br>\n"; + echo "copy ( \"http://whatever.com\", \$newfilename );<br><br>\n"; + return FALSE; + } + + // get the JPEG headers + $jpeg_header_data = get_jpeg_header_data( $filename ); + + + // Flag that an EXIF segment has not been found yet + $EXIF_Location = -1; + + //Cycle through the header segments + for( $i = 0; $i < count( $jpeg_header_data ); $i++ ) + { + // If we find an APP1 header, + if ( strcmp ( $jpeg_header_data[$i]['SegName'], "APP1" ) == 0 ) + { + // And if it has the EXIF label, + if ( ( strncmp ( $jpeg_header_data[$i]['SegData'], "Exif\x00\x00", 6) == 0 ) || + ( 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 + { + // Save the location of the EXIF segment + $EXIF_Location = $i; + } + } + + } + + // Check if an EXIF segment was found + if ( $EXIF_Location == -1 ) + { + // Couldn't find any EXIF block to decode + return FALSE; + } + + $filehnd = @fopen($filename, 'rb'); + + // Check if the file opened successfully + if ( ! $filehnd ) + { + // Could't open the file - exit + echo "<p>Could not open file $filename</p>\n"; + return FALSE; + } + + fseek( $filehnd, $jpeg_header_data[$EXIF_Location]['SegDataStart'] + 6 ); + + // Decode the Exif segment into an array and return it + $exif_data = process_TIFF_Header( $filehnd, "TIFF" ); + + + + // Close File + fclose($filehnd); + return $exif_data; +} + +/****************************************************************************** +* End of Function: get_EXIF_JPEG +******************************************************************************/ + + + +/****************************************************************************** +* +* Function: put_EXIF_JPEG +* +* Description: Stores information into a Exchangeable Image File Format (EXIF) +* APP1 segment from an EXIF array. +* +* WARNING: Because the EXIF standard allows pointers to data +* outside the APP1 segment, if there are any such pointers in +* a makernote, this function will DAMAGE them since it will not +* be aware that there is an external pointer. This will often +* happen with Makernotes that include an embedded thumbnail. +* This damage could be prevented where makernotes can be decoded, +* but currently this is not implemented. +* +* +* Parameters: exif_data - The array of EXIF data to insert into the JPEG header +* jpeg_header_data - The JPEG header into which the EXIF data +* should be stored, as from get_jpeg_header_data +* +* Returns: jpeg_header_data - JPEG header array with the EXIF segment inserted +* FALSE - If an error occured +* +******************************************************************************/ + +function put_EXIF_JPEG( $exif_data, $jpeg_header_data ) +{ + // pack the EXIF data into its proper format for a JPEG file + $packed_data = get_TIFF_Packed_Data( $exif_data ); + if ( $packed_data === FALSE ) + { + return $jpeg_header_data; + } + + $packed_data = "Exif\x00\x00$packed_data"; + + //Cycle through the header segments + for( $i = 0; $i < count( $jpeg_header_data ); $i++ ) + { + // If we find an APP1 header, + if ( strcmp ( $jpeg_header_data[$i]['SegName'], "APP1" ) == 0 ) + { + // And if it has the EXIF label, + if ( ( strncmp ( $jpeg_header_data[$i]['SegData'], "Exif\x00\x00", 6) == 0 ) || + ( 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 + { + // Found a preexisting EXIF block - Replace it with the new one and return. + $jpeg_header_data[$i]['SegData'] = $packed_data; + return $jpeg_header_data; + } + } + } + + // No preexisting segment segment found, insert a new one at the start of the header data. + + // Determine highest position of an APP segment at or below APP3, so we can put the + // new APP3 at this position + + + $highest_APP = -1; + + //Cycle through the header segments + for( $i = 0; $i < count( $jpeg_header_data ); $i++ ) + { + // Check if we have found an APP segment at or below APP3, + if ( ( $jpeg_header_data[$i]['SegType'] >= 0xE0 ) && ( $jpeg_header_data[$i]['SegType'] <= 0xE3 ) ) + { + // Found an APP segment at or below APP12 + $highest_APP = $i; + } + } + + // No preexisting EXIF block found, insert a new one at the start of the header data. + array_splice($jpeg_header_data, $highest_APP + 1 , 0, array( array( "SegType" => 0xE1, + "SegName" => "APP1", + "SegDesc" => $GLOBALS[ "JPEG_Segment_Descriptions" ][ 0xE1 ], + "SegData" => $packed_data ) ) ); + return $jpeg_header_data; + +} + +/****************************************************************************** +* End of Function: put_EXIF_JPEG +******************************************************************************/ + + + + +/****************************************************************************** +* +* Function: get_Meta_JPEG +* +* Description: Retrieves information from a Meta APP3 segment and returns it +* in an array. Uses information supplied by the +* get_jpeg_header_data function. +* The Meta segment has the same format as an EXIF segment, but +* uses different tags +* +* Parameters: filename - the filename of the JPEG image to process +* +* Returns: OutputArray - Array of Meta records +* FALSE - If an error occured in decoding +* +******************************************************************************/ + +function get_Meta_JPEG( $filename ) +{ + // Change: Added as of version 1.11 + // Check if a wrapper is being used - these are not currently supported (see notes at top of file) + if ( ( stristr ( $filename, "http://" ) != FALSE ) || ( stristr ( $filename, "ftp://" ) != FALSE ) ) + { + // A HTTP or FTP wrapper is being used - show a warning and abort + echo "HTTP and FTP wrappers are currently not supported with Meta - See EXIF/Meta functionality documentation - a local file must be specified<br>"; + echo "To work on an internet file, copy it locally to start with:<br><br>\n"; + echo "\$newfilename = tempnam ( \$dir, \"tmpmeta\" );<br>\n"; + echo "copy ( \"http://whatever.com\", \$newfilename );<br><br>\n"; + return FALSE; + } + + // get the JPEG headers + $jpeg_header_data = get_jpeg_header_data( $filename ); + + + // Flag that an Meta segment has not been found yet + $Meta_Location = -1; + + //Cycle through the header segments + for( $i = 0; $i < count( $jpeg_header_data ); $i++ ) + { + // If we find an APP3 header, + if ( strcmp ( $jpeg_header_data[$i]['SegName'], "APP3" ) == 0 ) + { + // And if it has the Meta label, + if ( ( strncmp ( $jpeg_header_data[$i]['SegData'], "Meta\x00\x00", 6) == 0 ) || + ( strncmp ( $jpeg_header_data[$i]['SegData'], "META\x00\x00", 6) == 0 ) ) + { + // Save the location of the Meta segment + $Meta_Location = $i; + } + } + } + + // Check if an EXIF segment was found + if ( $Meta_Location == -1 ) + { + // Couldn't find any Meta block to decode + return FALSE; + } + + + $filehnd = @fopen($filename, 'rb'); + + // Check if the file opened successfully + if ( ! $filehnd ) + { + // Could't open the file - exit + echo "<p>Could not open file $filename</p>\n"; + return FALSE; + } + + fseek( $filehnd, $jpeg_header_data[$Meta_Location]['SegDataStart'] + 6 ); + + // Decode the Meta segment into an array and return it + $meta = process_TIFF_Header( $filehnd, "Meta" ); + + // Close File + fclose($filehnd); + + return $meta; +} + +/****************************************************************************** +* End of Function: get_Meta +******************************************************************************/ + + + + + + + +/****************************************************************************** +* +* Function: put_Meta_JPEG +* +* Description: Stores information into a Meta APP3 segment from a Meta array. +* +* +* WARNING: Because the Meta (EXIF) standard allows pointers to data +* outside the APP1 segment, if there are any such pointers in +* a makernote, this function will DAMAGE them since it will not +* be aware that there is an external pointer. This will often +* happen with Makernotes that include an embedded thumbnail. +* This damage could be prevented where makernotes can be decoded, +* but currently this is not implemented. +* +* +* Parameters: meta_data - The array of Meta data to insert into the JPEG header +* jpeg_header_data - The JPEG header into which the Meta data +* should be stored, as from get_jpeg_header_data +* +* Returns: jpeg_header_data - JPEG header array with the Meta segment inserted +* FALSE - If an error occured +* +******************************************************************************/ + +function put_Meta_JPEG( $meta_data, $jpeg_header_data ) +{ + // pack the Meta data into its proper format for a JPEG file + $packed_data = get_TIFF_Packed_Data( $meta_data ); + if ( $packed_data === FALSE ) + { + return $jpeg_header_data; + } + + $packed_data = "Meta\x00\x00$packed_data"; + + //Cycle through the header segments + for( $i = 0; $i < count( $jpeg_header_data ); $i++ ) + { + // If we find an APP1 header, + if ( strcmp ( $jpeg_header_data[$i]['SegName'], "APP3" ) == 0 ) + { + // And if it has the Meta label, + if ( ( strncmp ( $jpeg_header_data[$i]['SegData'], "Meta\x00\x00", 6) == 0 ) || + ( strncmp ( $jpeg_header_data[$i]['SegData'], "META\x00\x00", 6) == 0 ) ) + { + // Found a preexisting Meta block - Replace it with the new one and return. + $jpeg_header_data[$i]['SegData'] = $packed_data; + return $jpeg_header_data; + } + } + } + // No preexisting segment segment found, insert a new one at the start of the header data. + + // Determine highest position of an APP segment at or below APP3, so we can put the + // new APP3 at this position + + + $highest_APP = -1; + + //Cycle through the header segments + for( $i = 0; $i < count( $jpeg_header_data ); $i++ ) + { + // Check if we have found an APP segment at or below APP3, + if ( ( $jpeg_header_data[$i]['SegType'] >= 0xE0 ) && ( $jpeg_header_data[$i]['SegType'] <= 0xE3 ) ) + { + // Found an APP segment at or below APP12 + $highest_APP = $i; + } + } + + // No preexisting Meta block found, insert a new one at the start of the header data. + array_splice($jpeg_header_data, $highest_APP + 1 , 0, array( array( "SegType" => 0xE3, + "SegName" => "APP3", + "SegDesc" => $GLOBALS[ "JPEG_Segment_Descriptions" ][ 0xE1 ], + "SegData" => $packed_data ) ) ); + return $jpeg_header_data; + +} + +/****************************************************************************** +* End of Function: put_Meta_JPEG +******************************************************************************/ + + + +/****************************************************************************** +* +* Function: get_EXIF_TIFF +* +* Description: Retrieves information from a Exchangeable Image File Format (EXIF) +* within a TIFF file and returns it in an array. +* +* Parameters: filename - the filename of the TIFF image to process +* +* Returns: OutputArray - Array of EXIF records +* FALSE - If an error occured in decoding +* +******************************************************************************/ + +function get_EXIF_TIFF( $filename ) +{ + // Change: Added as of version 1.11 + // Check if a wrapper is being used - these are not currently supported (see notes at top of file) + if ( ( stristr ( $filename, "http://" ) != FALSE ) || ( stristr ( $filename, "ftp://" ) != FALSE ) ) + { + // A HTTP or FTP wrapper is being used - show a warning and abort + echo "HTTP and FTP wrappers are currently not supported with TIFF - See EXIF/TIFF functionality documentation - a local file must be specified<br>"; + echo "To work on an internet file, copy it locally to start with:<br><br>\n"; + echo "\$newfilename = tempnam ( \$dir, \"tmptiff\" );<br>\n"; + echo "copy ( \"http://whatever.com\", \$newfilename );<br><br>\n"; + return FALSE; + } + + + $filehnd = @fopen($filename, 'rb'); + + // Check if the file opened successfully + if ( ! $filehnd ) + { + // Could't open the file - exit + echo "<p>Could not open file $filename</p>\n"; + return FALSE; + } + + // Decode the Exif segment into an array and return it + $exif_data = process_TIFF_Header( $filehnd, "TIFF" ); + + // Close File + fclose($filehnd); + return $exif_data; +} + +/****************************************************************************** +* End of Function: get_EXIF_TIFF +******************************************************************************/ + + + + +/****************************************************************************** +* +* Function: Interpret_EXIF_to_HTML +* +* Description: Generates html detailing the contents an APP1 EXIF array +* which was retrieved with a get_EXIF_.... function. +* Can also be used for APP3 Meta arrays. +* +* Parameters: Exif_array - the EXIF array,as read from get_EXIF_.... +* filename - the name of the Image file being processed ( used +* by scripts which displays EXIF thumbnails) +* +* Returns: output_str - A string containing the HTML +* +******************************************************************************/ + +function Interpret_EXIF_to_HTML( $Exif_array, $filename ) +{ + // Create the string to receive the html output + $output_str = ""; + + // Check if the array to process is valid + if ( $Exif_array === FALSE ) + { + // Exif Array is not valid - abort processing + return $output_str; + } + + // Ouput the heading according to what type of tags were used in processing + if ( $Exif_array[ 'Tags Name' ] == "TIFF" ) + { + $output_str .= "<h2 class=\"EXIF_Main_Heading\">Contains Exchangeable Image File Format (EXIF) Information</h2>\n"; + } + else if ( $Exif_array[ 'Tags Name' ] == "Meta" ) + { + $output_str .= "<h2 class=\"EXIF_Main_Heading\">Contains META Information (APP3)</h2>\n"; + } + else + { + $output_str .= "<h2 class=\"EXIF_Main_Heading\">Contains " . $Exif_array[ 'Tags Name' ] . " Information</h2>\n"; + } + + + // Check that there are actually items to process in the array + if ( count( $Exif_array ) < 1 ) + { + // No items to process in array - abort processing + return $output_str; + } + + // Output secondary heading + $output_str .= "<h3 class=\"EXIF_Secondary_Heading\">Main Image Information</h2>\n"; + + // Interpret the zeroth IFD to html + $output_str .= interpret_IFD( $Exif_array[0], $filename, $Exif_array['Byte_Align'] ); + + // Check if there is a first IFD to process + if ( array_key_exists( 1, $Exif_array ) ) + { + // There is a first IFD for a thumbnail + // Add a heading for it to the output + $output_str .= "<h3 class=\"EXIF_Secondary_Heading\">Thumbnail Information</h2>\n"; + + // Interpret the IFD to html and add it to the output + $output_str .= interpret_IFD( $Exif_array[1], $filename, $Exif_array['Byte_Align'] ); + } + + // Cycle through any other IFD's + $i = 2; + while ( array_key_exists( $i, $Exif_array ) ) + { + // Add a heading for the IFD + $output_str .= "<h3 class=\"EXIF_Secondary_Heading\">Image File Directory (IFD) $i Information</h2>\n"; + + // Interpret the IFD to html and add it to the output + $output_str .= interpret_IFD( $Exif_array[$i], $filename, $Exif_array['Byte_Align'] ); + $i++; + } + + // Return the resulting HTML + return $output_str; +} + +/****************************************************************************** +* End of Function: Interpret_EXIF_to_HTML +******************************************************************************/ + + + + + + + + + + + + + + + + +/****************************************************************************** +* +* INTERNAL FUNCTIONS +* +******************************************************************************/ + + + + + + + + + + + +/****************************************************************************** +* +* Internal Function: get_TIFF_Packed_Data +* +* Description: Packs TIFF IFD data from EXIF or Meta into a form ready for +* either a JPEG EXIF/Meta segment or a TIFF file +* This function attempts to protect the contents of an EXIF makernote, +* by ensuring that it remains in the same position relative to the +* TIFF header +* +* Parameters: tiff_data - the EXIF array,as read from get_EXIF_JPEG or get_Meta_JPEG +* +* Returns: packed_data - A string containing packed segment +* +******************************************************************************/ + +function get_TIFF_Packed_Data( $tiff_data ) +{ + // Check that the segment is valid + if ( $tiff_data === FALSE ) + { + return FALSE; + } + + // Get the byte alignment + $Byte_Align = $tiff_data['Byte_Align']; + + // Add the Byte Alignment to the Packed data + $packed_data = $Byte_Align; + + // Add the TIFF ID to the Packed Data + $packed_data .= put_IFD_Data_Type( 42, 3, $Byte_Align ); + + // Create a string for the makernote + $makernote = ""; + + // Check if the makernote exists + if ( $tiff_data[ 'Makernote_Tag' ] !== FALSE ) + { + // A makernote exists - We need to ensure that it stays in the same position as it was + // Put the Makernote before any of the IFD's by padding zeros to the correct offset + $makernote .= str_repeat("\x00",( $tiff_data[ 'Makernote_Tag' ][ 'Offset' ] - 8 ) ); + $makernote .= $tiff_data[ 'Makernote_Tag' ]['Data']; + } + + // Calculage where the zeroth ifd will be + $ifd_offset = strlen( $makernote ) + 8; + + // Add the Zeroth IFD pointer to the packed data + $packed_data .= put_IFD_Data_Type( $ifd_offset, 4, $Byte_Align ); + + // Add the makernote to the packed data (if there was one) + $packed_data .= $makernote; + + //Add the IFD's to the packed data + $packed_data .= get_IFD_Array_Packed_Data( $tiff_data, $ifd_offset, $Byte_Align ); + + // Return the result + return $packed_data; +} + +/****************************************************************************** +* End of Function: get_TIFF_Packed_Data +******************************************************************************/ + + + + +/****************************************************************************** +* +* Internal Function: get_IFD_Array_Packed_Data +* +* Description: Packs a chain of IFD's from EXIF or Meta segments into a form +* ready for either a JPEG EXIF/Meta segment or a TIFF file +* +* Parameters: ifd_data - the IFD chain array, as read from get_EXIF_JPEG or get_Meta_JPEG +* Zero_IFD_offset - The offset to the first IFD from the start of the TIFF header +* Byte_Align - the Byte alignment to use - "MM" or "II" +* +* Returns: packed_data - A string containing packed IFD's +* +******************************************************************************/ + +function get_IFD_Array_Packed_Data( $ifd_data, $Zero_IFD_offset, $Byte_Align ) +{ + // Create a string to receive the packed output + $packed_data = ""; + + // Count the IFDs + $ifd_count = 0; + foreach( $ifd_data as $key => $IFD ) + { + // Make sure we only count the IFD's, not other information keys + if ( is_numeric( $key ) ) + { + $ifd_count++; + } + } + + + // Cycle through each IFD, + for ( $ifdno = 0; $ifdno < $ifd_count; $ifdno++ ) + { + // Check if this IFD is the last one + if ( $ifdno == $ifd_count - 1 ) + { + // This IFD is the last one, get it's packed data + $packed_data .= get_IFD_Packed_Data( $ifd_data[ $ifdno ], $Zero_IFD_offset +strlen($packed_data), $Byte_Align, FALSE ); + } + else + { + // This IFD is NOT the last one, get it's packed data + $packed_data .= get_IFD_Packed_Data( $ifd_data[ $ifdno ], $Zero_IFD_offset +strlen($packed_data), $Byte_Align, TRUE ); + } + + } + + // Return the packed output + return $packed_data; +} + +/****************************************************************************** +* End of Function: get_IFD_Array_Packed_Data +******************************************************************************/ + + + +/****************************************************************************** +* +* Internal Function: get_IFD_Packed_Data +* +* Description: Packs an IFD from EXIF or Meta segments into a form +* ready for either a JPEG EXIF/Meta segment or a TIFF file +* +* Parameters: ifd_data - the IFD chain array, as read from get_EXIF_JPEG or get_Meta_JPEG +* IFD_offset - The offset to the IFD from the start of the TIFF header +* Byte_Align - the Byte alignment to use - "MM" or "II" +* Another_IFD - boolean - false if this is the last IFD in the chain +* - true if it is not the last +* +* Returns: packed_data - A string containing packed IFD's +* +******************************************************************************/ + +function get_IFD_Packed_Data( $ifd_data, $IFD_offset, $Byte_Align, $Another_IFD ) +{ + + $ifd_body_str = ""; + $ifd_data_str = ""; + + $Tag_Definitions_Name = $ifd_data[ 'Tags Name' ]; + + + // Count the Tags in this IFD + $tag_count = 0; + foreach( $ifd_data as $key => $tag ) + { + // Make sure we only count the Tags, not other information keys + if ( is_numeric( $key ) ) + { + $tag_count++; + } + } + + // Add the Tag count to the packed data + $packed_data = put_IFD_Data_Type( $tag_count, 3, $Byte_Align ); + + // Calculate the total length of the IFD (without the offset data) + $IFD_len = 2 + $tag_count * 12 + 4; + + + // Cycle through each tag + foreach( $ifd_data as $key => $tag ) + { + // Make sure this is a tag, not another information key + if ( is_numeric( $key ) ) + { + + // Add the tag number to the packed data + $ifd_body_str .= put_IFD_Data_Type( $tag[ 'Tag Number' ], 3, $Byte_Align ); + + // Add the Data type to the packed data + $ifd_body_str .= put_IFD_Data_Type( $tag['Data Type'], 3, $Byte_Align ); + + // Check if this is a Print Image Matching entry + if ( $tag['Type'] == "PIM" ) + { + // This is a Print Image Matching entry, + // encode it + $data = Encode_PIM( $tag, $Byte_Align ); + } + // Check if this is a IPTC/NAA Record within the EXIF IFD + else if ( ( ( $Tag_Definitions_Name == "EXIF" ) || ( $Tag_Definitions_Name == "TIFF" ) ) && + ( $tag[ 'Tag Number' ] == 33723 ) ) + { + // This is a IPTC/NAA Record, encode it + $data = put_IPTC( $tag['Data'] ); + } + // Change: Check for embedded XMP as of version 1.11 + // Check if this is a XMP Record within the EXIF IFD + else if ( ( ( $Tag_Definitions_Name == "EXIF" ) || ( $Tag_Definitions_Name == "TIFF" ) ) && + ( $tag[ 'Tag Number' ] == 700 ) ) + { + // This is a XMP Record, encode it + $data = write_XMP_array_to_text( $tag['Data'] ); + } + // Change: Check for embedded IRB as of version 1.11 + // Check if this is a Photoshop IRB Record within the EXIF IFD + else if ( ( ( $Tag_Definitions_Name == "EXIF" ) || ( $Tag_Definitions_Name == "TIFF" ) ) && + ( $tag[ 'Tag Number' ] == 34377 ) ) + { + // This is a Photoshop IRB Record, encode it + $data = pack_Photoshop_IRB_Data( $tag['Data'] ); + } + // Exif Thumbnail Offset + else if ( ( $tag[ 'Tag Number' ] == 513 ) && ( $Tag_Definitions_Name == "TIFF" ) ) + { + // The Exif Thumbnail Offset is a pointer but of type Long, not Unknown + // Hence we need to put the data into the packed string separately + // Calculate the thumbnail offset + $data_offset = $IFD_offset + $IFD_len + strlen($ifd_data_str); + + // Create the Offset for the IFD + $data = put_IFD_Data_Type( $data_offset, 4, $Byte_Align ); + + // Store the thumbnail + $ifd_data_str .= $tag['Data']; + } + // Exif Thumbnail Length + else if ( ( $tag[ 'Tag Number' ] == 514 ) && ( $Tag_Definitions_Name == "TIFF" ) ) + { + // Encode the Thumbnail Length + $data = put_IFD_Data_Type( strlen($ifd_data[513]['Data']), 4, $Byte_Align ); + } + // Sub-IFD + else if ( $tag['Type'] == "SubIFD" ) + { + // This is a Sub-IFD + // Calculate the offset to the start of the Sub-IFD + $data_offset = $IFD_offset + $IFD_len + strlen($ifd_data_str); + // Get the packed data for the IFD chain as the data for this tag + $data = get_IFD_Array_Packed_Data( $tag['Data'], $data_offset, $Byte_Align ); + } + else + { + // Not a special tag + + // Create a string to receive the data + $data = ""; + + // Check if this is a type Unknown tag + if ( $tag['Data Type'] != 7 ) + { + // NOT type Unknown + // Cycle through each data value and add it to the data string + foreach( $tag[ 'Data' ] as $data_val ) + { + $data .= put_IFD_Data_Type( $data_val, $tag['Data Type'], $Byte_Align ); + } + } + else + { + // This is a type Unknown - just add the data as is to the data string + $data .= $tag[ 'Data' ]; + } + } + + // Pad the data string out to at least 4 bytes + $data = str_pad ( $data, 4, "\x00" ); + + + // Check if the data type is an ASCII String or type Unknown + if ( ( $tag['Data Type'] == 2 ) || ( $tag['Data Type'] == 7 ) ) + { + // This is an ASCII String or type Unknown + // Add the Length of the string to the packed data as the Count + $ifd_body_str .= put_IFD_Data_Type( strlen($data), 4, $Byte_Align ); + } + else + { + // Add the array count to the packed data as the Count + $ifd_body_str .= put_IFD_Data_Type( count($tag[ 'Data' ]), 4, $Byte_Align ); + } + + + // Check if the data is over 4 bytes long + if ( strlen( $data ) > 4 ) + { + // Data is longer than 4 bytes - it needs to be offset + // Check if this entry is the Maker Note + if ( ( $Tag_Definitions_Name == "EXIF" ) && ( $tag[ 'Tag Number' ] == 37500 ) ) + { + // This is the makernote - It will have already been stored + // at its original offset to help preserve it + // all we need to do is add the Offset to the IFD packed data + $data_offset = $tag[ 'Offset' ]; + + $ifd_body_str .= put_IFD_Data_Type( $data_offset, 4, $Byte_Align ); + } + else + { + // This is NOT the makernote + // Calculate the data offset + $data_offset = $IFD_offset + $IFD_len + strlen($ifd_data_str); + + // Add the offset to the IFD packed data + $ifd_body_str .= put_IFD_Data_Type( $data_offset, 4, $Byte_Align ); + + // Add the data to the offset packed data + $ifd_data_str .= $data; + } + } + else + { + // Data is less than or equal to 4 bytes - Add it to the packed IFD data as is + ... [truncated message content] |