在PHP中更改数组的内容

时间:2013-05-15 11:58:02

标签: php arrays exif

我有一个php脚本,其中一些代码将照片中的exif数据提取到变量中,如下所示:

$exif_data = get_EXIF_JPEG( $filename );

注意:“get_EXIF_JPEG($ filename)”函数是从“包含”脚本调用的。

然后,我打印了变量的内容,以了解如何使用以下变量保存某些exif信息:

var_dump ($exif_data);

对于有兴趣查看变量全部内容的人,可以使用此链接查看 - http://uko.com/testexif2/phptest.php

在结果中有一个部分,其中包含相机记录的照片尺寸。该部分的内容是:

[40962]=> array(9) { ["Tag Number"]=> int(40962) ["Tag Name"]=> string(17) "Pixel X Dimension" ["Tag Description"]=> string(0) "" ["Data Type"]=> int(4) ["Type"]=> string(7) "Numeric" ["Units"]=> string(6) "pixels" ["Data"]=> array(1) { [0]=> int(4000) } ["Text Value"]=> string(11) "4000 pixels" ["Decoded"]=> bool(true) }

我想更改此内容以更改记录的图像大小,并找到一些导致我尝试此代码的信息($ new_width在脚本开头指定):

$exif_data[40962]['Data'][0] = $new_width;
$exif_data[40962]['Text Value'] = $new_width . ' pixels';

这显然是不对的,因为它不会改变现有数据,它所做的只是将信息添加到变量中保存的数据的末尾。

任何人都可以告诉我必要的代码应该是什么 - 或者指出我可以获得一些信息帮助的方向。

2 个答案:

答案 0 :(得分:0)

你错过了一个级别

$exif_data[0][40962]['Data'][0] = $new_width;
$exif_data[0][40962]['Text Value'] = $new_width . ' pixels';

答案 1 :(得分:0)

以下是包含的php文件中的get_exif_jpeg代码

******************************************************************************/

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