使用 PHP 删除 GPS EXIF 数据

发布于 2024-11-25 13:03:22 字数 1824 浏览 2 评论 0原文

今天早上,我被一个运行 Concrete5 网站的客户的电话吵醒,说上传到他们网站的所有图像突然停止工作。经过一两个小时的实验和研究,我意识到了问题的根本原因。几天前,这位客户得到了一部新手机(三星 Epic),它会将 GPS EXIF 数据记录到每张图像中。她使用这部手机作为相机,无论出于何种原因,如果图像中存在任何 GPS 数据,Concrete5 就会爆炸。我怀疑这是因为 GPS 数据存储为数组而不是字符串。

我一直在尝试开发一个通用函数来删除任何 EXIF 数据(如果 GPS EXIF 数据存在)。这是我到目前为止所拥有的(位于 tools/files/importers 中):

$fileExtension = strtolower(substr($_FILES['Filedata']['name'], strrpos($_FILES['Filedata']['name'], '.')));
if ($fileExtension == ".jpg" || $fileExtension == ".jpeg"){
    /* File is a JPEG */
    if (exif_read_data($_FILES['Filedata']['tmp_name'], 'GPS') !== false){
        /* File contains GPS EXIF data */
        if (extension_loaded('magickwand') && function_exists("NewMagickWand")) {
            /* ImageMagick is installed and working */
            $img = new Imagick($_FILES['Filedata']['tmp_name']);
            $img->stripImage();
            $img->writeImage($_FILES['Filedata']['tmp_name']);
            $img->clear();
        } elseif (extension_loaded('gd') && function_exists('gd_info')) {
            /* GD is installed and working */
            $img = imagecreatefromjpeg($_FILES['Filedata']['tmp_name']);
            imagejpeg ($img, $_FILES['Filedata']['tmp_name']);
        } else {
            /* Neither ImageMagick nor GD are installed */
            die('No appropriate image handler to remove EXIF data');    
        }
    }
}

即使使用此代码,当我上传图像时,EXIF 数据仍然保留。我尝试在整个代码中放置 die() 命令,以准确找出代码所采用的路径。在这个特定的服务器上,我永远不会输入 if (extension_loaded('magickwand') && function_exists("NewMagickWand")) 块,但我确实输入 elseif (extension_loaded(' gd') && function_exists('gd_info')) 块。因此,我的 ImageMagick 代码可能没问题,但 GD 似乎没有正确删除 EXIF 数据。

有人有使用 ImageMagick/GD 或 PHP 和 EXIF 数据的经验吗?

This morning I was woken up by a call from a client running a Concrete5 website, saying that any images uploaded to their website suddenly stopped working. After an hour or two of experimentation and study I realized the root cause of the issue. A couple days ago this client got a new cellphone (A Samsung Epic) which records GPS EXIF data to every image. She was using this cellphone as her camera and for whatever reason if there is any GPS data in the image then Concrete5 explodes. I suspect this is because GPS data is stored as an array rather than a string.

I've been trying to work on a generic function to remove any EXIF data if GPS EXIF data exists. Here's what I have so far (this is located within tools/files/importers):

$fileExtension = strtolower(substr($_FILES['Filedata']['name'], strrpos($_FILES['Filedata']['name'], '.')));
if ($fileExtension == ".jpg" || $fileExtension == ".jpeg"){
    /* File is a JPEG */
    if (exif_read_data($_FILES['Filedata']['tmp_name'], 'GPS') !== false){
        /* File contains GPS EXIF data */
        if (extension_loaded('magickwand') && function_exists("NewMagickWand")) {
            /* ImageMagick is installed and working */
            $img = new Imagick($_FILES['Filedata']['tmp_name']);
            $img->stripImage();
            $img->writeImage($_FILES['Filedata']['tmp_name']);
            $img->clear();
        } elseif (extension_loaded('gd') && function_exists('gd_info')) {
            /* GD is installed and working */
            $img = imagecreatefromjpeg($_FILES['Filedata']['tmp_name']);
            imagejpeg ($img, $_FILES['Filedata']['tmp_name']);
        } else {
            /* Neither ImageMagick nor GD are installed */
            die('No appropriate image handler to remove EXIF data');    
        }
    }
}

Even with this code in place, when I upload an image the EXIF data remains. I have tried placing die() commands throughout the code to figure out exactly which path the code is taking. On this particular server I am never entering the if (extension_loaded('magickwand') && function_exists("NewMagickWand")) block, however I do enter the elseif (extension_loaded('gd') && function_exists('gd_info')) block. Therefore my ImageMagick code may be just fine, but it seems that GD is not removing the EXIF data properly.

Does anyone have experience with ImageMagick/GD or with PHP and EXIF data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无尽的现实 2024-12-02 13:03:22

我在本地机器上进行了测试,尝试按照您的方法去除 EXIF 数据,它成功了~它确实添加了以下评论数据:
创建者:gd-jpeg v1.0(使用 IJG JPEG v80),质量 = 100

您使用什么版本的 gd-jpeg?

另外,您是否尝试过var_dump调用imagejpeg?它可能会失败〜我从来没有很幸运地写入任何服务器的 /tmp 文件夹。也许您应该将目标文件设置在您的网络根目录下的某个位置。

I ran a test on my local machine trying to strip out EXIF data following your method, and it worked~ It did add this comment data:
CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), quality = 100

What version of gd-jpeg are you using?

Also, have you tried var_dumping your call to imagejpeg? It might be failing~ I've never had great luck writing to the /tmp folder of any server. Maybe you should set the destination file somewhere under your webroot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文