-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasepicture.py
More file actions
158 lines (137 loc) · 6.39 KB
/
basepicture.py
File metadata and controls
158 lines (137 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -*- coding: utf-8 -*-
###########################################################################
# Copyright (C) 2006 by kosh
# <kosh@kosh.aesaeion.com>
#
# Copyright: See COPYING file that comes with this distribution
#
###########################################################################
#This software is released under GNU public license. See details in the URL:
#http://www.gnu.org/copyleft/gpl.html
from file import File
#For Security control and init
from AccessControl import ClassSecurityInfo
import Globals
import magicfile
from PIL import Image
import utility
class BasePicture(File):
"Image class"
meta_type = "BasePicture"
security = ClassSecurityInfo()
format = 'JPEG'
color = 'RGB'
width = 0
height = 0
scriptChangePath = ''
crop_left = 0
crop_upper = 0
crop_right = 0
crop_lower = 0
imageFormat = {'BMP' : 'Bitmap', 'PNG' : "PNG", "GIF" : "GIF", "JPEG" : "JPEG", "TIFF" : "TIFF"}
formats = sorted(imageFormat.iteritems())
colorFormat = {'L' : 'Black and White', 'RGB' : 'Color', 'RGBA' : 'Color + Transparency'}
colors = sorted(colorFormat.iteritems())
classConfig = {}
classConfig['width'] = {'name':'Suggested Width of Image:', 'type': 'int'}
classConfig['height'] = {'name':'Suggested Height of Image:', 'type': 'int'}
classConfig['crop_left'] = {'name':'Crop position left:', 'type': 'int'}
classConfig['crop_upper'] = {'name':'Crop position up:', 'type': 'int'}
classConfig['crop_right'] = {'name':'Crop position right:', 'type': 'int'}
classConfig['crop_lower'] = {'name':'Crop position lower:', 'type': 'int'}
classConfig['format'] = {'name':'', 'type':'list', 'values': formats}
classConfig['color'] = {'name':'', 'type':'list', 'values': colors}
classConfig['scriptChangePath'] = {'name':'Path to change notification script', 'type': 'path'}
configurable = ('format', 'color',)
security.declarePrivate('makeThumbnail')
def makeThumbnail(self, filename):
"""
Makes a thumbnail image given an image Id when called on a Zope
folder.
The thumbnail is a Zope image object that is a small JPG
representation of the original image. The thumbnail has a
'original_id' property set to the id of the full size image
object.
"""
size=30
data = self.data or self.image
content_type = magicfile.magic(filename)
if content_type.startswith('image'):
image=Image.open(filename)
image=image.convert('RGB')
(x,y) = image.size
if x > size: x = size
if y > size: y = size
image = image.resize((x,y))
thumbnail_file = utility.saveImage(image, 'JPEG')
thumbnail_id = "thumbnail"
if not getattr(self, thumbnail_id):
self.manage_addProduct['Image'].manage_addImage(thumbnail_id, thumbnail_file, thumbnail_id)
else:
self._getOb(thumbnail_id).manage_upload(thumbnail_file)
extensionLookup = {}
extensionLookup['image/jpeg'] = 'jpg'
extensionLookup['image/jpeg; charset=binary'] = 'jpg'
extensionLookup['image/png'] = 'png'
extensionLookup['image/png; charset=binary'] = 'png'
extensionLookup['image/gif'] = 'gif'
extensionLookup['image/gif; charset=binary'] = 'gif'
security.declarePublic("__bobo_traverse__")
def __bobo_traverse__(self, REQUEST, name):
"__bobo_traverse__"
if name.startswith('ver_'):
name = self.REQUEST.TraversalRequestNameStack.pop()
self.REQUEST.RESPONSE.setHeader('Cache-Control', 'public,max-age=315360000')
self.REQUEST.RESPONSE.setHeader('Expires', 'Thu, 01 Dec 2030 12:00:0')
obj = getattr(self, name, None)
if obj is not None:
return obj
elif name == self.getImageNameExtension():
data = self.data or self.image
return data.index_html
security.declareProtected('View', 'index_html')
def index_html(self):
"the base view of this object"
if self.exists():
data = self.data or self.image
return data.index_html(self.REQUEST, self.REQUEST.RESPONSE)
return ''
security.declareProtected('View', 'getImageNameExtension')
def getImageNameExtension(self):
"return the name of this image plus extension"
name = 'data'
if self.exists():
data = self.data or self.image
extension = self.extensionLookup.get(data.content_type, '')
name = name + '.' + extension
return name
security.declareProtected('View', 'absolute_url_path_extension')
def absolute_url_path_extension(self):
"return the url to this image plus the filename and extension"
data = self.data or self.image
return self.absolute_url_path() + '/ver_%s/' % int(data.bobobase_modification_time().timeTime()) + self.getImageNameExtension()
security.declareProtected('View', 'absolute_url_path_extension_thumbnail')
def absolute_url_path_extension_thumbnail(self):
"return the url to this image plus the filename and extension"
data = self.thumbnail
return self.absolute_url_path() + '/ver_%s/thumbnail' % int(data.bobobase_modification_time().timeTime())
security.declareProtected('View', 'absolute_url_extension')
def absolute_url_extension(self):
"return the url to this image plus the filename and extension"
data = self.data or self.image
return self.absolute_url() + '/ver_%s/' % int(data.bobobase_modification_time().timeTime()) + self.getImageNameExtension()
security.declarePrivate('small')
def small(self):
"Draw a small version of the object"
if self.exists() and self.thumbnail is not None:
decode = {'url': self.absolute_url_path_extension_thumbnail(),
'fullUrl':self.absolute_url_path_extension(),
'height':self.thumbnail.height ,
'width':self.thumbnail.width ,
'alt': "",
'name': "thumbnail"}
return '<a href="%(fullUrl)s" rel="lightbox"><img src="%(url)s" width="%(width)s" height="%(height)s" alt="%(alt)s"></a>' % decode
return ""
Globals.InitializeClass(BasePicture)
import register
register.registerClass(BasePicture)