]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/generic/helpers.py
Update for newer django
[sommitrealweird.git] / sommitrealweird / generic / helpers.py
1 from django.conf import settings
2 from PIL import Image
3 import os, sys
4
5 def make_thumbnail(imagefile,width=None,height=None):
6     # first check that the thumbnail image doesn't already exist
7     # and if it does that it's newer than the origional image
8     
9     base_dir = settings.MEDIA_ROOT
10     thumbs_dir = base_dir
11     if not thumbs_dir.endswith("/"):
12         thumbs_dir = "%s/" %(thumbs_dir,)
13         base_dir = "%s/" %(base_dir,)
14     
15     try:
16         thumbs_dir = "%s%s/" %(thumbs_dir, settings.THUMBS_DIRECTORY)
17         thumbsdirectory = settings.THUMBS_DIRECTORY
18     except:
19         thumbs_dir = "%sthumbs/" %(thumbs_dir,)
20         thumbsdirectory = "thumbs"
21
22     if width != None:
23         try:
24             thumb = os.stat("%sw%d/%s" % (thumbs_dir, width, imagefile))
25             pic = os.stat("%s%s" % (base_dir, imagefile))
26             if thumb.st_ctime < pic.st_ctime:
27                 # regenerate the icon
28                 img = Image.open("%s%s" %(base_dir, imagefile))
29                 (curwidth,curheight) = img.size
30
31                 newwidth = width
32                 newheight = int((newwidth * curheight) / curwidth)
33
34                 newimg = img.resize((newwidth, newheight))
35                 newimg.save("%sw%d/%s" %(thumbs_dir, width, imagefile))
36                 
37                 return "%s%s/w%d/%s" %(settings.MEDIA_URL, thumbsdirectory, width, imagefile)
38             else:
39                 # we have a winner!
40                 return "%s%s/w%d/%s" %(settings.MEDIA_URL, thumbsdirectory, width, imagefile)
41         except:
42             # well, there's not one there. feh. sucks.
43             try:
44                 directory_to_create = imagefile[:imagefile.rindex(os.sep)]
45                 img = Image.open("%s%s" %(base_dir, imagefile))
46                 if not os.path.isdir("%sw%d/%s" %(thumbs_dir, width, directory_to_create)):
47                     os.makedirs("%sw%d/%s" %(thumbs_dir, width, directory_to_create))
48                 (curwidth,curheight) = img.size
49
50                 newwidth = width
51                 newheight = int((newwidth * curheight) / curwidth)
52
53                 newimg = img.resize((newwidth, newheight))
54                 newimg.save("%sw%d/%s" %(thumbs_dir, width, imagefile))
55                 
56                 return "%s%s/w%d/%s" %(settings.MEDIA_URL, thumbsdirectory, width, imagefile)
57             except Exception as e:
58                 sys.stderr.write("Got exception: %s" %(e,))
59
60     elif height != None:
61         try:
62             thumb = os.stat("%sh%d/%s" % (thumbs_dir, height, imagefile))
63             pic = os.stat("%s%s" % (base_dir, imagefile))
64             if thumb.st_ctime < pic.st_ctime:
65                 # regenerate the icon
66                 img = Image.open("%s%s" %(base_dir, imagefile))
67                 (curwidth,curheight) = img.size
68
69                 newheight = height
70                 newheight = int((newheight * curwidth) / curheight)
71
72                 newimg = img.resize((newwidth, newheight))
73                 newimg.save("%sh%d/%s" %(thumbs_dir, height, imagefile))
74                 
75                 return "%s%s/h%d/%s" %(settings.MEDIA_URL, thumbsdirectory, height, imagefile)
76             else:
77                 # we have a winner!
78                 return "%s%s/h%d/%s" %(settings.MEDIA_URL, thumbsdirectory, height, imagefile)
79         except:
80             # well, there's not one there. feh. sucks.
81             try:
82                 directory_to_create = imagefile[:imagefile.rindex(os.sep)]
83                 img = Image.open("%s%s" %(base_dir, imagefile))
84                 os.makedirs("%sh%h/%s" %(thumbs_dir, height, directory_to_create))
85                 (curwidth,curheight) = img.size
86
87                 newheight = height
88                 newwidth = int((newheight * curwidth) / curheight)
89
90                 newimg = img.resize((newwidth, newheight))
91                 newimg.save("%sh%d/%s" %(thumbs_dir, height, imagefile))
92                 
93                 return "%s%s/h%d/%s" %(settings.MEDIA_URL, thumbsdirectory, height, imagefile)
94             except:
95                 pass
96
97     # if everything fails, we'll return the origional image URL
98     # this is for simpilicity
99     return "%s%s" %(settings.MEDIA_URL, imagefile)
100