Prevents crash on WPImageOptimizer

Adds a few extra checks to make sure we don't release NULL objects or
return invalid data.

Fixes #2713