gpt4 book ai didi

php - 在 php 中写入 exif 数据

转载 作者:IT王子 更新时间:2023-10-29 00:11:37 24 4
gpt4 key购买 nike

我正在尝试创建一个网站,我可以在其中添加和修改 JPEG 文件中的元数据。

有没有一种方法可以让我以相当简单的方式写入 exif 数据。

我见过一两个例子,但它们太复杂了,无法在我给出的时间范围内掌握。

我知道 IPTC,我知道可以将元数据添加到 JPEG 文件中。但这样做的正确方法是什么?

如果有人可以提供一些关于如何使用 EXIF 或 IPTC 或任何其他库或 PHP 功能将元数据添加到 JPEG 的帮助,那么我将不胜感激。

更新:

首先感谢dbers的回复.

我已经查看了代码。我已经设法将默认标签添加到 JPG 中。

我仍然对一小部分代码的含义感到困惑。

例如在php函数中写入exif数据:

function iptc_make_tag($rec, $data, $value) 
{
$length = strlen($value);
$retval = chr(0x1C) . chr($rec) . chr($data);
...
}

我还没有遇到函数变量,$rec$data$value 没有的话如何被引用被定义。或者它们是从 iptc_make_tag 中获取的?

我回显了 $rec$value 但我没有在屏幕上得到返回值。

if(isset($info['APP13']))

我不确定 APP13 是什么意思,当我尝试回显 $info 时,当我在表中回显 $info 时,我只是得到以下信息。

'2#120' => 'Test image','2#116' => 'Copyright 2008-2009, The PHP Group'

最佳答案

我知道您找到了解决方案,但这可能会对其他正在寻找相同问题的人有所帮助!

我修改了一个我找到的类 here (感谢 debers)。

所有对 IPTC 标签的引用都可以从这个 PDF 中读取

现在是代码(PHP >= 5.4):

<?
define("IPTC_OBJECT_NAME", "005");
define("IPTC_EDIT_STATUS", "007");
define("IPTC_PRIORITY", "010");
define("IPTC_CATEGORY", "015");
define("IPTC_SUPPLEMENTAL_CATEGORY", "020");
define("IPTC_FIXTURE_IDENTIFIER", "022");
define("IPTC_KEYWORDS", "025");
define("IPTC_RELEASE_DATE", "030");
define("IPTC_RELEASE_TIME", "035");
define("IPTC_SPECIAL_INSTRUCTIONS", "040");
define("IPTC_REFERENCE_SERVICE", "045");
define("IPTC_REFERENCE_DATE", "047");
define("IPTC_REFERENCE_NUMBER", "050");
define("IPTC_CREATED_DATE", "055");
define("IPTC_CREATED_TIME", "060");
define("IPTC_ORIGINATING_PROGRAM", "065");
define("IPTC_PROGRAM_VERSION", "070");
define("IPTC_OBJECT_CYCLE", "075");
define("IPTC_BYLINE", "080");
define("IPTC_BYLINE_TITLE", "085");
define("IPTC_CITY", "090");
define("IPTC_PROVINCE_STATE", "095");
define("IPTC_COUNTRY_CODE", "100");
define("IPTC_COUNTRY", "101");
define("IPTC_ORIGINAL_TRANSMISSION_REFERENCE", "103");
define("IPTC_HEADLINE", "105");
define("IPTC_CREDIT", "110");
define("IPTC_SOURCE", "115");
define("IPTC_COPYRIGHT_STRING", "116");
define("IPTC_CAPTION", "120");
define("IPTC_LOCAL_CAPTION", "121");

class IPTC
{
var $meta = [];
var $file = null;

function __construct($filename)
{
$info = null;

$size = getimagesize($filename, $info);

if(isset($info["APP13"])) $this->meta = iptcparse($info["APP13"]);

$this->file = $filename;
}

function getValue($tag)
{
return isset($this->meta["2#$tag"]) ? $this->meta["2#$tag"][0] : "";
}

function setValue($tag, $data)
{
$this->meta["2#$tag"] = [$data];

$this->write();
}

private function write()
{
$mode = 0;

$content = iptcembed($this->binary(), $this->file, $mode);

$filename = $this->file;

if(file_exists($this->file)) unlink($this->file);

$fp = fopen($this->file, "w");
fwrite($fp, $content);
fclose($fp);
}

private function binary()
{
$data = "";

foreach(array_keys($this->meta) as $key)
{
$tag = str_replace("2#", "", $key);
$data .= $this->iptc_maketag(2, $tag, $this->meta[$key][0]);
}

return $data;
}

function iptc_maketag($rec, $data, $value)
{
$length = strlen($value);
$retval = chr(0x1C) . chr($rec) . chr($data);

if($length < 0x8000)
{
$retval .= chr($length >> 8) . chr($length & 0xFF);
}
else
{
$retval .= chr(0x80) .
chr(0x04) .
chr(($length >> 24) & 0xFF) .
chr(($length >> 16) & 0xFF) .
chr(($length >> 8) & 0xFF) .
chr($length & 0xFF);
}

return $retval . $value;
}

function dump()
{
echo "<pre>";
print_r($this->meta);
echo "</pre>";
}

#requires GD library installed
function removeAllTags()
{
$this->meta = [];
$img = imagecreatefromstring(implode(file($this->file)));
if(file_exists($this->file)) unlink($this->file);
imagejpeg($img, $this->file, 100);
}
}

$file = "photo.jpg";
$objIPTC = new IPTC($file);

//set title
$objIPTC->setValue(IPTC_HEADLINE, "A title for this picture");

//set description
$objIPTC->setValue(IPTC_CAPTION, "Some words describing what can be seen in this picture.");

echo $objIPTC->getValue(IPTC_HEADLINE);
?>

关于php - 在 php 中写入 exif 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5384962/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com