當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP iptcparse()用法及代碼示例

什麽是IPTC數據?

IPTC 數據通常稱為與圖像文件等相關聯的元數據。它是為使用媒體而創建的標準化元數據格式。此 IPTC 數據通常包括圖像詳細信息,例如位置、描述、標題、日期和版權。

PHP iptcparse() 函數是一個內置函數,用於解析圖像中的 IPTC 數據。

用法:

array|false iptcparse( string $iptc_data )



參數:該函數接受上述和以下描述的單個參數:

  • $iptc_data:它需要我們從圖像中獲取的 IPTC 數據的二進製塊。

返回值:如果出錯或在圖像中找到數據,則返回布爾值 false。如果在圖像中找到數據,則它返回一個帶有標簽標記作為索引的數組。

範例1:以下代碼適用於圖像中存在數據時的圖像。

PHP


<?php
  
// Fetching image size but we actually have to
// use $info that contain meta data of image
// 1st argument is name of the image and second
// is optional but we pass any variable name 
// because it contains meta data 
$img_size = getimagesize('1.jpg', $img_info);
  
/* We have an array of length three of meta
data of image in $img_info variable but we
have to parse only IPTC data so we have to
pass APP13 which contain IPTC data */
$iptc_data = iptcparse($img_info['APP13']);
  
// IPTC data fetched 
foreach ($iptc_data as $key => $value) {
    echo var_dump($key,"---",$value)."<br>";
}
  
echo "<br><b>Fetching data:</b><br>";
  
// Now looping through each and every
// key value pair
foreach ($iptc_data as $key => $value) {
    for ($i=0; $i < sizeof($value); $i++) { 
        echo $key." -- ".$value[$i]."<br>";
    }
}
  
?>

輸出:我們可以看到這些鍵代表——

2#120 — 圖片標題

2#080 — 圖片作者

2#055 — 創建圖像的日期



2#060 — 時間

2#025 — 圖像中的標簽

2#116 — 圖片版權

string(5) “1#090” string(3) “—” array(1) { [0]=> string(3) “%G” }
string(5) “1#000” string(3) “—” array(1) { [0]=> string(2) “” }
string(5) “2#000” string(3) “—” array(1) { [0]=> string(2) “” }
string(5) “2#120” string(3) “—” array(1) { [0]=> string(91) “Front part views of
three exciting steam engines in a railroad museum near Vienna (Austria)” }
string(5) “2#080” string(3) “—” array(1) { [0]=> string(32) “IPTC Photo Metadata WorkingGroup” }
string(5) “2#055” string(3) “—” array(1) { [0]=> string(8) “20191123” }
string(5) “2#060” string(3) “—” array(1) { [0]=> string(6) “000000” }
string(5) “2#025” string(3) “—” array(2) { [0]=> string(8) “railroad” [1]=> string(12) “steam engine” }
string(5) “2#116” string(3) “—” array(1) { [0]=> string(38) “© Copyright 2019, IPTC - www.iptc.org” }
string(5) “2#110” string(3) “—” array(1) { [0]=> string(4) “IPTC” }

Fetching data:
1#090 — %G
1#000 —
2#000 —
2#120 — Front part views of three exciting steam engines in a railroad museum near Vienna (Austria)
2#080 — IPTC Photo Metadata WorkingGroup
2#055 — 20191123
2#060 — 000000
2#025 — railroad
2#025 — steam engine
2#116 — © Copyright 2019, IPTC - www.iptc.org
2#110 — IPTC

示例 2:以下代碼演示了圖像中不存在數據時的函數。

PHP


<?PHP
     
$img_size = getimagesize(
'https://media.geeksforgeeks.org/wp-content/uploads/20210726112758/gfg2.jpg',
    $img_info);
  
echo var_dump($img_info);
$iptc_data = iptcparse($img_info['APP13']);
  
?>

輸出:

array(2) { [“APP0”]=> string(14) “JFIF“” [“APP1”]=> string(32) “ExifMM*” }
Notice:
Undefined index:APP13 in C:\xampp\htdocs\GeeksForGeeks\contentReviewJuly\iptcparse\indexCopy.php on line 4

正如我們所看到的,圖像中不存在 IPTC 塊,它會引發錯誤。

參考:  https://www.php.net/manual/en/function.iptcparse.php




相關用法


注:本文由純淨天空篩選整理自namankumar2592大神的英文原創作品 PHP iptcparse() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。