Answers.Body: <p>Have a look at the method <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/controllers/AssetTransformsController.php#L77" rel="nofollow">actionSaveTransform</a> in the <a href="http://buildwithcraft.com/classreference/controllers/AssetTransformsController" rel="nofollow">AssetTransformsController</a>.</p>
  
  <p>The code you actually need for your <code>onAfterInstall</code> would be like this:</p>
  
  <pre><code>// Only create if it doesn't exist (by handle)
  if (is_null(craft()-&gt;assetTransforms-&gt;getTransformByHandle($handle))) {
      $transform = new AssetTransformModel();
      $transform-&gt;id = $transformId;
      $transform-&gt;name = $name;
      $transform-&gt;handle = $handle;
      $transform-&gt;width = $width;
      $transform-&gt;height = $height;
      $transform-&gt;mode = $mode;
      $transform-&gt;position = $position;
      $transform-&gt;quality = $quality;
      $transform-&gt;format = $format;
  
      if(craft()-&gt;assetTransforms-&gt;saveTransform($transform)) {
          // Success
      }
      else {
          // Error
      }   
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-29T09:17:26.990Z
Answers.LastActivityDate: 2015-09-29T16:24:29.507Z
Answers.LastEditDate: 2015-09-29T16:24:29.507Z
Answers.UserDisplayName: Jay
Body: <p>As the title suggests, I would like my plugin to generate a new image transform automatically (with a few settings) when the plugin is installed. </p>
  
  <p>The purpose is to avoid my plugin from crashing if a transform hasn't already been generated yet (or doesn't have the same handle names).</p>
  
  <p>Thanks,</p>
  
  <p>Mark</p>
  
Tags: <plugin-development><image-transforms>
Title: Is there a way to create a new image transform 'onAfterInstall()' of a plugin?
ThreadText: # Is there a way to create a new image transform 'onAfterInstall()' of a plugin?
  
  (Asked by: username_0 on 2015-09-29)
  
  As the title suggests, I would like my plugin to generate a new image transform automatically (with a few settings) when the plugin is installed.
  
  The purpose is to avoid my plugin from crashing if a transform hasn't already been generated yet (or doesn't have the same handle names).
  
  Thanks,
  
  Mark
  
  **Tags:** plugin-development, image-transforms
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-09-29. Score: 3 | Accepted answer
  
  Have a look at the method [actionSaveTransform](https://github.com/pixelandtonic/Craft-Release/blob/master/app/controllers/AssetTransformsController.php#L77) in the [AssetTransformsController](http://buildwithcraft.com/classreference/controllers/AssetTransformsController).
  
  The code you actually need for your `onAfterInstall` would be like this:
  
  ```
  // Only create if it doesn't exist (by handle)
  if (is_null(craft()->assetTransforms->getTransformByHandle($handle))) {
      $transform = new AssetTransformModel();
      $transform->id = $transformId;
      $transform->name = $name;
      $transform->handle = $handle;
      $transform->width = $width;
      $transform->height = $height;
      $transform->mode = $mode;
      $transform->position = $position;
      $transform->quality = $quality;
      $transform->format = $format;
  
      if(craft()->assetTransforms->saveTransform($transform)) {
          // Success
      }
      else {
          // Error
      }   
  }
  
  ```
  
  
  ---
  
  