Horizontal-branch morphology and dense environments. Hubble Space Telescope observations of globular clusters NGC 2298, 5897, 6535, and 6626