2

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.

4

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 %}
<img src="{{ image.url }}" />
{% endif %}
1
  • 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 Jan 11 '16 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.