在 PHP 中写入 exif 和 itpc 数据

发布于 2024-11-04 23:57:58 字数 1671 浏览 1 评论 0原文

我正在尝试制作一个图像上传系统以将元数据添加到文件本身。

我正在使用 GD 库中的 iptcembed,如下所示:

    <?php

// iptc_make_tag() function by Thies C. Arntzen
function iptc_make_tag($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;
}

// Path to jpeg file
$path = './phplogo.jpg';

// We need to check if theres any IPTC data in the jpeg image. If there is then 
// bail out because we cannot embed any image that already has some IPTC data!
$image = getimagesize($path, $info);

if(isset($info['APP13']))
{
    die('Error: IPTC data found in source image, cannot continue');
}

// Set the IPTC tags
$iptc = array(
    '2#120' => 'Test image',
    '2#116' => 'Copyright 2008-2009, The PHP Group'
);

// Convert the IPTC tags into binary code
$data = '';

foreach($iptc as $tag => $string)
{
    $tag = substr($tag, 2);
    $data .= iptc_make_tag(2, $tag, $string);
}

// Embed the IPTC data
$content = iptcembed($data, $path);

// Write the new image data out to the file.
$fp = fopen($path, "wb");
fwrite($fp, $content);
fclose($fp);
?>

但是,当我附加表单并将 $path 更改为上传图像的路径并将 iptc 数组标签更改为变量表单文本字段时数据表单中,没有添加任何信息。

图片将被上传,但作者、版权标签不存在。

I'm trying to make a image upload system to add meta-data to the files themselves.

I'm using the iptcembed from the GD library as shown below:

    <?php

// iptc_make_tag() function by Thies C. Arntzen
function iptc_make_tag($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;
}

// Path to jpeg file
$path = './phplogo.jpg';

// We need to check if theres any IPTC data in the jpeg image. If there is then 
// bail out because we cannot embed any image that already has some IPTC data!
$image = getimagesize($path, $info);

if(isset($info['APP13']))
{
    die('Error: IPTC data found in source image, cannot continue');
}

// Set the IPTC tags
$iptc = array(
    '2#120' => 'Test image',
    '2#116' => 'Copyright 2008-2009, The PHP Group'
);

// Convert the IPTC tags into binary code
$data = '';

foreach($iptc as $tag => $string)
{
    $tag = substr($tag, 2);
    $data .= iptc_make_tag(2, $tag, $string);
}

// Embed the IPTC data
$content = iptcembed($data, $path);

// Write the new image data out to the file.
$fp = fopen($path, "wb");
fwrite($fp, $content);
fclose($fp);
?>

However when I attach a form and change the $path to the path of the uploaded image and change the iptc array tags to variables form textfields in the data form, it doesn't put add the information.

The image will be uploaded but the tags for author, copyright are not there.

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

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

发布评论

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

评论(1

遥远的她 2024-11-11 23:57:58

如果是权限问题,则包含目录必须可由运行 Web 服务器(大多数情况下为 Apache 或 IIS)的用户写入。

为了确保文件被写入,包含的目录必须可供任何人写入。这可能是一个安全问题,但您随时可以撤消修改。

如果您可以通过 FTP 访问 $path(如我所想),并且可以更改远程目录的权限,请将 $path 目录的权限更改为“全局可写”或 0777 (八进制数字,其中三个7 分别表示文件所有者、所有者所在组和其他所有人的写权限。

如果网络服务器在可以执行此操作的用户下运行,您可以通过 PHP 更改目录权限,使用指令:

chmod(dirname($path),0777);

dirname() 函数返回包含指定路径的目录。

请注意:第二个arg尾随0,这意味着0777是一个八进制数;如果写 777,则表示十进制 777,而八进制 777 则表示十进制 511)。
请查看chmod() 文档以获取有关可能问题的更多信息。

如果你想知道网络服务器运行的用户,你可以使用 phpinfo()< /a>:如果网络服务器是 Apache,您将在“apache2handler”部分找到用户和组,unser“用户/组”。对于 IIS 和其他服务器,您将能够找到(但我不知道到底在哪个组下)。

If it's a permission issue, the containing directory must be writable by the user under which the webserver (Apache or IIS in most cases) is running.

To be sure the file is written, the containing directory must be writable by anyone. It could be a security issue but you can always undo your modifications.

If you have FTP access to $path (as I suppose), and can change permissions of remote directories, change the permissions of the $path directory to "world writable" or 0777 (octal number where the three 7 mean respectively write permission for the owner of the file, the owner's group, and everyone else).

If the webserver runs under a user who can do this, you can change directory permissions via PHP, with the instruction:

chmod(dirname($path),0777);

The dirname() function returns the directory containing the specified path.

Please note: the trailing 0 in the second arg, which means that 0777 is an octal number; if you write 777 it means a decimal 777, while octal 777 is decimal 511).
Please have a look at the chmod() documentation to have additional information on possible issues.

If you want to know the user under which the webserver is running, you may use phpinfo(): if the webserver is Apache, you'll find the user and group in the "apache2handler" section, unser "User/group". For IIS and other servers you'll be able to find out (but I don't know exactly under which group).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文