用PHP编写exif和itpc数据

时间:2011-05-03 15:16:57

标签: php exif iptc

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

我正在使用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数组标记更改为数据形式的文本字段中的变量时,它不会添加信息。

图片将被上传,但作者,版权的标签不在那里。

1 个答案:

答案 0 :(得分:0)

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

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

如果您有FTP访问$ path(如我所想),并且可以更改远程目录的权限,请将$ path目录的权限更改为“world writable”或0777(八位数字所在的三位数) 7分别表示文件所有者,所有者组以及其他所有人的写入权限。

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

chmod(dirname($path),0777);

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

请注意:第二个arg中的尾随0,表示0777是八进制数;如果你写777它意味着一个小数777,而八进制777是十进制511)。 请查看chmod() documentation以获取有关可能问题的其他信息。

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