How can I use another plugin in PHP? I'm running the ElementAPI but would like to take advantage of some functionality provided by another plugin, to return these results in the API. Is this possible? Should all functions available in the main templates be available to the ElementAPI?

To be specific about what I'm trying to achieve--I'm trying to access the Imager plugin to handle optimised transformations of an uploaded image. Here's how the code looks in a regular Twig template

{% set transformedImage = craft.imager.transformImage(image, { width: 200 }) %}
&lt;img src="{{ transformedImage.url }}"&gt;


And my attempt to do this inside elementapi.php is:

$sizerOptions = array( 'width' =&gt; '202' )
transformedImage = craft()-&gt;imager-&gt;transformImage( $asset-&gt;url, $sizerOptions );


My PHP is not brilliant so I may just be doing something silly here, but wondering if it's my fault, or if I'm attempting to do something impossible!
Your approach is technically correct – the problem is that craft()-&gt;imager-&gt;transformImage() requires two additional parameters; $transformDefaults and $configOverrides.

You can read more about these parameters and the transformImage method in the plugin's official docs, but it's not super important. The two parameters can actually be null, which means the following should work:

$sizerOptions = array('width' =&gt; 202);
$transformedImage = craft()-&gt;imager-&gt;transformImage($asset, $sizerOptions, null, null);


Note that while Imager does work with URLs, if you do have an AssetFileModel (i.e. $asset) it's actually better to feed that to transformImage() because then Imager will handle automatic cache breaking etc. for you (e.g. if you replace the image in Craft later on) – so my advice is to use transformImage($asset), not transformImage($asset-&gt;url), whenever possible.

Finally, in case you're wondering why the below works for templates:

{% set transformedImage = craft.imager.transformImage(image, transform) %}


...when you need the additional two parameters when you do the same thing in PHP, here's a quick explanation of what's going on:

Plugins in Craft are made up of several different components – among them, Services and Variables.

You can think of Services and Variables as two different APIs for a plugin – one is only made available to PHP (Services) and the other is only made available to Twig/templates (Variables).

Craft makes plugin Services available in PHP by instantiating them and adding them to the global craft() object in the format craft()-&gt;serviceName. For many plugins, there'll only be a single service, usually named after the plugin itself (e.g. ImagerService, which is mapped to craft()-&gt;imager). There can be several though, e.g. Imager_AnotherService, which would be mapped to craft()-&gt;imager_another.

Unlike Services, plugins can only have a single Variables class, which is always named after the plugin (e.g. PluginNameVariable) and mapped to craft.pluginName. In Imager's case, this means that the ImagerVariable is mapped to craft.imager in Twig.

Whenever you call craft.imager.transformImage() in a template, you're actually interacting with a Variable class – in this case, ImagerVariable. When you call craft()-&gt;imager-&gt;transformImage() in PHP, you're interacting with the ImagerService class.

The fact that the ImagerVariable and ImagerService both have a method called transformImage is a common design pattern in Craft, where variable classes are used to "wrap" Services, in order to expose the latter for use in templates. Often (and this is true for Imager, as well), the Variable methods do little more than to call their namesake Service methods. Using the same names for Variable and Service methods isn't a requirement, though – just a "best practice" to ensure consistent plugin APIs for templates and PHP. Imager's author could've opted to call the Variable method transform, doTransform, transformy or basically anything else.

Finally – as I mentioned, Imager's Variable method transformImage basically just wraps the ImagerService-&gt;transformImage method – but with one important difference: The Variable method makes the two additional parameters ($transformDefaults and $configOverrides) optional. This is why you don't have to declare them when you use Imager in a template, but you do have to define them when you use it in PHP.

A fun trick is that while Craft doesn't expose Template Variable classes to PHP out of the box, there's nothing preventing you from using them "manually". For example, say you prefer the Imager's Template Variable API over its Service API; here's how you can use the Variable methods wherever you want in your PHP:

Craft::import('plugins.imager.variables.ImagerVariable');
$imager = new ImagerVariable();

$transformedImage = $imager-&gt;transformImage($asset, $sizerOptions);