Making it an influential state in Europe is impossible, as that ship had long sailed after the War of the Spanish Succession or perhaps even earlier. Spain's most preferable path in the nineteenth century is "splendid isolation" from the affairs of Europe". Making it influential in, say South America on the other hand....
In South America, it's very possible that Peru remains Spanish considering that it had to. Argentina would be a great addition, but it was a fairly rebellious colony so that's impossible, and same goes for formerly-New Granada. In Mexico, the
Treaty of Cordoba initially made an independent Mexican Empire in personal union with Spain. Replacing Ferdinand with a sane king would be required for this to be accepted. Of course, it's by no means secure that Spain keeps the colonies for another generation - the Treaty of Cordoba is a great model that should prohibit rebellions if something similar is done to Peru down the line.
Replacing the idiot Ferdinand with some other monarch (by killing Ferdinand) would make Spain more stable, which means that Catalonia's and the Basque regions' burgeoning industrialization (for instance, by the 1790s, they had spinning jennies) will happen earlier. This should serve to make Spain more modern earlier.