$image_data per chè lo ridefinisci 4 volte?

Per non ricevere errore basta che verifichi prima l'esistenza del file con file_exists()