加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 读取文件的元数据

发布时间:2020-12-13 17:17:40 所属栏目:PHP教程 来源:网络整理
导读:我正在为 PHP制作一个库实现. 管理员可以访问上传页面,在那里他将上传图像并对图像进行分类.到现在为止还挺好. 该实现将允许用户评论管理员已上传的图片,因此我正在实现数据库表以将评论与其各自的图像链接. id | path .:. id | datetime | comment_title |
我正在为 PHP制作一个库实现.

管理员可以访问上传页面,在那里他将上传图像并对图像进行分类.到现在为止还挺好.

该实现将允许用户评论管理员已上传的图片,因此我正在实现数据库表以将评论与其各自的图像链接.

id | path .:. id | datetime | comment_title | comment_body | uid

到现在为止还挺好.

我希望管理员能够重命名文件(不一定通过网站,但通过FTP或其他root访问),而不是破坏整个系统.

所以我在考虑实现元数据系统来将ID与图像链接起来.这将节省我的第一个表,并将允许自由处理图像(都在文件夹内移动,重命名等).

问题是,我该如何实现它?如何编写附加到文件的元数据,如何阅读?谷歌没有给出真正的结果.

如果有更好的方法,我也很乐意听到!

将不胜感激任何帮助!

解决方法

虽然我自己没有使用过这个,但是在sourceforge上的XMP PHP工具包听起来就像你可能正在寻找的那样: http://xmpphptoolkit.sourceforge.net/据说 – 它是alpha版本,并且在一年多的时间里没有更新.

XMP Toolkit PHP Extension is a PHP module which includes the Adobe XMP
Toolkit SDK. This PHP5 extension will provide classes and methods to
manipulate XMP Metadatas from files like jpegs,tiff,png,but also
wav,mp3,avi,mpeg4,pdf,ai,eps… It’s based from the Adobe XMP
Toolkit SDK 4.4.2. The goal of this extension is to have php classes
which can open files,extract metadatas,manipulate them,and put them
back within few lines of php code. This project is under GPL v3
License.

您还可以使用iptcembed将任意元数据写入图像文件.正如您在评论中提到的,这仅适用于JPEG文件.

http://php.net/manual/en/function.iptcembed.php

这是一个来自类的注释的脚本,它将获取和设置IPTC数据:

<?

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

        IPTC EASY 1.0 - IPTC data manipulator for JPEG images

        All reserved www.image-host-script.com

        Sep 15,2008

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

    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=Array();
        var $hasmeta=false;
        var $file=false;


        function iptc($filename) {
            $size = getimagesize($filename,$info);
            $this->hasmeta = isset($info["APP13"]);
            if($this->hasmeta)
                $this->meta = iptcparse ($info["APP13"]);
            $this->file = $filename;
        }
        function set($tag,$data) {
            $this->meta ["2#$tag"]= Array( $data );
            $this->hasmeta=true;
        }
        function get($tag) {
            return isset($this->meta["2#$tag"]) ? $this->meta["2#$tag"][0] : false;
        }

        function dump() {
            print_r($this->meta);
        }
        function binary() {
            $iptc_new = '';
            foreach (array_keys($this->meta) as $s) {
                $tag = str_replace("2#","",$s);
                $iptc_new .= $this->iptc_maketag(2,$tag,$this->meta[$s][0]);
            }        
            return $iptc_new;    
        }
        function iptc_maketag($rec,$dat,$val) {
            $len = strlen($val);
            if ($len < 0x8000) {
                   return chr(0x1c).chr($rec).chr($dat).
                   chr($len >> 8).
                   chr($len & 0xff).
                   $val;
            } else {
                   return chr(0x1c).chr($rec).chr($dat).
                   chr(0x80).chr(0x04).
                   chr(($len >> 24) & 0xff).
                   chr(($len >> 16) & 0xff).
                   chr(($len >> 8 ) & 0xff).
                   chr(($len ) & 0xff).
                   $val;

            }
        }    
        function write() {
            if(!function_exists('iptcembed')) return false;
            $mode = 0;
            $content = iptcembed($this->binary(),$this->file,$mode);    
            $filename = $this->file;

            @unlink($filename); #delete if exists

            $fp = fopen($filename,"w");
            fwrite($fp,$content);
            fclose($fp);
        }    

        #requires GD library installed
        function removeAllTags() {
            $this->hasmeta=false;
            $this->meta=Array();
            $img = imagecreatefromstring(implode(file($this->file)));
            @unlink($this->file); #delete if exists
            imagejpeg($img,100);
        }
    };


?>

示例读取版权字符串:

$i = new iptc("test.jpg");
echo $i->get(IPTC_COPYRIGHT_STRING);

更新版权声明:

$i = new iptc("test.jpg");
echo $i->set(IPTC_COPYRIGHT_STRING,"Here goes the new data"); 
$i->write();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读