Hi. Thank you for responding.
So far, what I did is use wp_insert_attachment, wp_generate_attachment_metadata and wp_update_attachment_metadata to register the images. I created a cron job to read the files from a specific directory.
This approach would still return the execution timeout error so I created a bunch of txt files that have 40 images listed in them. I created these txt files via code and there are a total of almost 800 txt files. Once all the images from the txt files are registered, the txt file is deleted.
I also added a checker if the image is already registered or not because sometimes, for some reason, it only registers the first image in the txt file and it just stops. I didn’t investigate further to know the reason.
It’s not the best solution but atleast it’s working in the background. You might get some kind of idea from it and make it better.