Skip to content

Instantly share code, notes, and snippets.

@zmsmith
Last active December 10, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmsmith/4445844 to your computer and use it in GitHub Desktop.
Save zmsmith/4445844 to your computer and use it in GitHub Desktop.
from django.conf import settings
from libthumbor import CryptoURL
def thumb(url, **kwargs):
'''
returns a thumbor url for 'url' with **kwargs as thumbor options.
Positional arguments:
url -- the location of the original image
Keyword arguments:
For the complete list of thumbor options
https://github.com/globocom/thumbor/wiki/Usage
and the actual implementation for the url generation
https://github.com/heynemann/libthumbor/blob/master/libthumbor/url.py
'''
if settings.THUMBOR_BASE_URL:
# If THUMBOR_BASE_URL is explicity set, use that
base = settings.THUMBOR_BASE_URL
else:
# otherwise assume that thumbor is setup behind the same
# CDN behind the `thumbor` namespace.
scheme, netloc = urlparse.urlsplit(url)[:2]
base = '{}://{}/thumbor'.format(scheme, netloc)
crypto = CryptoURL(key=settings.THUMBOR_KEY)
# just for code clarity
thumbor_kwargs = kwargs
if not 'fit_in' in thumbor_kwargs:
thumbor_kwargs['fit_in'] = True
thumbor_kwargs['image_url'] = url
path = crypto.generate(**thumbor_kwargs)
return u'{}{}'.format(base, path)
upstream thumbor {
server 127.0.0.1:9000;
server 127.0.0.1:9001;
server 127.0.0.1:9002;
server 127.0.0.1:9003;
}
server {
listen 8000;
server_name thumbor.yipit.com;
# merge_slashes needs to be off if the image src comes in with a protocol
merge_slashes off;
location ^~ /thumbor/ {
rewrite /thumbor(/.*) $1 break;
proxy_pass http://thumbor;
}
location / {
proxy_pass http://thumbor;
}
}
[program:thumbor]
command=/var/www/thumbor-env/bin/python /var/www/thumbor-env/bin/thumbor --port=900%(process_num)s --conf=/var/www/thumbor-env/thumbor/thumbor.conf
process_name=thumbor900%(process_num)s
numprocs=4
user=ubuntu
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/thumbor900%(process_num)s.stdout.log
stdout_logfile_backups=3
stderr_logfile=/var/log/supervisor/thumbor900%(process_num)s.stderr.log
stderr_logfile_backups=3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment