使用PHP imageCreateFromJpeg复制图像并保留其EXIF/IPTC数据?
我对存储有EXIF/IPTC数据的图像有一些问题使用PHP imageCreateFromJpeg复制图像并保留其EXIF/IPTC数据?,php,gd,exif,iptc,Php,Gd,Exif,Iptc,我对存储有EXIF/IPTC数据的图像有一些问题 当我使用imageCreateFromJpeg(旋转/裁剪等)时,新存储的文件不会保留EXIF/IPTC数据 我当前的代码如下所示: <?php // Before executing - EXIF/IPTC data is there (checked) $image = "/path/to/my/image.jpg"; $source = imagecreatefromjpeg($image); $rotate = imagerotat
当我使用
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.
?>
您没有做错任何事情,但GD根本不处理IPTC数据的Exif,因为它超出了GD的范围
您必须使用第三方库或其他PHP扩展从源图像读取数据,并将其重新插入到由imagejpeg
创建的输出图像中
以下是一些感兴趣的库:,php.net上的一个示例,展示了如何执行所需操作。以下是使用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');
输出图像:
输入图像:
啊。。。因此,换句话说,我必须复制EXIF/IPTC数据并存储在新图像中?正确,在创建图像之前或之后,您必须从源图像中提取元数据。由于您使用的是imagejpeg
来输出最终图像,因此您必须在保存后将其写入最终图像。这是一个大的pitty GD,但它不能这样做,这应该是默认设置!为什么图书馆需要了解exif?为什么仅仅盲目地将特定的字节块复制到新图像是不够的?(我想EXIF是在一个街区内出现的…@Tomas我想到了同样的事情。我惊讶地发现它没有复制它!因为EXIF不能通过php函数嵌入,所以您需要自己编写一个类来将标题放入图像中——这是多么浪费时间啊!