读取文件的元数据

发布于 2024-12-27 03:49:00 字数 479 浏览 2 评论 0原文

我正在为 PHP 制作一个画廊实现。

管理员可以访问上传页面,上传图像并对其进行分类。到目前为止,一切都很好。

该实现将允许用户对管理员上传的图片发表评论,因此我正在实现一个数据库表来将评论与各自的图像链接起来。

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

到目前为止,一切都很好。

我希望管理员能够重命名文件(不一定通过站点,而是通过 FTP 或其他根访问权限),并且不会破坏整个系统。

所以我正在考虑实现一个元数据系统来将 ID 与图像链接起来。这将为我节省第一个表,并允许自由操作图像(将其移动到文件夹内,重命名等)。

问题是,我该如何实施?如何写入附加到文件的元数据以及如何读取它?谷歌没有给出这方面的实际结果。

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

将不胜感激任何帮助!

I am making a gallery implementation for PHP.

The administrator is able to access the upload page, where he would upload and categorize the image. So far so good.

The implementation will allow users to comment on the pictures the admin has uploaded, so I'm implementing a database table to link comments with their respective images.

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

So far so good.

I want the administrator to have the ability to rename files (not necessarily via the site, but via FTP or otherwise root access), and not break the whole system.

So I was thinking of implementing a metadata system to link the ID with the image. That would save me both the first table, and will allow free manipulation of the image (both move it inside folders, rename it, etc).

The question is, how do I implement it? How do I write a metadata attached to a file, and how do I read it? Google gave no real results on this one.

If there's a better way of doing it, I'd love to hear as well!

Will appreciate any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不即不离 2025-01-03 03:49:00

虽然我自己没有使用过,但 sourceforge 上的 XMP PHP 工具包听起来正是您可能正在寻找的: http://xmpphptoolkit .sourceforge.net/ 话虽这么说,它还是 alpha 版本,而且看起来已经有一年多没有更新了。

XMP Toolkit PHP 扩展是一个 PHP 模块,其中包括 Adob​​e XMP
工具包SDK。这个 PHP5 扩展将提供类和方法
操作 jpegs、tiff、png 等文件中的 XMP 元数据,还可以
wav、mp3、avi、mpeg4、pdf、ai、eps…它基于 Adob​​e XMP
工具包 SDK 4.4.2。这个扩展的目标是拥有 php 类
它可以打开文件、提取元数据、操作它们并将它们放入
返回几行 php 代码。该项目受 GPL v3 约束
许可证。

您还可以使用 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,$this->file,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();

While I have not used this myself the XMP PHP Toolkit on sourceforge sounds like just what you might be looking for: http://xmpphptoolkit.sourceforge.net/ That being said - it's in alpha and hasn't been updated in over a year it appears.

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.

You are also be able to write arbitrary metadata to an image file with iptcembed. As you mention in your comment this only works for JPEG files.

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

Here is a script from the comments of a class that will get and set IPTC data:

<?

    /************************************************************\

        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,$this->file,100);
        }
    };


?>

Example read copyright string:

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

Update copyright statement:

$i = new iptc("test.jpg");
echo $i->set(IPTC_COPYRIGHT_STRING,"Here goes the new data"); 
$i->write();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文