How to manage correct orientation for upload image

I am using GD library to combine two or more than two images in code igniter. The combining part is working fine. But if I open any image in my system and rotate the image and then try to combine the image then it combines with the rotated way. Means, I need to combine image always with correct orientation. If, any image is not in the correct orientation then the image must be in correct orientation then combine, below is my code.

       private function combine_images($tmp_files, $output_path)
    {
        $merged_image_width = 0;
        $merged_image_height = 0;
        $image_resources = [];

        // Load images, fix orientation, and calculate dimensions for the merged image
        foreach ($tmp_files as $tmp_file) {
            $temp_resource = null;

            // Load the image based on its MIME type
            switch (mime_content_type($tmp_file)) {
                case 'image/jpeg':
                    $temp_resource = imagecreatefromjpeg($tmp_file);
                    break;
                case 'image/png':
                    $temp_resource = imagecreatefrompng($tmp_file);
                    break;
                default:
                    throw new Exception("Unsupported image type: " . mime_content_type($tmp_file));
            }

            // Fix orientation, if needed
            $temp_resource = $this->fix_orientation_and_normalize($tmp_file, $temp_resource);

            // Get dimensions after fixing the orientation
            $width = imagesx($temp_resource);
            $height = imagesy($temp_resource);

            $image_resources[] = $temp_resource;

            $merged_image_width = max($merged_image_width, $width); // Use the widest image
            $merged_image_height += $height; // Total height for vertical stacking
        }

        // Create the merged image
        $merged_image = imagecreatetruecolor($merged_image_width, $merged_image_height);
        $y_offset = 0;

        // Copy each image into the merged image
        foreach ($image_resources as $image_resource) {
            imagecopy($merged_image, $image_resource, 0, $y_offset, 0, 0, imagesx($image_resource), imagesy($image_resource));
            $y_offset += imagesy($image_resource); // Increment vertical offset
            imagedestroy($image_resource); // Free memory for each resource
        }

        // Save the merged image
        $merged_image_path = $output_path . '/merged_image_' . time() . rand(10, 30) . '.jpg';
        imagejpeg($merged_image, $merged_image_path, 100); // Save with maximum quality
        imagedestroy($merged_image); // Free the merged image resource

        return $merged_image_path;
    }

    private function fix_orientation_and_normalize($file_path, $image_resource)
    {
        $exif = @exif_read_data($file_path);

        if (!empty($exif['Orientation'])) {
            switch ($exif['Orientation']) {
                case 3: // Rotate 180 degrees
                    $image_resource = imagerotate($image_resource, 180, 0);
                    break;
                case 6: // Rotate 90 degrees clockwise
                    $image_resource = imagerotate($image_resource, -90, 0);
                    break;
                case 8: // Rotate 90 degrees counter-clockwise
                    $image_resource = imagerotate($image_resource, 90, 0);
                    break;
            }
        }

        // Normalize by resetting EXIF orientation (if you save the image later)
        if (function_exists('imagejpeg')) {
            $temp_path = sys_get_temp_dir() . '/temp_' . uniqid() . '.jpg';
            imagejpeg($image_resource, $temp_path, 100);
            $image_resource = imagecreatefromjpeg($temp_path); // Reload normalized image
            unlink($temp_path); // Clean up temporary file
        }

        return $image_resource;
    }

enter image description here