我在存储EXIF/IPTC数据的图像中遇到了一些问题。
当我使用imageCreateFromJpeg (旋转/裁剪等)时,新存储的文件不会保留EXIF/IPTC数据。
我当前的代码如下所示:
<?php
// Before executing - EXIF/IPTC data is there (checked)
$image = "/path/to/my/image.jpg";
$source = imagecreatefromjpeg($image);
$rotate = imagerotate($source,90,0);
imageJPEG($rotate,$image);
// After executing - EXIF/IPTC data doesn't exist anymore.
?>我做错了什么吗?
发布于 2012-04-17 07:14:23
你没有做错什么,但是GD根本不处理IPTC数据的Exif,因为它超出了GD的范围。
您必须使用第三方库或其他PHP扩展来读取源图像中的数据,并将其重新插入到由imagejpeg创建的输出图像中。
这里有一些你感兴趣的库:pel (php exif library),php.net上的一个例子,展示了如何通过use pel来做你想做的事情,php metadata toolkit,iptcembed() function。
发布于 2019-06-02 03:17:27
下面是使用gd进行图像缩放的示例,以及使用PEL复制Exif和ICC颜色配置文件的示例:
function scaleImage($inputPath, $outputPath, $scale) {
$inputImage = imagecreatefromjpeg($inputPath);
list($width, $height) = getimagesize($inputPath);
$outputImage = imagecreatetruecolor($width * $scale, $height * $scale);
imagecopyresampled($outputImage, $inputImage, 0, 0, 0, 0, $width * $scale, $height * $scale, $width, $height);
imagejpeg($outputImage, $outputPath, 100);
}
function copyMeta($inputPath, $outputPath) {
$inputPel = new \lsolesen\pel\PelJpeg($inputPath);
$outputPel = new \lsolesen\pel\PelJpeg($outputPath);
if ($exif = $inputPel->getExif()) {
$outputPel->setExif($exif);
}
if ($icc = $inputPel->getIcc()) {
$outputPel->setIcc($icc);
}
$outputPel->saveFile($outputPath);
}
copy('https://i.stack.imgur.com/p42W6.jpg', 'input.jpg');
scaleImage('input.jpg', 'without_icc.jpg', 0.2);
scaleImage('input.jpg', 'with_icc.jpg', 0.2);
copyMeta('input.jpg', 'with_icc.jpg');输出图像:


输入图像:

发布于 2021-08-11 19:20:27
@drew010的答案是正确的,因为如果没有外部库或其他程序,这是不可能的。然而,这个答案已经相当陈旧了,现在至少有两种好的方法。@Thiago给出了一个答案,使用PEL。
这里是一个完全不同的脚本,它使用了不同的PERL-script exiftool by Paul Harvey。我更喜欢这个解决方案,因为exiftool有更长的开发和使用历史,有更好的文档记录,对我来说似乎更稳定和可靠。PEL更新了近10年,API不稳定,项目易手的历史,还没有达到1.0版。我试着设置它,但遇到了一些障碍,没有找到克服它们的文档,而设置exiftool是开箱即用的。
安装exiftool,然后在将旧镜像保存到新路径后运行:
exec('exiftool -TagsFromFile /full/path/to/original_image.jpg /full/path/to/newly_saved_image.jpg');您必须让这两个文件都存在才能正常工作;如果像原始代码那样覆盖该文件,则EXIF数据将会丢失。
确保您的php.ini允许exec()调用;出于安全原因,有时不允许这样做。此外,请非常小心,不要在传递给该调用的任何参数中允许任何用户生成的输入,因为这可能允许攻击者以web服务器的权限执行任意命令。如果脚本使用固定的目录路径根据某种公式生成文件名,然后将它们提供给exec调用,则exec调用最安全。
如果你不想全局安装exiftool,你可以用它的完整路径替换exiftool。如果您使用的是SELinux,请确保将exiftool脚本的文件上下文设置为httpd_exec_t,以允许web服务器执行该脚本,并确保整个脚本所在的目录具有上下文httpd_sys_content_t或其他允许web服务器访问的上下文。
https://stackoverflow.com/questions/10182890
复制相似问题