What happens when the world’s greatest spin doctor commits malpractice — on himself?

That is the question that now bedevils Barack Obama after what have been, without a doubt, the worst weeks of his presidency.

From the Veterans Administration scandal to the jaw-dropping events surrounding the swap of Sgt. Bowe Bergdahl, the man with the most remarkable intuitive grasp of how to preserve and enhance his own image the world has ever seen has now tarnished it almost beyond recognition.

Who would have expected such a development? From the speech in 2004 that made him a rising star through the campaign in 2008 that made him president, Obama was the most formidable political propagandist of all time.

An unabashed liberal and among the most nakedly partisan politicians in this country’s history, Obama came to fame in ’04 loudly declaiming that we are not red states or blue states, but the United States. He knocked off Hillary Clinton in the Democratic primaries by using the mechanisms his party had put in place to empower its left wing — while claiming to be a more unifying figure than she.

In June ’08, he began speaking at a podium with a decal resembling the presidential seal on it, a brilliant way of planting the suggestion that his election was a foregone conclusion.

He delivered his convention speech that year on a set that suggested he was speaking in the Athens Acropolis, thus hardening in the minds of the chattering classes the idea that he was the greatest orator since Pericles.

Since taking office, he has continued to work assiduously to maintain total control over his own image. He has stiffed the White House press corps, not only by denying reporters access to him but by creating a force field of discipline that keeps his staff from talking about what is going on in any way.

The public gets what little White House information it has from Obama’s own propaganda stream — official tweets, Instagram photos and mini-events carefully manufactured for positive media effect.

Meanwhile, the administration as a whole has declared a cold war against the media it does not control. It tapped phones of Associated Press staff. In court papers, it called James Rosen of Fox News a “co-conspirator” in espionage for seeking out and publishing a leak. It could have, but has not, dropped a Bush-era subpoena of The New York Times’s James Risen — with the end result that Risen will likely go to jail.

The old-line media may scream and squawk — Jill Abramson, late of the Times, said “this is the most secretive White House I have ever been involved in covering, and that includes . . . presidents from President Reagan on up through now, and I was Washington bureau chief of the Times during George W. Bush’s first term.”

Obama was able to do this because he has fed the public-relations beast in other, arguably more effective ways. Historians and his fellow politicians will be studying his mastery of the trade of image creation and enhancement in the age of Twitter and Instagram and Reddit.

While there has been little or no cooperation with reporters, there sure was plenty of cooperation with the production team of the Oscar-bait film “Zero Dark Thirty.”

The president favors jokey late-night interviews with starstruck hosts who thrill to call him “dude” (Jon Stewart in 2010) over substantive discussions with expert journalists. He “slow jams the news” with Jimmy Fallon. He has become a national entertainer.

Previous presidents could only have dreamt of such uncritical treatment by pop culture; but then, of course, most previous presidents believed the presidency was too serious and august a position, and the power it wields far too formidable, to participate in lowering its exalted standing in the way Obama has.

Obama therefore had reason to believe his stage management of the swap of Bowe Bergdahl for five Taliban commanders would play to his advantage.

He made a conscious decision to play up the emotion — having Bergdahl’s parents standing beside him in the Rose Garden, proudly declaiming his obligation as commander in chief not to leave a man behind.

He knew a firestorm would inevitably erupt over the release of the Taliban 5 from Gitmo, but as long as it was a political and policy firestorm, he could insulate himself from it by invoking the greatest of all pop-culture fantasies: a happy ending.

The importance of the storyline was so absolute that his national-security adviser, Susan Rice, found it necessary to go on a Sunday chat show and say Bowe Bergdahl had “served with distinction and honor.”

She knew that was not a true thing to say about Bergdahl’s service, but she had to say it because the pop-culture plotline called for it.

She also said reporters Bergdahl’s release had been urgent because he was near death — a detail that offered even greater emotional justification.

Alas, this proved not to be true either; at a closed-door meeting on Capitol Hill on Wednesday, the director of national intelligence, James Clapper, said the administration had no intelligence to suggest this.

The bottom line is that the president settled on a controversial, high- risk strategy here in a difficult and problematic manner — and then sought to use his mastery of pop culture to change the story to a more palatable one. But some stories just can’t be gussied up.

More important for the president’s future fortunes is this lesson: You can only spin for so long before you start spinning yourself. Spin and spin and spin and soon you have a whirlwind to reap.