TV-star comedian Jerry Stiller narrowly escaped a devastating injury yesterday when he cracked his head on the door of a moving cab and fell to the street on the Upper East Side.

But the 78-year-old funnyman, who had just stepped out of the vehicle, came through like a real trouper, "goofing and laughing" after the frightening accident, a source said.

The beloved comic - who has starred in the sitcom hits "Seinfeld" and "King of Queens," as well as a host of movies, including those featuring his son, Ben - never lost consciousness, according to witness Phil Singer.

"His glasses were knocked off and he had a good gash on his head," Singer said.

"He looked shook up.

"I said, 'Jerry, are you OK?' He asked me to get his bag and we talked a little bit. He seemed coherent. He was in control."

According to Singer, Stiller was hurt at around 11:30 a.m. after he stepped from the cab at East 64th Street and Madison Avenue, and as a new rider climbed in from the opposite side.

"The guy on the driver's side closed the door and the cabdriver thought his other passenger was already out and put it in drive and started to go," Singer said.

"Stiller was leaning into the cab to grab his backpack when the driver started to go, and it spun him around. I'm sure the back part of the door whacked him in the head." Stiller tumbled to the ground and blood was flowing from a wound above his ear, Singer said.

The cabby stayed behind, but his new passenger departed.

Stiller asked to borrow Singer's cellphone while Singer ran for a cold, wet towel.

"He thanked me - told me I did a mitzvah," said Singer, using the Hebrew word for a good deed. A police source said, "He was in good spirits, goofing and laughing" before he was put into an ambulance.

Jerry "was pretty badly shaken up," said a woman who witnessed the accident. "There was a stream of blood from the accident to the ambulance. But he was walking on his own."

Another cabby at the scene, Sarabkid Singh, 33, described Stiller as looking "woozy - and he couldn't stand up straight" right after getting smacked.