在php中编写exif数据

前端之家收集整理的这篇文章主要介绍了在php中编写exif数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试创建一个网站,我可以在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,$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,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);
?>
原文链接:https://www.f2er.com/php/139807.html

猜你在找的PHP相关文章