Answers.Body: <p>In yii2 every instance of a module is stored in a public static property of the main class.</p>
  
  <p>If the plugin is called image you can grab it via</p>
  
  <pre><code>Imager::getInstance()
  </code></pre>
  
  <p>This instance contains all the required components. The imager plugin it has 3 <a href="https://github.com/aelvan/Imager-Craft/blob/craft3/src/Imager.php#L108" rel="nofollow noreferrer">components</a></p>
  
  <pre><code>$this-&gt;setComponents([
      'imager' =&gt; ImagerService::class,
      'placeholder' =&gt; PlaceholderService::class,
      'color' =&gt; ImagerColorService::class,
  ]);
  </code></pre>
  
  <p>You can access those via <code>Imager::getInstance()-&gt;imager</code> or <code>placeholder</code> or <code>color</code> in this case the instance of the plugin is also stored in a static <code>$plugin</code> variable. You can call </p>
  
  <pre><code>Imager::$plugin-&gt;imager
  </code></pre>
  
  <p>as well. You can see a working example <a href="https://github.com/aelvan/Imager-Craft/blob/craft3/src/variables/ImagerVariable.php#L307" rel="nofollow noreferrer">here</a></p>
  
  <pre><code>use aelvan\imager\Imager as Plugin;
  .... 
  return Plugin::$plugin-&gt;imager-&gt;isAnimated($asset);
  </code></pre>
  
  <p><a href="https://github.com/aelvan/Imager-Craft/blob/craft3/src/variables/ImagerVariable.php#L35" rel="nofollow noreferrer">This</a> is the function that is being called when you do <code>craft.imager.transform</code> -> the PHP equivalent what you want to do</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-03T09:04:26.290Z
Answers.LastActivityDate: 2018-10-03T09:04:26.290Z
Answers.UserDisplayName: Robin Schambach
Answers.Body: <p>I was able to find a working solution, but really not sure if this is the best way use a plugin in the Element API.</p>
  
  <pre><code>'transformer' =&gt; function(Entry $entry) {
  
      $imager = new aelvan\imager\services\ImagerService();
      $image  = $entry-&gt;image-&gt;one();
  
      $resizedImage = $imager-&gt;transformImage($image, [
          'width' =&gt; 100
      ]);
  
      return [
          'image' =&gt; $resizedImage-&gt;url
      ];
  },
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-02T21:33:43.463Z
Answers.LastActivityDate: 2018-10-02T21:33:43.463Z
Answers.UserDisplayName: Scott Thiessen
Body: <p>I'm trying to use the <a href="https://github.com/aelvan/Imager-Craft" rel="nofollow noreferrer">Imager-Craft</a> plugin to make specific image transformations, but cannot figure out how to access Imager within the <code>element-api.php</code> file.</p>
  
  <p>Imager shows plenty of template examples, but I'm not sure how to translate template code like this into straight Element API php.</p>
  
  <pre><code>{% set image = craft.assets().one() %}
  {% set transformedImage = craft.imager.transformImage(image, { width: 1000 }) %}
  &lt;img src="{{ transformedImage.url }}"&gt;```
  </code></pre>
  
  <p>Specifically, I'm trying to access that <code>craft.imager.transformImage</code> function.</p>
  
  <p>Is this possible?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-02T18:41:45.543Z
Comments.Text: I see people using `craft()->plugins->getPlugin('Imager')`, but this doesn't seem to work in Craft 3 with Element API v2. I there a v2 equivalent?
Comments.UserDisplayName: Scott Thiessen
Tags: <craft3><plugin-elementapi><plugin-imager>
Title: Use Imager plugin with Element API?
ThreadText: # Use Imager plugin with Element API?
  
  (Asked by: username_0 on 2018-10-02)
  
  I'm trying to use the [Imager-Craft](https://github.com/aelvan/Imager-Craft) plugin to make specific image transformations, but cannot figure out how to access Imager within the `element-api.php` file.
  
  Imager shows plenty of template examples, but I'm not sure how to translate template code like this into straight Element API php.
  
  ```
  {% set image = craft.assets().one() %}
  {% set transformedImage = craft.imager.transformImage(image, { width: 1000 }) %}
  <img src="{{ transformedImage.url }}">```
  
  ```
  
  Specifically, I'm trying to access that `craft.imager.transformImage` function.
  
  Is this possible?
  
  **Tags:** craft3, plugin-elementapi, plugin-imager
  
  > **Comments:**
  > **username_0**: I see people using `craft()->plugins->getPlugin('Imager')`, but this doesn't seem to work in Craft 3 with Element API v2. I there a v2 equivalent?  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2018-10-03. Score: 3 | Accepted answer
  
  In yii2 every instance of a module is stored in a public static property of the main class.
  
  If the plugin is called image you can grab it via
  
  ```
  Imager::getInstance()
  
  ```
  
  This instance contains all the required components. The imager plugin it has 3 [components](https://github.com/aelvan/Imager-Craft/blob/craft3/src/Imager.php#L108)
  
  ```
  $this->setComponents([
      'imager' => ImagerService::class,
      'placeholder' => PlaceholderService::class,
      'color' => ImagerColorService::class,
  ]);
  
  ```
  
  You can access those via `Imager::getInstance()->imager` or `placeholder` or `color` in this case the instance of the plugin is also stored in a static `$plugin` variable. You can call
  
  ```
  Imager::$plugin->imager
  
  ```
  
  as well. You can see a working example [here](https://github.com/aelvan/Imager-Craft/blob/craft3/src/variables/ImagerVariable.php#L307)
  
  ```
  use aelvan\imager\Imager as Plugin;
  .... 
  return Plugin::$plugin->imager->isAnimated($asset);
  
  ```
  
  [This](https://github.com/aelvan/Imager-Craft/blob/craft3/src/variables/ImagerVariable.php#L35) is the function that is being called when you do `craft.imager.transform` -> the PHP equivalent what you want to do
  
  
  ---
  
  ### Answer by username_0 on 2018-10-02. Score: 0
  
  I was able to find a working solution, but really not sure if this is the best way use a plugin in the Element API.
  
  ```
  'transformer' => function(Entry $entry) {
  
      $imager = new aelvan\imager\services\ImagerService();
      $image  = $entry->image->one();
  
      $resizedImage = $imager->transformImage($image, [
          'width' => 100
      ]);
  
      return [
          'image' => $resizedImage->url
      ];
  },
  
  ```
  
  
  ---
  
  