I edited the entire question to better represent the answer.
I had a for loop which would access and edit image files. Within the loop a file_exists() check was made. If the image file did not exist a blank one would be created.
What was happening was that even after the file was created, the file_exists() would continue returning false and creating new blank files (overwriting the previous operations).
This happens because file_exists() and some other file functions caches the result. To prevent this, use clearstatcache(); before using these functions if you expect the result to have changed and are checking the same file!