Commit dbd7285f authored by fred's avatar fred

switch thumbnail system from homegrown to sorl-thumbnail

parent a6b64632
# initiated from http://djangosnippets.org/snippets/2145/
import os
import re
from PIL import Image
from django.conf import settings
from django.db.models.signals import post_save, pre_delete
from django.template import Library
register = Library()
def thumbnail(image, size='100x100'):
# defining the size
x, y = [int(x) for x in size.split('x')]
# defining the filename and the miniature filename
try:
filehead, filetail = os.path.split(image.path)
except (AttributeError, ValueError):
# return transparent pixel if the image doesn't actually exist
return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII='
basename, format = os.path.splitext(filetail)
if format.lower() not in ('.jpg', '.jpeg'):
format = '.png'
miniature = basename + '__' + size + format
filename = image.path
miniature_filename = os.path.join(filehead, miniature)
filehead, filetail = os.path.split(image.url)
miniature_url = filehead + '/' + miniature
if os.path.exists(miniature_filename) and \
os.path.getmtime(filename) > os.path.getmtime(miniature_filename):
os.unlink(miniature_filename)
# if the image wasn't already resized, resize it
if not os.path.exists(miniature_filename) and os.path.exists(filename):
image = Image.open(filename)
if abs( (1.0*x/y) - (1.0*image.size[0]/image.size[1]) ) > 0.1:
# aspect ratio change, crop the image first
box = [0, 0, image.size[0], int(image.size[0] * (1.0*y/x))]
if box[2] > image.size[0]:
box = [int(t*(1.0*image.size[0]/box[2])) for t in box]
if box[3] > image.size[1]:
box = [int(t*(1.0*image.size[1]/box[3])) for t in box]
if image.size[0] > image.size[1]: # landscape
box[0] = (image.size[0] - box[2]) / 2 # keep the middle
box[2] += box[0]
else:
box[1] = (image.size[1] - box[3]) / 4 # keep mostly the top
box[3] += box[1]
image = image.crop(box)
image = image.resize([x, y], Image.ANTIALIAS)
try:
image.save(miniature_filename, image.format, quality=90, optimize=1)
except:
image.save(miniature_filename, image.format, quality=90)
return miniature_url
register.filter(thumbnail)
def clean_thumb(sender, instance, **kwargs):
if not hasattr(instance, 'image'):
return
if not instance.image:
return
name, ext = os.path.splitext(os.path.basename(instance.image.name))
exp = '^%s__\d+x\d+x[0-1]{1}\%s' % (name, ext)
for file_path in os.listdir(settings.MEDIA_ROOT):
if re.search(exp, file_path):
os.unlink(settings.MEDIA_ROOT + file_path)
post_save.connect(clean_thumb)
pre_delete.connect(clean_thumb)
......@@ -145,6 +145,7 @@ INSTALLED_APPS = (
'panikweb.paniktags',
'mptt',
'compressor',
'sorl.thumbnail',
'jquery',
'ckeditor',
'emissions',
......
{% extends "emissions.html" %}
{% load paniktags thumbnails staticfiles i18n %}
{% load paniktags staticfiles i18n %}
{% block bodyID %}Emissions{% endblock %}
{% block title %}{{ emission.title }}{% endblock %}
......
{% extends "emissions/emission_detail.html" %}
{% load paniktags thumbnails staticfiles soundfiles %}
{% load paniktags staticfiles soundfiles %}
{% block title %}{{ episode.title }} - {{ episode.emission.title }} {% endblock %}
{% block head %}
......
{% extends "news.html" %}
{% load i18n thumbnails paniktags %}
{% load i18n paniktags %}
{% block title %}{{ newsitem.title }}{% endblock %}
{% block toptitle %}
......
{% load thumbnails staticfiles %}
{% load thumbnail staticfiles %}
<div class="emission emission-resume resume cf">
<div class="{% if emission.archived %}archived{% endif %}">
<a class="block" href="{% url 'emission-view' slug=emission.slug %}">
<div class="logo left">
{% if emission.image %}
<img width="60" height="60" src="{{ emission.image|thumbnail:'60x60' }}"/>
{% thumbnail emission.image "60x60" crop="50% 25%" as im %}
<img width="60" height="60" src="{{im.url}}"/>
{% endthumbnail %}
{% else %}
<img class="smooth" style="width:60px;" src="{% static "img/emission.png" %}"/>
{% endif %}
......
{% load thumbnails paniktags %}
{% load thumbnail paniktags %}
<div class="episode detail episode-detail cf">
{% if episode.first_diffusion %}
<div class="dateBloc">
......@@ -45,7 +45,9 @@
{% endif %}
<div class="content userContent marged">
{% if episode.image %}
<img class="logo right button" data-toggle-img="/media/{{episode.image}}" src="{{ episode.image|thumbnail:'640x480' }}"/>
{% thumbnail emission.image "640x480" crop="50% 25%" as im %}
<img class="logo right button" data-toggle-img="/media/{{episode.image}}" src="{{im.url}}"/>
{% endthumbnail %}
{% endif %}
{% if episode.text %}
<article class="text">
......
{% load thumbnails paniktags staticfiles %}
{% load thumbnail paniktags staticfiles %}
<div class="episode inline episode-inline">
<div class="logo">
{% if episode.image %}
<img src="{{ episode.image|thumbnail:'60x60' }}"/>
{% thumbnail episode.image "60x60" crop="50% 25%" as im %}
<img src="{{im.url}}"/>
{% endthumbnail %}
{% elif episode.emission.image %}
<img src="{{ episode.emission.image|thumbnail:'60x60' }}"/>
{% thumbnail episode.emission.image "60x60" crop="50% 25%" as im %}
<img src="{{im.url}}"/>
{% endthumbnail %}
{% else %}
<img class="smooth" style="width:60px;" src="{% static "img/emission.png" %}"/>
{% endif %}
......
{% load thumbnails paniktags staticfiles %}
{% load thumbnail paniktags staticfiles %}
<div class="episode {% if model %}{{ model }}{% else %}resume{% endif %} cf {{ class }}">
{% if date != False %}
<div class="dateBloc">
......@@ -23,15 +23,23 @@
<div class="logo">
<a href="{% url 'episode-view' emission_slug=episode.emission.slug slug=episode.slug %}">
{% if model = "inline" and episode.image %}
<img width="60" height="60" src="{{ episode.image|thumbnail:'60x60' }}"/>
{% thumbnail episode.image "60x60" crop="50% 25%" as im %}
<img width="60" height="60" src="{{im.url}}"/>
{% endthumbnail %}
{% elif model = "inline" and episode.emission.image %}
<img width="60" height="60" src="{{ episode.emission.image|thumbnail:'60x60' }}"/>
{% thumbnail emission.episode.image "60x60" crop="50% 25%" as im %}
<img width="60" height="60" src="{{im.url}}"/>
{% endthumbnail %}
{% elif model = "inline" %}
<img class="smooth" style="width:60px;" src="{% static "img/emission.png" %}"/>
{% elif episode.image %}
<img src="{{ episode.image|thumbnail:'150x150' }}"/>
{% thumbnail episode.image "150x150" crop="50% 25%" as im %}
<img width="150" height="150" src="{{im.url}}"/>
{% endthumbnail %}
{% elif episode.emission.image %}
<img src="{{ episode.emission.image|thumbnail:'150x150' }}"/>
{% thumbnail episode.emission.image "150x150" crop="50% 25%" as im %}
<img width="150" height="150" src="{{im.url}}"/>
{% endthumbnail %}
{% else %}
<img class="smooth" style="width:150px;" src="{% static "img/emission.png" %}"/>
{% endif %}
......
{% load thumbnails %}
{% load thumbnail %}
{% if obj.image %}
<img src="{{ obj.image|thumbnail:'320x240' }}"/>
{% thumbnail obj.image "320x240" crop="50% 25%" as im %}
<img src="{{im.url}}"/>
{% endthumbnail %}
{% endif %}
{% autoescape off %}
......
{% load thumbnails %}
{% load thumbnail %}
{% if obj.episode.image %}
<img src="{{ obj.episode.image|thumbnail:'320x240' }}"/>
{% thumbnail obj.episode.image "320x240" crop="50% 25%" as im %}
<img src="{{im.url}}"/>
{% endthumbnail %}
{% endif %}
{% autoescape off %}
......
{% extends "base.html" %}
{% load thumbnails paniktags staticfiles i18n %}
{% load paniktags staticfiles i18n %}
{% block bodyID %}Home{% endblock %}
{% block title %}{% trans 'Home' %}{% endblock %}
......
{% extends "base.html" %}
{% load paniktags staticfiles thumbnails i18n %}
{% load paniktags staticfiles thumbnail i18n %}
{% block title %}{% trans 'Sounds' %}{% endblock %}
{% block toptitle %}
<h1 class="top"><a href="{% url 'listen' %}">{% trans 'Sounds' %}</a></h1>
......@@ -29,9 +29,13 @@
</div>
<div class="logo">
{% if soundfile.episode.image %}
<img class="normal" src="{{ soundfile.episode.image|thumbnail:'480x320' }}"/>
{% thumbnail soundfile.episode.image "480x320" crop="50% 25%" as im %}
<img class="normal" src="{{im.url}}"/>
{% endthumbnail %}
{% elif soundfile.episode.emission.image %}
<img class="normal" src="{{ soundfile.episode.emission.image|thumbnail:'480x320' }}"/>
{% thumbnail soundfile.episode.emission.image "480x320" crop="50% 25%" as im %}
<img class="normal" src="{{im.url}}"/>
{% endthumbnail %}
{% else %}
<img class="normal" src="{% static "img/sound.png" %}"/>
{% endif %}
......
{% extends "news.html" %}
{% load thumbnails paniktags i18n %}
{% load paniktags i18n %}
{% block title %}{% trans 'News' %} - Archives{% endblock %}
{% block nav %}
<div class="search-filters">
......
{% load thumbnails staticfiles %}
{% load thumbnail staticfiles %}
<div class="content content-inline {% if class != "special" %}inline{% endif %} cf {{ class }}">
<a class="block cf" href="{% url 'newsitem-view' slug=content.slug %}">
{% if class == "special" and content.category %}
......@@ -9,11 +9,17 @@
<div class="logo">
{% if class == "special" and content.image %}
<img class="normal" src="{{ content.image|thumbnail:'480x320' }}"/>
{% thumbnail content.image "480x320" crop="50% 25%" as im %}
<img class="normal" src="{{im.url}}"/>
{% endthumbnail %}
{% elif content.image %}
<img class="left" width="60" height="60" src="{{ content.image|thumbnail:'60x60' }}"/>
{% thumbnail content.image "60x60" crop="50% 25%" as im %}
<img class="left" width="60" height="60" src="{{im.url}}"/>
{% endthumbnail %}
{% elif content.emission.image %}
<img class="left" width="60" height="60" src="{{ content.emission.image|thumbnail:'60x60' }}"/>
{% thumbnail content.emission.image "60x60" crop="50% 25%" as im %}
<img class="left" width="60" height="60" src="{{im.url}}"/>
{% endthumbnail %}
{% else %}
<img class="left" width="60" height="60" src="{% static "img/actu.png" %}"/>
{% endif %}
......
{% load thumbnails i18n paniktags %}
{% load thumbnail i18n paniktags %}
<div id="newsRoll">
<div class="newsRoll center cf">
<ul id="ticker" class="custom bigNews marged" style="height:300px;overflow:hidden;">
......@@ -7,7 +7,9 @@
id="newsRollId-{{ focus.id }}"
class="">
<a
style="max-width:100%;height:300px;background: no-repeat 50% 50% url('{{ focus.content_image|thumbnail:'500x375' }}');"
{% thumbnail focus.content_image "500x375" crop="50% 25%" as im %}
style="max-width:100%;height:300px;background: no-repeat 50% 50% url('{{im.url}}');"
{% endthumbnail %}
class="block news relative"
href="{{ focus|get_focus_url }}">
{% if focus.content_category_title %}
......@@ -26,7 +28,9 @@
{% for focus in news %}
<li style="width:30%;" class="num-{{ forloop.counter }} padded">
<button class="inBlock" data-about="#newsRollId-{{ focus.id }}">
<img style="width:95%;" src="{{ focus.content_image|thumbnail:'160x120' }}" />
{% thumbnail focus.content_image "160x120" crop="50% 25%" as im %}
<img style="width:95%;" src="{{im.url}}" />
{% endthumbnail %}
</button>
</li>
{% endfor %}
......
{% load paniktags thumbnails staticfiles %}
{% load paniktags thumbnail staticfiles %}
<div class="wrapper extra-soundfiles">
<div class="logo">
{% if soundfile.episode.image %}
<img src="{{ soundfile.episode.image|thumbnail:'60x60' }}"/>
{% thumbnail soundfile.episode.image "60x60" crop="50% 25%" as im %}
<img width="60" height="60" src="{{im.url}}"/>
{% endthumbnail %}
{% elif soundfile.episode.emission.image %}
<img src="{{ soundfile.episode.emission.image|thumbnail:'60x60' }}"/>
{% thumbnail soundfile.episode.emission.image "60x60" crop="50% 25%" as im %}
<img width="60" height="60" src="{{im.url}}"/>
{% endthumbnail %}
{% else %}
<img class="smooth" style="width:60px;" src="{% static "img/emission.png" %}"/>
{% endif %}
......
{% load paniktags thumbnails staticfiles %}
{% load paniktags thumbnail staticfiles %}
<div class="wrapper extra-soundfiles">
<div class="logo">
{% if episode.image %}
<img src="{{ episode.image|thumbnail:'60x60' }}"/>
{% thumbnail episode.image "60x60" crop="50% 25%" as im %}
<img width="60" height="60" src="{{im.url}}"/>
{% endthumbnail %}
{% elif episode.emission.image %}
<img src="{{ episode.emission.image|thumbnail:'60x60' }}"/>
{% thumbnail episode.emission.image "60x60" crop="50% 25%" as im %}
<img width="60" height="60" src="{{im.url}}"/>
{% endthumbnail %}
{% else %}
<img class="smooth" style="width:60px;" src="{% static "img/emission.png" %}"/>
{% endif %}
......
{% extends "base.html" %}
{% load thumbnails paniktags %}
{% load thumbnail paniktags %}
{% block bodyID %}Party{% endblock %}
{% block title %}Party{% endblock %}
......@@ -40,7 +40,9 @@ $(function() {
</div>
{% endif %}
<div class="logo">
<img class="normal" src="{{ focus.content_image|thumbnail:'500x375' }}"/>
{% thumbnail focus.content_image "500x375" crop="50% 25%" as im %}
<img width="500" height="375" class="normal" src="{{im.url}}"/>
{% endthumbnail %}
</div>
<div class="title"><div>{{ focus.focus_title }}</div></div>
</a>
......
{% load thumbnails paniktags staticfiles %}
{% load thumbnail paniktags staticfiles %}
<div class="episode soundfile inline cf">
<div class="logo">
{% if soundfile.episode.image %}
<img width="60" height="60" src="{{ soundfile.episode.image|thumbnail:'60x60' }}"/>
{% thumbnail soundfile.episode.image "60x60" crop="50% 25%" as im %}
<img width="60" height="60" src="{{im.url}}"/>
{% endthumbnail %}
{% elif soundfile.episode.emission.image %}
<img width="60" height="60" src="{{ soundfile.episode.emission.image|thumbnail:'60x60' }}"/>
{% thumbnail soundfile.episode.emission.image "60x60" crop="50% 25%" as im %}
<img width="60" height="60" src="{{im.url}}"/>
{% endthumbnail %}
{% else %}
<img width="60" height="60" src="{% static "img/sound.png" %}"/>
{% endif %}
......
......@@ -17,6 +17,7 @@ setup(name='panikweb',
'django-jquery',
'django-registration',
'django-datetime-widget',
'sorl-thumbnail',
],
license='GPL',
classifiers=['Development Status :: 3 - Alpha',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment