Copy an image and preserve its EXIF/IPTC data with PHP imageCreateFromJpeg?
You aren't doing anything wrong, but GD doesn't deal with Exif of IPTC data at all as its beyond the scope of what GD does.
You will have to use a 3rd party library or other PHP extension to read the data from the source image and re-insert it to the output image created by imagejpeg
.
Here are some libraries of interest: pel (php exif library), an example on php.net showing how to use pel to do what you want, php metadata toolkit, iptcembed() function.
Here is an example of image scaling using gd, and copying Exif and ICC color profile using PEL:
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');
Output images:
Input image: