Moved the DB query check into file_exists so that we are not constantly hitting the DB when the image already exists. Also added $quality attribute for ImageMagick version.