Drupal 8 add image style programmatically

Hi, during port to Drupal 8 one of my modules I had to create own image style in code. How I did that?

1) Follow image style page and create image style you need;

2) Follow conf sync page yoursite.com/admin/config/development/configuration/single/export, selecting "Image style" in "Configuration type" select list. Then select previously created image style in "Configuration name" select list. You will get all settings of Image style in "Here is your configuration:" text area. You should copy image style configuration starting from "name" key.

3) Create own module. Create followinf folder structure inside you module config/install/image.style.image_style_machine_name.yml. Paste content of previous step into this file.

 

name: image_style_machine_name
label: 'My style name (200xANY)'
effects:
  ddd73aa7-4bd6-4c85-b600-bdf2b1628d77:
    uuid: ddd73aa7-4bd6-4c85-b600-bdf2b1628d1d
    id: image_scale
    weight: 0
    data:
      width: 200
      height: null
      upscale: false

Then if we will disable and uninstall this module image_style will still be available on the site and when we will try to enable module once more we will get the error.
I solved this issue with creating mymodule.install file and implementing following hook_uninstall():

use Drupal\image\Entity\ImageStyle;

/**
 * @file
 * Mymodule install file.
 */

/**
 * Implements hook_uninstall().
 */
function mymodule_uninstall() {
  // Remove image_style_machine_name image style to prevent error on next module turn on.
  $image_style = ImageStyle::load('image_style_machine_name');
  $image_style->delete();
}

All of above tested with drupal 8.1.5.

Thanks