📄 xmp.php
字号:
<?php
/******************************************************************************
*
* Filename: XMP.php
*
* Description: Provides functions for reading and writing information to/from
* the 'App 1' Extensible Metadata Platform (XMP) segment of JPEG
* format files. This XMP segment is XML based and contains the
* Resource Description Framework (RDF) data, which itself can
* contain the Dublin Core Metadata Initiative (DCMI) information.
*
* Author: Evan Hunter
*
* Date: 27/7/2004
*
* Project: JPEG Metadata
*
* Revision: 1.10
*
* Changes: 1.00 -> 1.04 : changed put_IPTC to fix a bug preventing the correct
* insertion of a XMP block where none existed previously
*
* 1.04 -> 1.10 : changed put_XMP_text to fix some array indexes which were missing qoutes
*
* 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
*
******************************************************************************/
include_once 'XML.php';
/******************************************************************************
*
* Function: get_XMP_text
*
* Description: Retrieves the Extensible Metadata Platform (XMP) information
* from an App1 JPEG segment and returns the raw XML text as a
* string. This includes the Resource Description Framework (RDF)
* information and may include Dublin Core Metadata Initiative (DCMI)
* information. Uses information supplied by the get_jpeg_header_data
* function
*
* Parameters: jpeg_header_data - a JPEG header data array in the same format
* as from get_jpeg_header_data
*
* Returns: xmp_data - the string of raw XML text
* FALSE - if an APP 1 XMP segment could not be found,
* or if an error occured
*
******************************************************************************/
function get_XMP_text( $jpeg_header_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 Adobe XMP/RDF label (http://ns.adobe.com/xap/1.0/\x00) ,
if( strncmp ( $jpeg_header_data[$i]['SegData'], "http://ns.adobe.com/xap/1.0/\x00", 29) == 0 )
{
// Found a XMP/RDF block
// Return the XMP text
$xmp_data = substr ( $jpeg_header_data[$i]['SegData'], 29 );
return $xmp_data;
}
}
}
return FALSE;
}
/******************************************************************************
* End of Function: get_XMP_text
******************************************************************************/
/******************************************************************************
*
* Function: put_XMP_text
*
* Description: Adds or modifies the Extensible Metadata Platform (XMP) information
* in an App1 JPEG segment. If a XMP segment already exists, it is
* replaced, otherwise a new one is inserted, using the supplied data.
* Uses information supplied by the get_jpeg_header_data function
*
* Parameters: jpeg_header_data - a JPEG header data array in the same format
* as from get_jpeg_header_data
* newXMP - a string containing the XMP text to be stored in the XMP
* segment. Should be constructed using the write_XMP_array_to_text
* function
*
* Returns: jpeg_header_data - the JPEG header data array with the
* XMP segment added.
* FALSE - if an error occured
*
******************************************************************************/
function put_XMP_text( $jpeg_header_data, $newXMP )
{
//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 Adobe XMP/RDF label (http://ns.adobe.com/xap/1.0/\x00) ,
if( strncmp ( $jpeg_header_data[$i]['SegData'], "http://ns.adobe.com/xap/1.0/\x00", 29) == 0 )
{
// Found a preexisting XMP/RDF block - Replace it with the new one and return.
$jpeg_header_data[$i]['SegData'] = "http://ns.adobe.com/xap/1.0/\x00" . $newXMP;
return $jpeg_header_data;
}
}
}
// No pre-existing XMP/RDF found - insert a new one after any pre-existing APP0 or APP1 blocks
// Change: changed to initialise $i properly as of revision 1.04
$i = 0;
// Loop until a block is found that isn't an APP0 or APP1
while ( ( $jpeg_header_data[$i]['SegName'] == "APP0" ) || ( $jpeg_header_data[$i]['SegName'] == "APP1" ) )
{
$i++;
}
// Insert a new XMP/RDF APP1 segment at the specified point.
// Change: changed to properly construct array element as of revision 1.04 - requires two array statements not one, requires insertion at $i, not $i - 1
array_splice($jpeg_header_data, $i, 0, array( array( "SegType" => 0xE1,
"SegName" => "APP1",
"SegDesc" => $GLOBALS[ "JPEG_Segment_Descriptions" ][ 0xE1 ],
"SegData" => "http://ns.adobe.com/xap/1.0/\x00" . $newXMP ) ) );
// Return the headers with the new segment inserted
return $jpeg_header_data;
}
/******************************************************************************
* End of Function: put_XMP_text
******************************************************************************/
/******************************************************************************
*
* Function: read_XMP_array_from_text
*
* Description: An alias for read_xml_array_from_text.
* Parses a string containing XMP data (XML), and returns the resulting
* tree structure array, which contains all the XMP (XML) information.
* Note: White space and comments in the XMP data (XML) are ignored
* Note: All text information contained in the tree structure
* is encoded as Unicode UTF-8. Hence text will appear as
* normal ASCII except where there is an extended character.
*
* Parameters: xmptext - a string containing the XMP data (XML) to be parsed
*
* Returns: output - the tree structure array containing the XMP (XML) information
* FALSE - if an error occured
*
******************************************************************************/
function read_XMP_array_from_text( $xmptext )
{
return read_xml_array_from_text( $xmptext );
}
/******************************************************************************
* End of Function: read_XMP_array_from_text
******************************************************************************/
/******************************************************************************
*
* Function: write_XMP_array_to_text
*
* Description: Takes a tree structure array containing XMP (in the same format
* as returned by read_XMP_array_from_text, and constructs a string
* containing the equivalent XMP, including the XMP Packet header
* and trailer. Produces XMP text which has correct indents, encoded
* using UTF-8.
* Note: All text information contained in the tree structure
* can be either 7-bit ASCII or encoded as Unicode UTF-8,
* since UTF-8 passes 7-bit ASCII text unchanged.
*
* Parameters: xmparray - the tree structure array containing the information to
* be converted to XMP text
*
* Returns: output_XMP_text - the string containing the equivalent XMP text
*
******************************************************************************/
function write_XMP_array_to_text( $xmparray )
{
// Add the XMP packet header
// The sequence 0xEFBBBF is the UTF-8 encoded version of the Unicode 搝ero
// width non-breaking space character
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -