Jump to content

Changing metadata in an photo.


pewe

Recommended Posts

In a previous post I explained a problem I was having extracting metadata from a photo and then adding it back after resizing the photo.

I managed to solve that problem but have since discovered another issue.

 

When I resize the image the exif data written back to it contains the original size and what I need to store is the new size - but I am now completely lost as to how to go about doing this.

 

The command to extract the data from the image before resizing is

 

$exif_data = get_EXIF_JPEG( $filename );

 

The function 'get_EXIF_JPEG' is not a standard PHP function, but one contained in a separate file (EXIF.php) called as an 'include' in the resizing script.

However I assume that the information is called into an array because a later instruction

 

put_EXIF_JPEG( $exif_data, $jpeg_header_data );

 

writes it 'as is' back to the new image.

 

How can I :

- determine the structure/content of the array

- alter some of the content (ie the size information)

before writing it back to the new image.

 

 

Any and all assistance appreciated.

 

This is the function 'get_EXIF_JPEG' from the included script:


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;
}

Link to comment
https://forums.phpfreaks.com/topic/277907-changing-metadata-in-an-photo/
Share on other sites

Update:

 

I have managed to view the content of the array '$exif_data' and the part I need to alter is the part of the array containing the the size.

There are 2 variables in the resize script which are

$new_width,

$new_height

 

I need these to replace the values in the array which are shown below as '2000' and '1500'

[40962] => Array ( [Tag Number] => 40962 [Tag Name] => Pixel X Dimension [Tag Description] => [Data Type] => 4 [Type] => Numeric [Units] => pixels [Data] => Array ( [0] => 2000 ) [Text Value] => 2000 pixels [Decoded] => 1 )

[40963] => Array ( [Tag Number] => 40963 [Tag Name] => Pixel Y Dimension [Tag Description] => [Data Type] => 4 [Type] => Numeric [Units] => pixels [Data] => Array ( [0] => 1500 ) [Text Value] => 1500 pixels [Decoded] => 1 )

I am not sure how to code this to replace the correct part of the content. Any assistance/suggestions appreciated?

Thanks for the response JC.

 

I tried adding your suggestion to my script but unfortunately it returns errors in the 'included' script that is used to write back to the photo.

 

In my haste I copied what you placed above - with no results at all (not even entries in the error log).

 

I then realised that the $data may well need changing to $exif_data in my situation.

However when I used that the error log reported

 

[12-May-2013 06:32:59 America/Chicago] PHP Warning:  Invalid argument supplied for foreach() in /home2/webi8726/public_html/uko.com/testexif/EXIF.php on line 885
[12-May-2013 06:32:59 America/Chicago] PHP Warning:  Invalid argument supplied for foreach() in /home2/webi8726/public_html/uko.com/testexif/EXIF.php on line 785
[12-May-2013 06:32:59 America/Chicago] PHP Warning:  Invalid argument supplied for foreach() in /home2/webi8726/public_html/uko.com/testexif/EXIF.php on line 802

 

 

EXIF.php is the script which is called to read/write the metadata.

 

The relevant lines contain:

 

885 - foreach( $tag[ 'Data' ] as $data_val )

785 - foreach( $ifd_data as $key => $tag )

802 - foreach( $ifd_data as $key => $tag )

 

So now I am stumped :confused:

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.