使用 GD 将 IPTC 嵌入 PHP 中的图像
我正在尝试拍摄照片并创建缩略图,而不会丢失包含版权信息和其他信息的 IPTC 信息。我使用 GD 编写脚本来调整大小,这当然会导致 IPTC 数据丢失,因为它创建了一个全新的新文件,而不是实际调整原始文件的大小。因此,我的解决方案是从原始图像中提取 IPTC 数据,然后将其嵌入到缩略图中。截至目前,一切运行正常,并且生成了缩略图,只是 IPTC 数据没有被复制。我的代码片段如下。谁能看到我缺少的东西吗?这是基于 PHP 手册中 iptcembed() 的示例。哦,是的,我正在 Zend Framework 中工作,但除了处理配置的注册表之外,这是非常简单的 OOP 代码。谢谢!
public function resizeImage($image, $size)
{
// Get Registry
$galleryConfig = Zend_Registry::get('gallery_config');
$path = APPLICATION_PATH . $galleryConfig->paths->mediaPath . $image->path . '/';
$file = $image->filename . '.' . $image->extension;
$newFilename = $image->filename . '_' . $size . '.' . $image->extension;
// Get Original Size
list($width, $height) = getimagesize($path . $file);
// Check orientation, create scalar
if($width > $height){
$scale = $width / $size;
}else{
$scale = $height / $size;
}
// Set Quality
switch($size){
case ($size <= 200):
$quality = 60;
break;
case ($size > 200 && $size <= 600):
$quality = 80;
break;
case ($size > 600):
$quality = 100;
break;
}
// Recalculate new sizes with default ratio
$new_width = round($width * (1 / $scale));
$new_height = round($height * (1/ $scale));
// Resize Original Image
$imageResized = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg ($path.$file);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Save File
$complete = imagejpeg($imageResized, $path . $newFilename, $quality);
// Copy IPTC info into new file
$imagesize = getImageSize($path . $file, $info);
if(isset($info['APP13'])){
$content = iptcembed($info['APP13'], $path . $newFilename);
$fw = fopen($path . $newFilename , 'wb');
fwrite($fw, $content);
fclose($fw);
}
}
I am trying to take a photograph and create a thumbnail without losing the IPTC info that contains copyright info and other information. I am scripting using GD for the resize which of course results in the loss of the IPTC data since it creates an entire new file, not actually resizing the original. So, my solution was to extract the IPTC data from the original image and then embed it in the thumbnail. As of now, everything runs fine and the thumbnail is generated except the IPTC data is not copied over. My code snippet is below. Can anyone see anyhting I am missing? This is based off of the examples in the PHP manual for iptcembed(). Oh yeah, I am working within the Zend Framework but apart from the Registry to handle the config, this is pretty straightforward OOP code. Thanks!
public function resizeImage($image, $size)
{
// Get Registry
$galleryConfig = Zend_Registry::get('gallery_config');
$path = APPLICATION_PATH . $galleryConfig->paths->mediaPath . $image->path . '/';
$file = $image->filename . '.' . $image->extension;
$newFilename = $image->filename . '_' . $size . '.' . $image->extension;
// Get Original Size
list($width, $height) = getimagesize($path . $file);
// Check orientation, create scalar
if($width > $height){
$scale = $width / $size;
}else{
$scale = $height / $size;
}
// Set Quality
switch($size){
case ($size <= 200):
$quality = 60;
break;
case ($size > 200 && $size <= 600):
$quality = 80;
break;
case ($size > 600):
$quality = 100;
break;
}
// Recalculate new sizes with default ratio
$new_width = round($width * (1 / $scale));
$new_height = round($height * (1/ $scale));
// Resize Original Image
$imageResized = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg ($path.$file);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Save File
$complete = imagejpeg($imageResized, $path . $newFilename, $quality);
// Copy IPTC info into new file
$imagesize = getImageSize($path . $file, $info);
if(isset($info['APP13'])){
$content = iptcembed($info['APP13'], $path . $newFilename);
$fw = fopen($path . $newFilename , 'wb');
fwrite($fw, $content);
fclose($fw);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 iptcembed 确实会自动保存文件,这意味着 php.net 手动示例是错误的:
请参阅 http://php.net/iptcembed
I think iptcembed does automatically save the file, which means that the php.net manual example is wrong:
See http://php.net/iptcembed