2
  {% set response = craft.twitter.get('statuses/user_timeline', { tweet_mode:'extended', count: 20 }) %}
            {% if response.success %}
            {% for tweet in response.data %}
            {% if tweet.extended_entities.media is defined %}
            {% for item in tweet.extended_entities.media if item.type == 'photo' %}

            <div><img src="{{ item.media_url.getUrl('footerThumbs') }}" alt=""/></div>
            {% endfor %}
            {% endif %}
            {% endfor %}
            {% endif %}

This throws a Runtime error: Impossible to invoke a method ("getUrl") on a string variable ("http://pbs.twimg.com/media/DlAIWwkUcAAY_3u.jpg").

What other method is possible?

Using the Imager plugin: {% set externalImage = 'http://pbs.twimg.com/media/DlAIWwkUcAAY_3u.jpg' %}

{% set transformedImages = craft.imager.transformImage(externalImage, [
                { width: 200, height: 200 }])
              %}
         {% set response = craft.twitter.get('statuses/user_timeline', { tweet_mode:'extended', count: 20 }) %}
            {% if response.success %}
            {% for tweet in response.data %}
            {% if tweet.extended_entities.media is defined %}
            {% for item in tweet.extended_entities.media if item.type == 'photo' %}

            <div><img src="{{ externalImage }}" alt=""/></div>
            {% endfor %}
            {% endif %}
            {% endfor %}
            {% endif %}}

2 Answers 2

3

Craft's built-in transform functionality doesn't support transforming external URLs. You could use my Imager plugin to do this instead.

EDIT:

Based on your example, you'd use Imager something like this:

{% set response = craft.twitter.get('statuses/user_timeline', { tweet_mode:'extended', count: 20 }) %}
{% if response.success %}
    {% for tweet in response.data %}
        {% if tweet.extended_entities.media is defined %}
            {% for item in tweet.extended_entities.media if item.type == 'photo' %}
                {% set transformedImage = craft.imager.transformImage(item.media_url, { width: 200, height: 200 }) %}
                <div><img src="{{ transformedImage.url }}" alt=""/></div>
            {% endfor %}
        {% endif %}
    {% endfor %}
{% endif %}

So, the essential part is that you need to pick up the url to each item in the loop (item.media_url, if that's what the twitter plugin returns), and do the transform inside the for loop.

3
  • I knew there was a way of achieving this. I've even used this before, but couldn't think of what it was called. 👍🏼Gonna add this to my list of regular use plugins.
    – Jay
    Aug 20, 2018 at 10:19
  • @André, I did try implementing your plugin Imager, which works great for a single image (knowing the absolute path) but how do you find the path for multiple tweet (image) entries? See added code in the original question.
    – Nutmeg
    Aug 20, 2018 at 20:41
  • 1
    @Nutmeg I've updated my reply. Aug 22, 2018 at 11:30
0

As far as I'm aware it's not possible, because the Twitter feed APIs are fed a URL path to the asset on Twitter's server. So Craft never handles the actual asset to be able to crop it.

I'm sure the P&T guys or Andrew that usually respond to questions will have a suggestion, but I don't think it's really possible.

Best you can do it crop images in CSS using object-fit: cover in a div the size you want the image to be.

EDIT:

But the actual error you're getting is because item.media_urlis a string for 'http://pbs.twimg.com/media/DlAIWwkUcAAY_3u.jpg', so it doesn't have a url to access with .getUrl.

I'm unsure if it'll work, but you could try item.media_url('footerThumbs')

Cheers

2
  • 1
    thank you, but I did try that. The solution will be in the Imager plugin, but the question is how....
    – Nutmeg
    Aug 20, 2018 at 21:15
  • I haven't used it for a long time unfortunately. Looks like Andre above is helping you. Good luck!
    – Jay
    Aug 20, 2018 at 21:28

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.