/[theodore]/bunnyblog/gallery.cgi


UCC Code Repository

Contents of /bunnyblog/gallery.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 43 - (show annotations) (download)
Fri Feb 22 07:16:31 2008 UTC (12 years, 9 months ago) by theodore
File size: 6848 byte(s)
Lots o' edits


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

Properties

Name Value
svn:executable

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26