Using Media in Drupal is a much better option than using old-school image fields. You can add all types of fields to Media to enrich your images, videos, and other media types with additional information, and you can also reuse Media. All of that cannot be easily done if you are using regular image fields. Let's see how to programmatically create media.
Converting existing image fields to Media image fields is easier than you might think. I just converted all image fields to Media image fields on this website and in this article, I'll show you how I did it.