使用PHP编写exif和itpc数据

4

我正在尝试创建一个图像上传系统,以向文件本身添加元数据。

我正在使用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数组标签更改为数据表单中文本字段的变量时,它不会添加信息。
图像将被上传,但作者、版权等标签不在其中。

这可能是一个文件/目录权限问题:您是否使用is_writable($path)或fwrite()的返回值进行了检查? - Paolo Stefan
谢谢你的回复,Paulo... 我已经使用 is_writable 进行了检查,它返回说文件不可写。我该如何解决这个问题? - lovinxlost
1个回答

0
如果这是一个权限问题,则包含该文件的目录必须可写,以供Web服务器(大多数情况下为Apache或IIS)所运行的用户写入。
为确保文件可以被写入,包含该文件的目录必须对所有人都可写。虽然可能存在安全问题,但您可以随时撤销修改。
如果您有FTP访问权限到$path(我假设您有),并且可以更改远程目录的权限,请将$path目录的权限更改为“world writable”或 0777 (每个数字7分别表示文件所有者、所有者组和其他所有人的写入权限)。
如果Web服务器在可以执行此操作的用户下运行,则可以通过PHP更改目录权限,使用以下指令:
chmod(dirname($path),0777);

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

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

如果您想知道 Web 服务器正在运行的用户,请使用phpinfo():如果 Web 服务器是 Apache,则会在“apache2handler”部分找到用户和组,在“User/group”下。对于 IIS 和其他服务器,您将能够找出来(但我不知道确切的组名)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接