MADRID — It was a humiliating video that fueled outrage on social media. A YouTube prankster filmed himself offering a homeless man in Barcelona an Oreo cookie filled with toothpaste rather than cream.

Now, the prankster, known as ReSet to his followers on YouTube but whose real name is Kanghua Ren, has been handed a 15-month prison sentence and must pay 20,000 euros, or about $22,300, compensation to his victim.

Mr. Ren was found guilty of violating the moral integrity of the homeless man. He is unlikely to serve any time behind bars, however, as Spanish law normally allows sentences under two years for first-time offenders in nonviolent crimes to be suspended.

The Barcelona court, in a verdict published on Friday in the Spanish news media, also ordered Mr. Ren’s YouTube and other social media channels to be shut down for five years.