0

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.

Darksymphony
  • 2,155
  • 30
  • 54
  • Does this answer your question? [writing exif data in php](https://stackoverflow.com/questions/5384962/writing-exif-data-in-php) – Markus Zeller Jun 09 '22 at 18:44
  • well, not really. There are options like PEL Library that needs installation from third party Github repo, PHP JPEG Metadata Toolkit - external library, and Exiftool by perl - external executable file. I am looking for a simple native solution, for sure it must work with a few lines of code. If PHP can detect exif, it must also remove it – Darksymphony Jun 09 '22 at 18:51
  • There is one code example answer with a modified class who can write without installing extra tools. – Markus Zeller Jun 10 '22 at 08:19
  • yeah, I wanted a few line solution. If you can do imagecreatefromjpeg, imagerotate, imagedestroy etc. with 1 command with GD, I wonder there is no easy way to remove exif. But I posted an answer, instead of removing exif I just determine the exif orientation and then just with imagerotate I rotate the incorrect image and this is the result I actually wanted to achieve. Easiest and better as removing the exif. – Darksymphony Jun 10 '22 at 08:43

1 Answers1

0

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);
            }
          } 
        } 
      }
Darksymphony
  • 2,155
  • 30
  • 54
  • "_no additional installs and libraries needed_" - yet you check if the function exists? It's also a difference between stripping Exif from a picture (without touching the image payload) and creating a new picture from scratch (thus having entirely a different payload, not just a rotation from the original). – AmigoJack Jun 16 '22 at 11:24
  • yes I just check if exif_read_data exists, I have not installed any library or anything, it is a part of my PHP already on the hosting. So I did not want to install ANYTHING, But this is the best solution for me. – Darksymphony Jun 17 '22 at 14:14
  • There's virtually no chance it cannot exist. So why the check? `imagecreatefromjpeg()` has a higher chance to be missing. – AmigoJack Jun 17 '22 at 15:47
  • well, you are right, actually I can remove that condition – Darksymphony Jun 17 '22 at 19:29