How to create thumbnails programmatically?

How to create thumbnails programmatically?