<p>The <a href="https://craftcms.com/classreference/services/ImagesService" rel="nofollow">ImagesService</a>'s <code>loadImage()</code> method is designed to read image files off of the local file system only, not URLs – hence the appropriate error message; Craft treats your URL as a local filepath and that file obviously doesn't exist.</p>

<p>To apply transforms to an image on a remote host, you'll have to jump through a few hoops. First, you'll need to download the image from the remote URL to your local server – the easiest way to do this is using PHP´s <a href="http://php.net/manual/en/function.file-get-contents.php" rel="nofollow"><code>file_get_contents</code></a> method – before creating and storing the image file (the best way to do that is by using the <a href="https://craftcms.com/classreference/helpers/IOHelper" rel="nofollow">IOHelper</a> class' <code>createFile</code> and <code>storeFile</code> methods). At that point you'll have an image file on the local filesystem and a valid filepath to use for the <code>ImageService</code> in order to apply your transform.</p>

<p>Along the way, you'll need to take care to retain the remote image file's extension (i.e. jpg, gif, png or the like), construct valid filepaths for the downloaded image and the transformed image(s), handle caching for the external file (avoiding a situation where you'll have the server downloading the remote image on every pageload), testing for existing transforms (to avoid needlessly re-transforming the image on every pageload) etc.</p>

<p>My advice: Unless you absolutely need to (or want to) write your own plugin, you should use the excellent <a href="https://github.com/aelvan/Imager-Craft" rel="nofollow">Imager plugin</a> – which is able to transform images from external hosts (i.e. URLs) out of the box. Here's how you could use Imager in your template:</p>

<pre><code>{% set thumb = {
    mode: 'crop',
    width: 100,
    height: 100,
    quality: 75,
    position: 'top-center'
} %}
{% set image = craft.imager.transformImage('http://thoughtfulweb.com/images/_halfPageStandard/Thoughtful-Indiana-Jones.jpg', thumb) %}
{% if image %}
&lt;img src="{{ image.url }}" /&gt;
{% endif %}
</code></pre>

CC BY-SA 3.0
2016-01-11T20:10:19.463Z
Thanks for answering this question so comprehensively. I had a sneaky suspicion that there were more hoops I needed to jump through to get this working. 

I had discovered the Imager plugin shortly after posting this and agree that this would be the best option. Thanks for your help!
Simon Leadbetter
CC BY-SA 3.0
2016-01-11T17:38:17.770Z
2016-01-11T18:45:10.550Z
2016-01-11T18:45:10.550Z
Mats Mikkel Rummelhoff
<p>In reference to this post <a href="https://craftcms.stackexchange.com/questions/627/can-i-apply-an-image-transformation-to-an-arbitrary-image-non-assets-field">Can I apply an image transformation to an arbitrary image (non-Assets field)?</a> does anyone have an example of the markup used to create the plugin to do this? </p>

<p>I need to create a transformed thumbnail from a Vimeo/YouTube URL, but I am having difficulty creating the plugin.</p>

<p>I have created the following function:</p>

<pre><code>public function videothumb($imageSource)
{

  $image = craft()-&gt;images-&gt;loadImage($imageSource);
  $image-&gt;setQuality($quality);
  $image-&gt;resize($transform-&gt;width, $transform-&gt;height);
  $image-&gt;saveAs($targetFile);

}
</code></pre>

<p>And I am calling it with:</p>

<pre><code>{% set thumb = {
    mode: 'crop',
    width: 100,
    height: 100,
    quality: 75,
    position: 'top-center'
} %}
{{ craft.thoughtful.videothumb('http://thoughtfulweb.com/images/_halfPageStandard/Thoughtful-Indiana-Jones.jpg', thumb) }}
</code></pre>

<p>I keep getting an error saying:</p>

<p><em>No file exists at the path “<a href="http://thoughtfulweb.com/images/_halfPageStandard/Thoughtful-Indiana-Jones.jpg" rel="nofollow noreferrer">http://thoughtfulweb.com/images/_halfPageStandard/Thoughtful-Indiana-Jones.jpg</a>”</em></p>

<p>Clearly I am missing something. Any pointers would be greatly received.</p>

<p>PS. I know the file path does not point to a video thumbnail. It’s just for reference.</p>

<image-transforms>
Plugin to create an image transformation for an external image (non-Assets field)
# Plugin to create an image transformation for an external image (non-Assets field)

(Asked by: username_0 on 2016-01-11)

In reference to this post Can I apply an image transformation to an arbitrary image (non-Assets field)? does anyone have an example of the markup used to create the plugin to do this? 
I need to create a transformed thumbnail from a Vimeo/YouTube URL, but I am having difficulty creating the plugin.
I have created the following function:
public function videothumb($imageSource)
{

  $image = craft()->images->loadImage($imageSource);
  $image->setQuality($quality);
  $image->resize($transform->width, $transform->height);
  $image->saveAs($targetFile);

}

And I am calling it with:
{% set thumb = {
    mode: 'crop',
    width: 100,
    height: 100,
    quality: 75,
    position: 'top-center'
} %}
{{ craft.thoughtful.videothumb('http://thoughtfulweb.com/images/_halfPageStandard/Thoughtful-Indiana-Jones.jpg', thumb) }}

I keep getting an error saying:
No file exists at the path “http://thoughtfulweb.com/images/_halfPageStandard/Thoughtful-Indiana-Jones.jpg”
Clearly I am missing something. Any pointers would be greatly received.
PS. I know the file path does not point to a video thumbnail. It’s just for reference.

**Tags:** image-transforms


---

## 1 answers

### Answer by username_1 on 2016-01-11. Score: 4 | Accepted answer

The ImagesService's loadImage() method is designed to read image files off of the local file system only, not URLs – hence the appropriate error message; Craft treats your URL as a local filepath and that file obviously doesn't exist.
To apply transforms to an image on a remote host, you'll have to jump through a few hoops. First, you'll need to download the image from the remote URL to your local server – the easiest way to do this is using PHP´s file_get_contents method – before creating and storing the image file (the best way to do that is by using the IOHelper class' createFile and storeFile methods). At that point you'll have an image file on the local filesystem and a valid filepath to use for the ImageService in order to apply your transform.
Along the way, you'll need to take care to retain the remote image file's extension (i.e. jpg, gif, png or the like), construct valid filepaths for the downloaded image and the transformed image(s), handle caching for the external file (avoiding a situation where you'll have the server downloading the remote image on every pageload), testing for existing transforms (to avoid needlessly re-transforming the image on every pageload) etc.
My advice: Unless you absolutely need to (or want to) write your own plugin, you should use the excellent Imager plugin – which is able to transform images from external hosts (i.e. URLs) out of the box. Here's how you could use Imager in your template:
{% set thumb = {
    mode: 'crop',
    width: 100,
    height: 100,
    quality: 75,
    position: 'top-center'
} %}
{% set image = craft.imager.transformImage('http://thoughtfulweb.com/images/_halfPageStandard/Thoughtful-Indiana-Jones.jpg', thumb) %}
{% if image %}

{% endif %}

> **Comments:**
> **username_0**: Thanks for answering this question so comprehensively. I had a sneaky suspicion that there were more hoops I needed to jump through to get this working.   I had discovered the Imager plugin shortly after posting this and agree that this would be the best option. Thanks for your help!  

---

