2013-05-15 37 views
2

我有一個PHP腳本,其中一些代碼獲取從相片的EXIF數據到一個變量如下:改變數組的內容在PHP

$exif_data = get_EXIF_JPEG($filename); 

注:函數「get_EXIF_JPEG($文件名) '從'包含'腳本中調用。

我然後使用打印變量的內容,找出一定的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'; 

這顯然是不對的,因爲它不會改變現有的數據,它的作用是在變量保存的數據的末尾添加信息。

任何人都可以告訴我必要的代碼應該是什麼 - 或者指向我可以獲取某些信息的方向。

+1

你想做什麼?調整照片大小並更新其exif數據或什麼? –

+0

你明白了 - 最終我想調整照片大小並更新exif數據。我有一個腳本來調整大小,幷包含一些代碼,這些代碼會將修改後的數據重新寫入照片。該查詢是關於在重寫它之前改變從照片中讀取的數據的正確部分。 – PhotoNewbie

+0

關鍵'40962'不在你已經顯示的_full_數組的最上層 - 所以'$ exif_data [40962] ['Data'] [0]'_將會_添加一個新的條目到你的數組中。找出鍵__ove_'40962',然後更新_correct_元素。 – CBroe

回答

0

你錯過了一個級別

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

謝謝伊戈爾。我嘗試了您的建議,並將數據添加到了我以前的代碼的不同位置,但仍未覆蓋所需的數據。我不知道如何找到正確的練級,因爲「轉儲」只是一個長文本打印(如http://ko.com/testexif2/phptest.php所示) – PhotoNewbie

+0

其實我錯過了多個級別 - 但問題現在解決了。感謝所有的貢獻。 – PhotoNewbie

+0

@PhotoNewbie歡迎您 –

0

這裏是get_exif_jpeg代碼從包括PHP文件

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

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