return thumbnails for images instead of full image

This commit is contained in:
j 2013-12-23 12:32:23 +00:00
parent 07363d3ada
commit c248a4c227
2 changed files with 17 additions and 6 deletions

View File

@ -15,6 +15,7 @@ import Image
import ox import ox
from item.models import Item from item.models import Item
from archive.extract import resize_image
import managers import managers
@ -167,18 +168,28 @@ class Document(models.Model):
return True return True
return False return False
def thumbnail(self): def thumbnail(self, size=None):
src = self.file.path
if self.extension == 'pdf': if self.extension == 'pdf':
thumbnail = '%s.jpg' % self.file.path src = '%s.jpg' % src
if size:
size = int(size)
path = src.replace('.jpg', '.%d.jpg'%size)
else: else:
thumbnail = self.file.path path = src
return thumbnail if os.path.exists(src) and not os.path.exists(path):
image_size = max(*Image.open(src).size)
if size > image_size:
path = src
else:
resize_image(src, path, size=size)
return path
def make_thumbnail(self, force=False): def make_thumbnail(self, force=False):
thumb = self.thumbnail() thumb = self.thumbnail()
if not os.path.exists(thumb) or force: if not os.path.exists(thumb) or force:
cmd = ['convert', '%s[0]' % self.file.path, cmd = ['convert', '%s[0]' % self.file.path,
'-background', 'white', '-flatten', '-resize', '256x256', thumb] '-background', 'white', '-flatten', '-resize', '1024x1024', thumb]
p = subprocess.Popen(cmd) p = subprocess.Popen(cmd)
p.wait() p.wait()

View File

@ -227,7 +227,7 @@ def file(request, id, name=None):
def thumbnail(request, id, size=256): def thumbnail(request, id, size=256):
size = int(size) size = int(size)
document = models.Document.get(id) document = models.Document.get(id)
return HttpFileResponse(document.thumbnail()) return HttpFileResponse(document.thumbnail(size))
class ChunkForm(forms.Form): class ChunkForm(forms.Form):
chunk = forms.FileField() chunk = forms.FileField()