php图像大小调整旋转一些图像

I am using the code below to resize an image in PHP. For some images the saved image is rotated by 90 degrees.

I figure it has something to do with an image which has a width smaller than its height. How would I modify the code to prevent this rotation?

function thumbnail( $img, $source, $dest, $maxw, $maxh ) {  
   
    $jpg = $source.$img;

    if( $jpg ) {
        list( $width, $height  ) = getimagesize( $jpg ); //$type will return the type of the image
        $source = imagecreatefromjpeg( $jpg );

        if( $maxw >= $width && $maxh >= $height ) {
            $ratio = 1;
        }elseif( $width > $height ) {
           
           $ratio = $maxw / $width;
        }else {
            
            $ratio = $maxh / $height; 
        }

        $thumb_width = round( $width * $ratio ); //get the smaller value from cal # floor()
        $thumb_height = round( $height * $ratio );

        $thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
        imagecopyresampled( $thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height );

        $path = $dest.$img;
        imagejpeg( $thumb, $path, 75 );
    }
    imagedestroy( $thumb );
    imagedestroy( $source );
}

</div>