Writing exif and itpc data in PHP
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' => 'Tes开发者_开发百科t 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.
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).
精彩评论