SmileyChris / easy-thumbnails

Easy thumbnails for Django
http://easy-thumbnails.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
1.37k stars 312 forks source link

Support for generating thumbnails from a URL #259

Open siovene opened 10 years ago

siovene commented 10 years ago

Hi, this is a feature request.

I'd like to generate thumbnails starting from a URL, the way sorl-thumbnails does.

Can this be added to easy-thumbnails?

Additionally, I'd like a setting to specify which user-agent to use when fetching the image (presumably with urllib2).

Thanks in advance!

SmileyChris commented 10 years ago

This is a somewhat frequent feature request. Personally, it's not a feature I'm interested in but if someone wants to do the work, I'll let it in.

Make a url_thumbnailer filter

It can be passed a url string or a urllib2 file-like object (so people can do ther own business logic like getting a url with a custom user agent).

Returns a Thumbnailer instance initialized with remote_source=True.

Set the thumbnailer's relative_name to something reasonably unique (let's say url/{sha1hash-of-full-url-salted-with-first-10-chars-of-secret_key}.{extension})

dpflucas commented 10 years ago

This is a feature I'm also interested. I would like to generate thumbnails from URL's, and directly from python code.

Could you please explain what you mean by "Make a url_thumbnailer filter"?

Thanks in advance

SmileyChris commented 10 years ago

There's a thumbnailer template filter a the moment that returns a Thumbnailer instance. This new filter would do the work of converting a url string to a urllib2 file-like object and the initializing the instance as I said.

If all you want to do is do this directly from python, you can already - just pass the file-like object (probably generated with urllib2.urlopen) to get_thumbnailer with some relative_name

dpflucas commented 10 years ago

So, from python code I tried to do this:

img = urllib2.urlopen('http://www.anuncommonfamily.com/wp-content/uploads/2011/08/soccer-ball.jpg')
thumbnailer = get_thumbnailer(img, relative_name='apps/logos/soccer-ball.jpg')
thumb = thumbnailer.get_thumbnail({'size': (100, 100)})

But I get the following exception:

Exception Type: AttributeError
Exception Value:    
addinfourl instance has no attribute 'closed'

Which occurs in generate_source_image from engine.py. Isn't this the way you said it is supposed to work?

Thanks again

SmileyChris commented 10 years ago

@dpflucas you'll see I just committed something to make this work better. For now, you could probably just do img = StringIO(img.read())

dpflucas commented 10 years ago

Thanks, it is now working with the StringIO trick!