Using Media in Drupal (versions 8+) 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.
Just like Content types, you can have multiple different types of Media. By default when you enable the Media module you get the following types:
use Drupal\media\Entity\Media;
$image_id = 1;
$image_media = Media::create([
'name' => 'My media name',
'bundle' => 'image',
'uid' => 1,
'langcode' => 'en',
'status' => 0,
'field_media_image' => [
'target_id' => $image_id,
'alt' => t('My media alt attribute'),
'title' => t('My media title attribute'),
],
'field_author' => 'admin',
'field_date' => '2025-12-31T23:59:59',
'field_location' => 'tunisia',
]);
$image_media->save();