/[theodore]/bunnyblog/gallery.cgi


UCC Code Repository

Contents of /bunnyblog/gallery.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (show annotations) (download)
Wed Jan 30 10:18:20 2008 UTC (12 years, 10 months ago) by theodore
File size: 6627 byte(s)
Updated gallery stuff, added image upload button


1 #!/usr/bin/python
2 import cgi
3 import cgitb; cgitb.enable()
4 import blog
5 import os
6 os.sys.path.insert(0,'./modules')
7 from PIL import Image
8 del os.sys.path[0]
9
10 """
11 BunnyBlog - Image Gallery
12 _________________________
13
14 This file generates all the views for the image gallery and the image manager.
15 _________________________
16 """
17
18 """
19 Here we set up all the variables for generating the page
20 """
21 main = ""
22 title = "<@[email protected]> - "
23 header = "Hoverbox Image Gallery"
24 meta = """
25 <link rel="stylesheet" href='css/hoverbox.css' type="text/css" media="screen, projection" />
26 <!--[if IE]>
27 <link rel="stylesheet" href='css/ie_fixes.css' type="text/css" media="screen, projection" />
28 <![endif]-->
29 """
30
31 uploaddir = "data/images/"
32 thumbsdir = "data/images/thumbs/"
33
34 """
35 Here we retrieve the blog and any arguments
36 Defaults are assigned here
37 """
38 form = cgi.FieldStorage()
39 myblog = blog.Blog()
40 httpstatuscode = "200"
41
42 def massconvert():
43 for file in os.listdir("./data/images/"):
44 ext = file[-4:]
45 if ext != '.jpg' and ext != ".gif" and ext != ".png": #Skip nonimage files
46 continue
47
48 size = 256,256
49 im = Image.open(uploaddir+file).save(thumbsdir+file)
50
51 im = Image.open(thumbsdir+file)
52 im.thumbnail(size)
53 im.save(thumbsdir+file)
54
55 def imagelist():
56 list = ""
57 for file in os.listdir("./data/images/thumbs/"):
58 ext = file[-4:]
59 if ext != '.jpg' and ext != ".gif" and ext != ".png": #Skip nonimage files
60 continue
61 list += '<li><a href="gallery.cgi?image=%s"><img src="data/images/thumbs/%s" alt="description" /><div class="image_container"><img src="data/images/thumbs/%s" alt="description" class="preview" /></div></a></li>' % (file,file, file)
62 return list
63
64 def showgallery():
65 upload_img = "<a href='gallery.cgi?image=upload' style='float:right'>Upload a new picture</a>"
66 return """
67 %s<h2>Click any of these pictures to see the full image, along with its comments.</h2>
68 <div style='clear:right'>
69 <ul class="hoverbox">
70 %s
71 </ul>
72 <p id="hoverbox_footer"><a href="http://validator.w3.org/check/referer">XHTML</a> <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a> <a href="http://www.contentquality.com/
73 mynewtester/cynthia.exe?Url1=http://host.sonspring.com/hoverbox/">508</a> | Hoverbox by <a href="http://sonspring.com/">Nathan Smith</a>. | Read the <a href="http://sonspring.com/
74 journal/hoverbox-image-gallery">Tutorial</a>.</p> </div>
75 """ % (blog.isadmin() and upload_img or "", imagelist())
76
77
78 if form.has_key('image'):
79 image = form['image'].value
80 if image == "gallery":
81 main = showgallery()
82 title = "<@[email protected]> - Image Gallery"
83 elif image == "upload":
84 if blog.isadmin():
85 main = blog.gettemplate("blog/img_upload")
86 else:
87 main = "Please login before trying to upload images"
88 elif image == "massconv":
89 if blog.isadmin():
90 massconvert()
91 else:
92 main = "Please login before trying to upload images"
93 else:
94 if os.path.exists("./data/images/"+image):
95 if blog.isadmin():
96 admin = '<span style="float:right"><a href="<@[email protected]>gallery.cgi?delete=%s">Delete this picture</a></span>' % image
97 else:
98 admin = ""
99 main += '%s<div style="text-align:center;overflow:auto;"><h2>%s</h2><br /><hr /><img src="data/images/%s" alt="description" /></div>' % (admin,image,image)
100 title = "<@[email protected]> - Viewing %s" % image
101 header = "<@[email protected]> - %s" % image
102 else:
103 main = blog.error(blog.cleanse("File - %s does not exist" % image))
104 header = "<@[email protected]> - ERROR finding image"
105 title = header
106 elif form.has_key('upload'):
107 if blog.isadmin():
108 file = form['upload'].file
109 filename = form['upload'].filename
110 ext = filename[-4:]
111 if ext != '.jpg' and ext != ".gif" and ext != ".png": #Skip nonimage files
112 main += blog.error('Upload failed - Disallowed filetype, %s . %s'%(filename,ext))
113 elif os.path.exists("./data/images/"+filename):
114 main = blog.error('Upload failed - File already uploaded')
115 else:
116 f = open(uploaddir+filename,"wb")
117 while 1:
118 chunk = file.read(100000)
119 if not chunk: break
120 f.write (chunk)
121 f.close()
122
123 size = 256,256
124 im = Image.open(uploaddir+filename).save(thumbsdir+filename)
125
126 im = Image.open(thumbsdir+filename)
127 pic_x, pic_y = im.size
128
129 if pic_x/pic_y > 1.5:
130 im = im.crop((.2*pic_x,0,.8*pic_x,pic_y))
131 elif pic_x/pic_y > 2.5:
132 im = im.crop((.3*pic_x,0,.7*pic_x,pic_y))
133 elif pic_x/pic_y > 1.5:
134 im = im.crop((0,.2*pic_y,pic_x,.8*pic_y))
135 elif pic_x/pic_y > 2.5:
136 im = im.crop((0,.3*pic_y,pic_x,.7*pic_y))
137
138 im.thumbnail(size)
139
140 im.save(thumbsdir+filename)
141
142 main = """
143 Upload of %s successful
144
145 Here is the thumbnail of the image:<br />
146
147 <div style="text-align:center;overflow:auto;">
148 <img src = "<@[email protected]>data/images/thumbs/%s" />
149 </div>
150 <br />
151 Here is the original image: <br />
152
153 <div style="text-align:center;overflow:auto;">
154 <img src = "<@[email protected]>data/images/%s" />
155 </div>
156 """ % (filename, filename, filename)
157 else:
158 main = "Please login before trying to upload images"
159 elif form.has_key('delete'):
160 image = form['delete'].value
161 try:
162 os.remove(uploaddir+image)
163 main += "File: " + uploaddir+image + " successfully deleted<br />"
164 except:
165 main += blog.error("Error: " + uploaddir+image + " was not able to be deleted. This file make have protected permissions, or no longer exists.<br />")
166 try:
167 os.remove(thumbsdir+image)
168 main += "File: " + thumbsdir+image + " successfully deleted<br />"
169 except:
170 main += blog.error("Error: " + thumbsdir+image + " was not able to be deleted. This file make have protected permissions, or no longer exists.<br />")
171 else:
172 main = showgallery()
173 title = "<@[email protected]> - Image Gallery"
174
175
176 sidebar = blog.gettemplate("shoutbox", 'data')
177 if blog.isadmin():
178 sidebar += "<hr />" + blog.gettemplate("admin/toolbar")
179 myblog.display(title,header,main, sidebar, meta)

Properties

Name Value
svn:executable

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26