How to add custom class to an image in Drupal 10

<?php

function hook_preprocess_image(&$variables): void {

    // Check if the image style matches the one you want to target.

   if ($variables['style_name'] === 'image_style_name') { 

          // Add your custom class to the image.

    $variables['image']['attributes']['class'][] = 'custom-class'; 

   }

}