PHP remove orientation exif from image only (without using imagick)
P粉141925181
P粉141925181 2024-03-27 09:27:01
0
1
410

I wrote a script to batch resize uploaded images locally and create thumbnails. The problem is if some images are oriented vertically but after resizing they rotate horizontally.

This is caused by the exif orientation of the image. Is there an easy way to remove orientation exif from an image via PHP? I know Imagick can do it, but I can't/don't want to install it.

Is there any solution without it?

Now I'm solving this problem by opening such an image in an image editor and resaving it without retaining the exif information. Afterwards, when I resize such image in the script, the result is correct.

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

I tried a function that checks the direction 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 the Imagick part with something else without installing any additional libraries, maybe using the already included GD or something.

P粉141925181
P粉141925181

reply all(1)
P粉569205478

Okay, so I decided to rotate the image instead of removing the exif, and it ended up having the same effect. So I check what the exif orientation value is (if any) and then based on that value I just use imagerotate and then resize the image. The result is perfect and no additional installation and libraries are required.

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);
            }
          } 
        } 
      }
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!