有没有办法使用 PHP 从 JPG 中删除 EXIF 数据?我听说过 PEL,但我希望有更简单的方法。我正在上传将在线显示的图像,并希望删除 EXIF 数据。
谢谢!
编辑:我不/不能安装 ImageMagick。
有没有办法使用 PHP 从 JPG 中删除 EXIF 数据?我听说过 PEL,但我希望有更简单的方法。我正在上传将在线显示的图像,并希望删除 EXIF 数据。
谢谢!
编辑:我不/不能安装 ImageMagick。
用于gd
在新的图像中重新创建图像的图形部分,并以另一个名称保存。
使用新的 Imagick 功能。
打开图片:
<?php
$incoming_file = '/Users/John/Desktop/file_loco.jpg';
$img = new Imagick(realpath($incoming_file));
确保在图像中保留任何 ICC 配置文件
$profiles = $img->getImageProfiles("icc", true);
然后剥离图像,如果有的话,把配置文件放回去
$img->stripImage();
if(!empty($profiles)) {
$img->profileImage("icc", $profiles['icc']);
}
来自此 PHP 页面,请参阅页面下方 Max Eremin 的评论。
一种使用 ImageMagick 在 PHP 中快速执行此操作的方法(假设您已安装并启用它)。
<?php
$images = glob('*.jpg');
foreach($images as $image)
{
try
{
$img = new Imagick($image);
$img->stripImage();
$img->writeImage($image);
$img->clear();
$img->destroy();
echo "Removed EXIF data from $image. \n";
} catch(Exception $e) {
echo 'Exception caught: ', $e->getMessage(), "\n";
}
}
?>
我也在寻找解决方案。最后,我使用 PHP 重写了删除所有 Exif 数据的 JPEG。为了我的目的,我不需要任何东西。
此选项有几个优点...
还有一个关于使用 imagecreatefromjpeg 的说明:我试过这个,我的文件变大了。如果您将质量设置为 100,您的文件将更大,因为图像已经过重新采样,然后以无损方式存储。如果你不使用质量 100,你就会失去图像质量。避免重采样的唯一方法是不使用 imagecreatefromjpeg。
这是我的功能...
/**
* Remove EXIF from a JPEG file.
* @param string $old Path to original jpeg file (input).
* @param string $new Path to new jpeg file (output).
*/
function removeExif($old, $new)
{
// Open the input file for binary reading
$f1 = fopen($old, 'rb');
// Open the output file for binary writing
$f2 = fopen($new, 'wb');
// Find EXIF marker
while (($s = fread($f1, 2))) {
$word = unpack('ni', $s)['i'];
if ($word == 0xFFE1) {
// Read length (includes the word used for the length)
$s = fread($f1, 2);
$len = unpack('ni', $s)['i'];
// Skip the EXIF info
fread($f1, $len - 2);
break;
} else {
fwrite($f2, $s, 2);
}
}
// Write the rest of the file
while (($s = fread($f1, 4096))) {
fwrite($f2, $s, strlen($s));
}
fclose($f1);
fclose($f2);
}
代码非常简单。它打开用于读取的输入文件和用于写入的输出文件,然后开始读取输入文件。它从一个数据到另一个数据。一旦到达 EXIF 标记,它就会读取 EXIF 记录的长度并跳过该字节数。然后它继续读取和写入剩余的数据。
以下将删除 jpeg 文件的所有 EXIF 数据。这将复制没有 EXIF 的原始文件并删除旧文件。使用 100 质量不丢失图片的任何质量细节。
$path = "/image.jpg";
$img = imagecreatefromjpeg ($path);
imagejpeg ($img, $path, 100);
imagedestroy ($img);
(可以在此处找到该图的简单近似)
function remove_exif($in, $out)
{
$buffer_len = 4096;
$fd_in = fopen($in, 'rb');
$fd_out = fopen($out, 'wb');
while (($buffer = fread($fd_in, $buffer_len)))
{
// \xFF\xE1\xHH\xLLExif\x00\x00 - Exif
// \xFF\xE1\xHH\xLLhttp:// - XMP
// \xFF\xE2\xHH\xLLICC_PROFILE - ICC
// \xFF\xED\xHH\xLLPhotoshop - PH
while (preg_match('/\xFF[\xE1\xE2\xED\xEE](.)(.)(exif|photoshop|http:|icc_profile|adobe)/si', $buffer, $match, PREG_OFFSET_CAPTURE))
{
echo "found: '{$match[3][0]}' marker\n";
$len = ord($match[1][0]) * 256 + ord($match[2][0]);
echo "length: {$len} bytes\n";
echo "write: {$match[0][1]} bytes to output file\n";
fwrite($fd_out, substr($buffer, 0, $match[0][1]));
$filepos = $match[0][1] + 2 + $len - strlen($buffer);
fseek($fd_in, $filepos, SEEK_CUR);
echo "seek to: ".ftell($fd_in)."\n";
$buffer = fread($fd_in, $buffer_len);
}
echo "write: ".strlen($buffer)." bytes to output file\n";
fwrite($fd_out, $buffer, strlen($buffer));
}
fclose($fd_out);
fclose($fd_in);
}
它是从命令行调用的原型。
这是最简单的方法:
$images = glob($location.'/*.jpg');
foreach($images as $image) {
$img = imagecreatefromjpeg($image);
imagejpeg($img,$image,100);
}
我完全误解了你的问题。
您可以使用一些命令行工具来完成这项工作。或编写您自己的 php 扩展来执行此操作。看看这个有用的库:http ://www.sno.phy.queensu.ca/~phil/exiftool/
干杯,
虚拟机
我不太确定,但如果可以使用GD
o ImageMagick
,我想到的第一件事就是创建一个新图像并将旧图像添加到新图像中。