php仅删除图像中的方向exif(无图像)

发布于 2025-02-06 02:01:40 字数 814 浏览 2 评论 0原文

我编写了一个脚本,该脚本将上传的图像在本地进行了调整,并创建了缩略图。问题是,如果某些图像垂直方向,但是在调整大小后,它们将水平旋转。

这是由于图像的Exif取向。是否有一种简单的方法可以通过PHP从图像中删除Exif?我知道它可以通过Imagick完成,但是我不能/不想安装它。

那么没有解决方案吗?

目前,我正在通过在图像编辑器中打开这样的图像并在不保留EXIF信息的情况下重新添加它来解决它。之后,当我在脚本中调整此图像大小时,结果是正确的。

因此,在调整大小之前,我只想从PHP脚本中的图像中删除EXIF。

我尝试了检查方向Exif的功能:

function removeExif($filename) {
    if (function_exists('exif_read_data')) {
      $exif = exif_read_data($filename);
      if($exif && isset($exif['Orientation'])) {
        $orientation = $exif['Orientation'];
        if($orientation != 1){

           // $img = new Imagick($filename);
           // $img->stripImage();
           // $img->writeImage($filename);

        } 
      } 
    } 
  }

因此,我只需要用其他东西替换该Imagick零件,而无需安装任何其他库,也许使用已包含的GD或其他东西。

I wrote a script, which resizes my uploaded images in a batch locally and creates also thumbnails. The problem is, if some images are vertically oriented, but after resizing they are rotated horizontally.

This is due to exif orientation of the image. Is there a simple way to remove the orientation exif from image via PHP? I know it can be done by Imagick, but I can't/don't want to install it.

So any solution without it?

For now I am solving it by opening such an image in image editor and re-saving it without keeping exif info. After that when I resize such image in my script, the result is correct.

So I just want to remove exif from the image in my PHP script before resizing it.

I tried a function for checking the orientation exif:

function removeExif($filename) {
    if (function_exists('exif_read_data')) {
      $exif = exif_read_data($filename);
      if($exif && isset($exif['Orientation'])) {
        $orientation = $exif['Orientation'];
        if($orientation != 1){

           // $img = new Imagick($filename);
           // $img->stripImage();
           // $img->writeImage($filename);

        } 
      } 
    } 
  }

So I just need to replace that Imagick part with something else, without installing any additional library, maybe using the already included GD or something.

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

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

发布评论

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

评论(1

月牙弯弯 2025-02-13 02:01:40

好的,所以我决定旋转图像而不是删除exif,结果具有相同的效果。因此,我检查了什么是EXIF方向值(如果有),并且根据我的使用,我只是使用Imagerotate,然后我调整了图像大小。结果是完美的,无需其他安装和库。

   function checkExif($filename) {
        if (function_exists('exif_read_data')) {
          $exif = exif_read_data($filename);
          if($exif && isset($exif['Orientation'])) {
            $orientation = $exif['Orientation'];
            if ($exif['Orientation']==3 OR $exif['Orientation']==6 OR $exif['Orientation']==8) {
                $imageResource = imagecreatefromjpeg($filename); 
                switch ($exif['Orientation']) { 
                case 3:
                $image = imagerotate($imageResource, 180, 0);
                break;
                case 6:
                $image = imagerotate($imageResource, -90, 0);
                break;
                case 8:
                $image = imagerotate($imageResource, 90, 0);
                break;
            } 
            imagejpeg($image, $filename);
            imagedestroy($imageResource);
            imagedestroy($image);
            }
          } 
        } 
      }

Ok, so I decided to rotate the images instead of removing exif, the result has the same effect. So I check what is the exif orientation value (if any), and according that I simply use imagerotate and after that I resize the images. The result is perfect, no additional installs and libraries needed.

   function checkExif($filename) {
        if (function_exists('exif_read_data')) {
          $exif = exif_read_data($filename);
          if($exif && isset($exif['Orientation'])) {
            $orientation = $exif['Orientation'];
            if ($exif['Orientation']==3 OR $exif['Orientation']==6 OR $exif['Orientation']==8) {
                $imageResource = imagecreatefromjpeg($filename); 
                switch ($exif['Orientation']) { 
                case 3:
                $image = imagerotate($imageResource, 180, 0);
                break;
                case 6:
                $image = imagerotate($imageResource, -90, 0);
                break;
                case 8:
                $image = imagerotate($imageResource, 90, 0);
                break;
            } 
            imagejpeg($image, $filename);
            imagedestroy($imageResource);
            imagedestroy($image);
            }
          } 
        } 
      }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文