Image styles within Drupal are one of the things you take for granted, yet are one of Drupal's best tools. A user with CMS access can create an 'Image Style' which can be used on a content type display page or within a 'View', all without any coding.
However, sometime you have to add an image style within a template (Drupal 7) or a module. So how d o we do this?
Normally we will start with something called a 'URI'. Take a look at these two Drupal 7 examples:
$url = file_create_url($row->field_field_image['raw']['uri']);
The above will get the full image path/url from the image URI using file_create_url().
$url = image_style_url('thumbnail', $row->field_field_image['raw']['uri']);
The above will get the full image path/url of the 'thumbnail' image style from the image URI using image_style_url().
print theme('image_style', array('style_name' => 'thumbnail', 'path' => $img['uri'], 'alt' => $img_alt));
The above is another example that I haven't used for a while, so I'm not 100% it works, but thought I'd add it for reference.
Drupal 9 Image Style
Drupal 9's Image Style code is quite simple. The following is in my controller where $productImg['uri'] is the image URI.
$url = ImageStyle::load('thumbnail')->buildUrl($productImg['uri']);
The important thing is to make sure you have the following at the top under the 'namespace' line of your controller: