diff options
Diffstat (limited to 'creactiweb/_templates/lib/server/tools/image.py')
-rw-r--r-- | creactiweb/_templates/lib/server/tools/image.py | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/creactiweb/_templates/lib/server/tools/image.py b/creactiweb/_templates/lib/server/tools/image.py new file mode 100644 index 0000000..cffc156 --- /dev/null +++ b/creactiweb/_templates/lib/server/tools/image.py @@ -0,0 +1,110 @@ + +# python import +import logging, os, struct, StringIO + +# atoideweb import +from atoideweb.tools import registry + + +def compute_width_height(width, height, max_width, max_height, use_max=False): + # compute ratio + _ratio_scr = max_width / float(max_height) + _ratio_img = width / float(height) + # .. + if width > max_width\ + or height > max_height: + if _ratio_img > _ratio_scr: + width = max_width + height = int(max_width / _ratio_img) + elif _ratio_img < _ratio_scr: + width = int(max_height * _ratio_img) + height = max_height + else: + width = max_width + height = max_height + # .. + return width, height + # .. + elif use_max is True: + return max_width, max_height + # .. + else: + return width, height + + +def get_image_info(path): + """Tricky method found on Internet that returns the image info from a given + raw image data. + """ + # little check + _info = registry.InfoRegistry().get_info(path) + # already exist + if _info is not None: + return _info + elif os.path.exists(path): + pass + else: + return None, 0, 0 + # read file + _f = open(path) + _data = _f.read() + _f.close() + # + size = len(_data) + height = 0 + width = 0 + content_type = None + + # handle GIFs + if (size >= 10) and _data[:6] in ('GIF87a', 'GIF89a'): + # Check to see if content_type is correct + content_type = 'image/gif' + w, h = struct.unpack("<HH", _data[6:10]) + width = int(w) + height = int(h) + + # See PNG 2. Edition spec (http://www.w3.org/TR/PNG/) + # Bytes 0-7 are below, 4-byte chunk length, then 'IHDR' + # and finally the 4-byte width, height + elif ((size >= 24) and _data.startswith('\211PNG\r\n\032\n') + and (_data[12:16] == 'IHDR')): + content_type = 'image/png' + w, h = struct.unpack(">LL", _data[16:24]) + width = int(w) + height = int(h) + + # Maybe this is for an older PNG version. + elif (size >= 16) and _data.startswith('\211PNG\r\n\032\n'): + # Check to see if we have the right content type + content_type = 'image/png' + w, h = struct.unpack(">LL", _data[8:16]) + width = int(w) + height = int(h) + + # handle JPEGs + elif (size >= 2) and _data.startswith('\377\330'): + content_type = 'image/jpeg' + jpeg = StringIO.StringIO(_data) + jpeg.read(2) + b = jpeg.read(1) + try: + while (b and ord(b) != 0xDA): + while (ord(b) != 0xFF): b = jpeg.read(1) + while (ord(b) == 0xFF): b = jpeg.read(1) + if (ord(b) >= 0xC0 and ord(b) <= 0xC3): + jpeg.read(3) + h, w = struct.unpack(">HH", jpeg.read(4)) + break + else: + jpeg.read(int(struct.unpack(">H", jpeg.read(2))[0])-2) + b = jpeg.read(1) + width = int(w) + height = int(h) + except struct.error: + pass + except ValueError: + pass + # udpate registry + registry.InfoRegistry().set_info(path, (content_type, width, height)) + # .. + return content_type, width, height |