php resizing image on upload rotates the image when i don't want it to

后端 未结 3 681
日久生厌
日久生厌 2020-12-15 01:20

I Have an upload script that resizes the images uploaded, however, some images are being saved as a rotated image when i dont want them to, any way to preserve the original

相关标签:
3条回答
  • 2020-12-15 01:57

    When you take a picture your phone saves any rotation metadata in EXIF headers. When you upload the image to your server, that metadata is still sitting there but it's your job to apply it to the image to rotate it (if you want). In PHP you can use a function called exif_read_data:

    function correctImageOrientation($filename)
    {
        $exif = exif_read_data($filename);
        if ($exif && isset($exif['Orientation'])) {
            $orientation = $exif['Orientation'];
            if ($orientation != 1) {
                $img = imagecreatefromjpeg($filename);
                $deg = 0;
                switch ($orientation) {
                    case 3:
                        $deg = 180;
                        break;
                    case 6:
                        $deg = 270;
                        break;
                    case 8:
                        $deg = 90;
                        break;
                }
                if ($deg) {
                    $img = imagerotate($img, $deg, 0);
                }
                imagejpeg($img, $filename, 95);
            }
        }
    }
    

    To use the function as-is simply call it after you save the file. For more info and an additional PHP solution see the original source.

    0 讨论(0)
  • 2020-12-15 02:00

    The EXIF data is probably being ignored by this basic script.

    Investigate whether your images have embedded rotation information using exif_read_data, and then auto-correct the rotation with a function like this.

    In your case,

    1. take the script I just linked,
    2. paste it above the first line of your code,
    3. change all the $image-> bits to $resizeObj->
      (vim-style grep: %s/image->/resizeObj->/g)
    0 讨论(0)
  • 2020-12-15 02:01

    This is my code, resize image and don't rotates images with exif inside

    PHP must be enabled: extension=php_mbstring.dll extension=php_exif.dll if error, See more: PHP:exif_read_data() not defined

    function CreateThumbnail($pic,$thumb,$thumbwidth, $quality = 100)
    {
    
            $im1=ImageCreateFromJPEG($pic);
    
            //if(function_exists("exif_read_data")){
                    $exif = exif_read_data($pic);
                    if(!empty($exif['Orientation'])) {
                    switch($exif['Orientation']) {
                    case 8:
                        $im1 = imagerotate($im1,90,0);
                        break;
                    case 3:
                        $im1 = imagerotate($im1,180,0);
                        break;
                    case 6:
                        $im1 = imagerotate($im1,-90,0);
                        break;
                    } 
                    }
            //}
            $info = @getimagesize($pic);
    
            $width = $info[0];
    
            $w2=ImageSx($im1);
            $h2=ImageSy($im1);
            $w1 = ($thumbwidth <= $info[0]) ? $thumbwidth : $info[0]  ;
    
            $h1=floor($h2*($w1/$w2));
            $im2=imagecreatetruecolor($w1,$h1);
    
            imagecopyresampled ($im2,$im1,0,0,0,0,$w1,$h1,$w2,$h2); 
            $path=addslashes($thumb);
            ImageJPEG($im2,$path,$quality);
            ImageDestroy($im1);
            ImageDestroy($im2);
    }
    
    0 讨论(0)
提交回复
热议问题