Do the error logs show anything?
One possibility is that you are running out of database connections, or else saturating the mysql server to the point of timing out. Add error checking to your DBI calls to see if that is the case. You probably would prefer missing images to page death, so the RaiseError attribute is contraindicated. Just add the same sort of error checking you use for the connection to the statement handle's execute method.
Update: "Premature end..." means the script exited before printing the headers. That means it hit die somewhere, a fatal runtime error, or else didn't compile correctly. Have you tried running from the command line? If it compiles correctly, the problem is likely to be dbi related. Try to get some useful messages into the log with diagnostics, Carp, etc.