Example Photoshop "File Info" Editor Scripts
These two example scripts areincluded with the PHP JPEG Metadata Toolkit. They show how the library can be used to edit metadata over the internet in exactly the same format as Photoshop"s "File Info" dialog box. This is an explanation of some of the commands used in the script.
Click here to see the Edit_File_Info_Example.phpClick here to see the Write_File_Info.php
Edit_File_Info_Example.php
Overview
This script utilises another script called Edit_File_Info.php.
Edit_File_Info.php outputs the HTML required to display a HTML form which emulates
the Photoshop "File Info" dialog box.
Edit_File_Info.php has four modes of operation:
- If $new_ps_file_info_array is defined then it's data will be used to fill the fields.
- If $new_ps_file_info_array is not defined but $filename is defined, then the file info fields will be filled from the metadata in the file specified
- If $new_ps_file_info_array is not defined but $filename and $default_ps_file_info_array are defined, then the file info fields will be filled from the metadata in the file specified, but where fields are blank, they will be filled from $default_ps_file_info_array
- Otherwise the fields will be blank
Forced Field Values
If the variable $new_ps_file_info_array before Edit_File_Info.php is included, then it forces the values of the fields to be loaded from $new_ps_file_info_array
NOTE:
- date must be in YYYY-MM-DD format
- coyrightstatus must be either "Copyrighted Work", "Public Domain", or "Unknown"
- urgency must be a single digit between 1 and 8 inclusive
- category must be 3 characters or less
- authors position is not used in Photoshop 7 or higher
- jobname is not used in Photoshop CS or higher
Here is some example code for defining $new_ps_file_info_array
$new_ps_file_info_array = array ( 'title' => "Frenchmans Cap", 'author' => "Evan Hunter", 'authorsposition' => "", 'caption' => "A view of Frenchmans cap from a bus on the Lyell Highway", 'captionwriter' => "Evan Hunter", 'jobname' => "", 'copyrightstatus' => "Copyrighted Work", 'copyrightnotice' => "Copyright (c) Evan Hunter 2004", 'ownerurl' => "http://www.ozhiker.com", 'keywords' => array( "Frenchmans Cap", "Franklin Gordon National Park", "Tasmania" ), 'category' => "abc", 'supplementalcategories'=> array( "Supp Category1", "Supp Category2" ), 'date' => "2004-05-11", 'city' => "Franklin Gordon National Park", 'state' => "Tasmania", 'country' => "Australia", 'credit' => "Evan Hunter", 'source' => "Evan Hunter", 'headline' => "Frenchmans Cap from Lyell Highway", 'instructions' => "No Instructions", 'transmissionreference' => "12345", 'urgency' => "3" );
Default Field Values
The variable $default_ps_file_info_array sets the default values of the File Info fields. These are only loaded into the fields where File Info has been loaded from a file, but the fields in question are still blank. It has similar definition requirements to $new_ps_file_info_array.
// Define defaults for the fields - These are only used where the image has blank fields $default_ps_file_info_array = array ( 'title' => "", 'author' => "Evan Hunter", 'authorsposition' => "", 'caption' => "", 'captionwriter' => "Evan Hunter", 'jobname' => "", 'copyrightstatus' => "Copyrighted Work", 'copyrightnotice' => "Copyright (c) Evan Hunter 2004", 'ownerurl' => "http://www.ozhiker.com", 'keywords' => array(), 'category' => "", 'supplementalcategories'=> array(), 'date' => "", 'city' => "", 'state' => "Tasmania", 'country' => "Australia", 'credit' => "Evan Hunter", 'source' => "Evan Hunter", 'headline' => "", 'instructions' => "", 'transmissionreference' => "", 'urgency' => "" );
Input Filename
When the $filename variable is defined before the inclusion of Edit_File_Info.php, it defines which JPEG file should be searched to fill the File Info fields
$filename = "test.jpg";
Output Filename
The $outputfilename variable must always be defined before the inclusion of Edit_File_Info.php. It defines the target image which will be modified with the information that the user enters. It can be the same as $filename.
$outputfilename = "test.jpg";
Including Edit_File_Info.php
After $outputfilename has been defined, and any of the other variables have been defined as required, Edit_File_Info.php should be included. It outputs the HTML for the File Info Editor.
include "Edit_File_Info.php";
Write_File_Info.php
Overview
This script receives the File Info data from the user via the HTML POST method.
Retrieving the POSTed variables
The field data is retrieved as follows:
$new_ps_file_info_array = $GLOBALS['HTTP_POST_VARS'];
Preparing the field data
The POSTed field data has some characters escaped with backslashes, which need to be removed. Two of the fields should also be arrays which and need to be split.
// Some characters are escaped with backslashes in HTML Posted variable // Cycle through each of the HTML Posted variables, and strip out the slashes foreach( $new_ps_file_info_array as $var_key => $var_val ) { $new_ps_file_info_array[ $var_key ] = stripslashes( $var_val ); } // Keywords should be an array - explode it on newline boundarys $new_ps_file_info_array[ 'keywords' ] = explode( "\n", trim( $new_ps_file_info_array[ 'keywords' ] ) ); // Supplemental Categories should be an array - explode it on newline boundarys $new_ps_file_info_array[ 'supplementalcategories' ] = explode( "\n", trim( $new_ps_file_info_array[ 'supplementalcategories' ] ) );
Writing the File Info
Now the File Info data can be written to a file. The following code results in the $jpeg_header_data being updated with the new Photoshop File Info, and it can then be written to a JPEG file as normal using put_jpeg_header_data:
// Retrieve the header information $jpeg_header_data = get_jpeg_header_data( $filename ); // Retreive the EXIF, XMP and Photoshop IRB information from // the existing file, so that it can be updated $Exif_array = get_EXIF_JPEG( $filename ); $XMP_array = read_XMP_array_from_text( get_XMP_text( $jpeg_header_data ) ); $IRB_array = get_Photoshop_IRB( $jpeg_header_data ); // Update the JPEG header information with the new Photoshop File Info $jpeg_header_data = put_photoshop_file_info( $jpeg_header_data, $new_ps_file_info_array, $Exif_array, $XMP_array, $IRB_array );