📄 iptc.php
字号:
<?php
/******************************************************************************
*
* Filename: IPTC.php
*
* Description: Provides functions for reading and writing IPTC-NAA Information
* Interchange Model metadata
*
* Author: Evan Hunter
*
* Date: 23/7/2004
*
* Project: PHP JPEG Metadata Toolkit
*
* Revision: 1.10
*
* Changes: 1.00 -> 1.01 : changed get_IPTC to return partial data when error occurs
* 1.01 -> 1.10 : changed put_IPTC to check if the incoming IPTC block is valid
* changed Interpret_IPTC_to_HTML, adding nl2br functions for each text field,
* so that multiline text displays properly
*
* 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: evan@ozhiker.com
*
******************************************************************************/
// TODO: Not all of the IPTC fields have been tested properly
/******************************************************************************
*
* Function: get_IPTC
*
* Description: Extracts IPTC-NAA IIM data from the string provided, and returns
* the information as an array
*
* Parameters: Data_Str - the string containing the IPTC-NAA IIM records. Must
* be exact length of the IPTC-NAA IIM data.
*
* Returns: OutputArray - Array of IPTC-NAA IIM records
* FALSE - If an error occured in decoding
*
******************************************************************************/
function get_IPTC( $Data_Str )
{
// Initialise the start position
$pos = 0;
// Create the array to receive the data
$OutputArray = array( );
// Cycle through the IPTC records, decoding and storing them
while( $pos < strlen($Data_Str) )
{
// TODO - Extended Dataset record not supported
// Check if there is sufficient data for reading the record
if ( strlen( substr($Data_Str,$pos) ) < 5 )
{
// Not enough data left for a record - Probably corrupt data - ERROR
// Change: changed to return partial data as of revision 1.01
return $OutputArray;
}
// Unpack data from IPTC record:
// First byte - IPTC Tag Marker - always 28
// Second byte - IPTC Record Number
// Third byte - IPTC Dataset Number
// Fourth and fifth bytes - two byte size value
$iptc_raw = unpack( "CIPTC_Tag_Marker/CIPTC_Record_No/CIPTC_Dataset_No/nIPTC_Size", substr($Data_Str,$pos) );
// Skip position over the unpacked data
$pos += 5;
// Construct the IPTC type string eg 2:105
$iptctype = sprintf( "%01d:%02d", $iptc_raw['IPTC_Record_No'], $iptc_raw['IPTC_Dataset_No']);
// Check if there is sufficient data for reading the record contents
if ( strlen( substr( $Data_Str, $pos, $iptc_raw['IPTC_Size'] ) ) !== $iptc_raw['IPTC_Size'] )
{
// Not enough data left for the record content - Probably corrupt data - ERROR
// Change: changed to return partial data as of revision 1.01
return $OutputArray;
}
// Add the IPTC record to the output array
$OutputArray[] = array( "IPTC_Type" => $iptctype ,
"RecName" => $GLOBALS[ "IPTC_Entry_Names" ][ $iptctype ],
"RecDesc" => $GLOBALS[ "IPTC_Entry_Descriptions" ][ $iptctype ],
"RecData" => substr( $Data_Str, $pos, $iptc_raw['IPTC_Size'] ) );
// Skip over the IPTC record data
$pos += $iptc_raw['IPTC_Size'];
}
return $OutputArray;
}
/******************************************************************************
* End of Function: get_IPTC
******************************************************************************/
/******************************************************************************
*
* Function: put_IPTC
*
* Description: Encodes an array of IPTC-NAA records into a string encoded
* as IPTC-NAA IIM. (The reverse of get_IPTC)
*
* Parameters: new_IPTC_block - the IPTC-NAA array to be encoded. Should be
* the same format as that received from get_IPTC
*
* Returns: iptc_packed_data - IPTC-NAA IIM encoded string
*
******************************************************************************/
function put_IPTC( $new_IPTC_block )
{
// Check if the incoming IPTC block is valid
if ( $new_IPTC_block == FALSE )
{
// Invalid IPTC block - abort
return FALSE;
}
// Initialise the packed output data string
$iptc_packed_data = "";
// Cycle through each record in the new IPTC block
foreach ($new_IPTC_block as $record)
{
// Extract the Record Number and Dataset Number from the IPTC_Type field
list($IPTC_Record, $IPTC_Dataset) = sscanf( $record['IPTC_Type'], "%d:%d");
// Write the IPTC-NAA IIM Tag Marker, Record Number, Dataset Number and Data Size to the packed output data string
$iptc_packed_data .= pack( "CCCn", 28, $IPTC_Record, $IPTC_Dataset, strlen($record['RecData']) );
// Write the IPTC-NAA IIM Data to the packed output data string
$iptc_packed_data .= $record['RecData'];
}
// Return the IPTC-NAA IIM data
return $iptc_packed_data;
}
/******************************************************************************
* End of Function: put_IPTC
******************************************************************************/
/******************************************************************************
*
* Function: Interpret_IPTC_to_HTML
*
* Description: Generates html detailing the contents a IPTC-NAA IIM array
* which was retrieved with the get_IPTC function
*
* Parameters: IPTC_info - the IPTC-NAA IIM array,as read from get_IPTC
*
* Returns: OutputStr - A string containing the HTML
*
******************************************************************************/
function Interpret_IPTC_to_HTML( $IPTC_info )
{
// Create a string to receive the HTML
$output_str ="";
// Check if the IPTC
if ( $IPTC_info !== FALSE )
{
// Add Heading to HTML
$output_str .= "<h3 class=\"IPTC_Main_Heading\">IPTC-NAA Record</h3>\n";
// Add Table to HTML
$output_str .= "\n<table class=\"IPTC_Table\" border=1>\n";
// Cycle through each of the IPTC-NAA IIM records
foreach( $IPTC_info as $IPTC_Record )
{
// Check if the record is a known IPTC field
$Record_Name = $IPTC_Record['RecName'];
if ( $Record_Name == "" )
{
// Record is an unknown field - add message to HTML
$output_str .= "<tr class=\"IPTC_Table_Row\"><td class=\"IPTC_Caption_Cell\">Unknown IPTC field '". htmlentities( $IPTC_Record['IPTC_Type'] ). "' :</td><td class=\"IPTC_Value_Cell\">" . nl2br( HTML_UTF8_Escape( $IPTC_Record['RecData'] ) ) ."</td></tr>\n";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -