def resizeExistingImage(self): "resize existing images" if self.exists(): filename, remove_after = utility.createTempFile(self.data.data) content_type = magicfile.magic(filename) if content_type.startswith('image'): if self.getConfig('resizeOnUpload'): self.resizeImage(filename) #have to redo the content_type after we modify the image in case it has changed content_type = magicfile.magic(filename) #not correct self.makeThumbnail(filename) self.setFileSize() self.storeContentType(content_type) self.updateImageSrcCache() utility.removeTempFile(filename, remove_after)
def before_manage_edit(self, dict): "This is the object specific edit stuff" if dict.pop('_del',None) == 'Y': self.clear() if dict.get('data', None): #don't process if data does not have anything in it filename, remove_after = utility.createTempFile(dict['data']) content_type = magicfile.magic(filename) if content_type.startswith('image'): if not self.data: self.manage_addProduct['Image'].manage_addImage('data', '', 'data') if self.getConfig('resizeOnUpload'): self.resizeImage(filename) elif self.getConfig('resaveOnUpload'): temp_file,x,y = utility.resaveExistingImage(filename) if temp_file is not None: self.data.manage_upload(temp_file) temp_file.close() self.data.width = x self.data.height = y else: self.data.manage_upload(open(filename, 'rb')) else: self.data.manage_upload(open(filename, 'rb')) self.updateParentPaths(set(['data'])) self.makeThumbnail(filename) self.setFileSize() self.storeContentType(content_type) utility.removeTempFile(filename, remove_after) try: del dict['data'] except KeyError: pass
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)
def generateImage(self, pic): "generate an image and thumbnail based on this data" filename, remove_after = utility.createTempFile(pic.data.data) content_type = magicfile.magic(filename) if content_type.startswith('image'): self.genImage(filename) self.updateImageSrcCache(pic) self.makeThumbnail(filename) utility.removeTempFile(filename, remove_after)
def resaveExistingImage(self): "reisze existing images" if self.exists(): filename, remove_after = utility.createTempFile(self.image.data) content_type = magicfile.magic(filename) if content_type.startswith('image'): temp_file, x, y = utility.resaveExistingImage(filename) beforeSize = utility.fileSizeToInt(self.getFileSize()) if temp_file is not None and os.stat(temp_file.name)[stat.ST_SIZE] < beforeSize: self.image.manage_upload(temp_file) self.image.width = x self.image.height = y #have to redo the content_type after we modify the image in case it has changed content_type = magicfile.magic(temp_file.name) temp_file.close() self.setFileSize() self.image.content_type = content_type utility.removeTempFile(filename, remove_after)
def updateFileContentType(self): "Update the content type of the installed file" if self.exists(): if self.hasObject('data'): file_obj = aq_base(self.data) if hasattr(file_obj, 'data'): filename, remove_after = utility.createTempFile(file_obj.data) file_obj.content_type = magicfile.magic(filename) utility.removeTempFile(filename, remove_after)
def resaveExistingImage(self): "reisze existing images" if self.exists(): filename, remove_after = utility.createTempFile(self.image.data) content_type = magicfile.magic(filename) if content_type.startswith('image'): temp_file, x, y = utility.resaveExistingImage(filename) beforeSize = utility.fileSizeToInt(self.getFileSize()) if temp_file is not None and os.stat( temp_file.name)[stat.ST_SIZE] < beforeSize: self.image.manage_upload(temp_file) self.image.width = x self.image.height = y #have to redo the content_type after we modify the image in case it has changed content_type = magicfile.magic(temp_file.name) temp_file.close() self.setFileSize() self.image.content_type = content_type utility.removeTempFile(filename, remove_after)
def updateFileContentType(self): "Update the content type of the installed file" if self.exists(): if self.hasObject('data'): file_obj = aq_base(self.data) if hasattr(file_obj, 'data'): filename, remove_after = utility.createTempFile( file_obj.data) file_obj.content_type = magicfile.magic(filename) utility.removeTempFile(filename, remove_after)
def resaveExistingImage(self): "reisze existing images" if self.exists(): filename, remove_after = utility.createTempFile(self.data.data) content_type = magicfile.magic(filename) if content_type.startswith('image'): if self.getConfig('resaveOnUpload'): temp_file,x,y = utility.resaveExistingImage(filename) beforeSize = utility.fileSizeToInt(self.getFileSize()) if temp_file is not None and os.stat(temp_file.name)[stat.ST_SIZE] < beforeSize: if not self.data: self.manage_addProduct['Image'].manage_addImage('data', temp_file, 'data') else: self.data.manage_upload(temp_file) self.data.width = x self.data.height = y #have to redo the content_type after we modify the image in case it has changed content_type = magicfile.magic(temp_file.name) temp_file.close() self.setFileSize() self.storeContentType(content_type) utility.removeTempFile(filename, remove_after)
def resaveExistingImage(self): "reisze existing images" if self.exists(): filename, remove_after = utility.createTempFile(self.data.data) content_type = magicfile.magic(filename) if content_type.startswith('image'): if self.getConfig('resaveOnUpload'): temp_file, x, y = utility.resaveExistingImage(filename) beforeSize = utility.fileSizeToInt(self.getFileSize()) if temp_file is not None and os.stat( temp_file.name)[stat.ST_SIZE] < beforeSize: if not self.data: self.manage_addProduct['Image'].manage_addImage( 'data', temp_file, 'data') else: self.data.manage_upload(temp_file) self.data.width = x self.data.height = y #have to redo the content_type after we modify the image in case it has changed content_type = magicfile.magic(temp_file.name) temp_file.close() self.setFileSize() self.storeContentType(content_type) utility.removeTempFile(filename, remove_after)
def before_manage_edit(self, dict): "This is the object specific edit stuff" if dict.pop('_del', None) == 'Y': self.clear() if dict.get('data', None): #don't process if data does not have anything in it filename, remove_after = utility.createTempFile(dict['data']) content_type = magicfile.magic(filename) if content_type.startswith('image'): if not self.data: self.manage_addProduct['Image'].manage_addImage( 'data', '', 'data') if self.getConfig('resizeOnUpload'): self.resizeImage(filename) elif self.getConfig('resaveOnUpload'): temp_file, x, y = utility.resaveExistingImage(filename) if temp_file is not None: self.data.manage_upload(temp_file) temp_file.close() self.data.width = x self.data.height = y else: self.data.manage_upload(open(filename, 'rb')) else: self.data.manage_upload(open(filename, 'rb')) self.updateParentPaths(set(['data'])) self.makeThumbnail(filename) self.setFileSize() self.storeContentType(content_type) utility.removeTempFile(filename, remove_after) try: del dict['data'] except KeyError: pass