You'll have to write the file to disk before you can open it with the GD tools or any other tool.
Just trying to explain why putting images in a database is not a good idea.
MySQL is no PgSQL or Informix.
1.
500k+ images of 20k each give you a database of 500000*20k = 10Gbyte.
As MySQL uses cooked files, you'll need a 10GByte file.
Most filesystems don't allow files larger than 2GB.
2.
If you want to stay 'live' during a backup, you can only backup into SQL statements, which means you have to SELECT just about all your data from the tables to back them up. I don't think 500k+ consecutive SELECT's is going to perform well on a 10GByte table :-)
(Let alone that the entire backup can be held up if you do a slow insert. and I don't think there'll be many fast inserts on a 10Gbyte file)
If I were you I'd most definately store the files in the filesystem.
You can store the location of the file in the database, and let PHP create subdirectories as required. Squid uses the same type of system, creating 16-256 directories with 16-256 subdirectories each.
That gives you a huge space to store the files in.
You won't lose track of which files you have because you'll write a PHP routine to handle all that.