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

后端 未结 3 682
日久生厌
日久生厌 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 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);
    }
    

提交回复
热议问题