I've written a script for importing a few thousand items from an old database into Drupal as nodes. There is a function for importing images to the new location (simple file_get_contents, file_put_contents to a temp directory nonsense) and then file_save_data is called to handle the database side of things. What I get back is a stdClass Object for the file, just the kind that Drupal likes. It works perfectly fine except that a few thumbnails are generated as duplicates. For the life of me I can't figure out why. I can run an audit and a repair, but that means I'd have to load up all the nodes I've imported first.

Does anyone have any recommendations on how to handle adding images programmatically or know how I can get these thumbnails to generate properly?