def post(self): if not self.request.files or 'myimage' not in self.request.files: self.write({"status": "error", "message": "对不起,请选择图片"}) return image_type_list = ['image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp', 'image/x-png'] send_file = self.request.files['myimage'][0] if send_file['content_type'] not in image_type_list: self.write({"status": "error", "message": "对不起,仅支持 jpg, jpeg, bmp, gif, png\ 格式的图片"}) return if len(send_file['body']) > 100 * 1024 * 1024: self.write({"status": "error", "message": "对不起,请上传100M以下的图片"}) return tmp_file = tempfile.NamedTemporaryFile(delete=True) tmp_file.write(send_file['body']) tmp_file.seek(0) try: img = Img.open(tmp_file.name) except IOError, error: logging.info(error) logging.info('+' * 30 + '\n') logging.info(self.request.headers) tmp_file.close() self.write({"status": "error", "message": "对不起,此文件不是图片"}) return
def crop(url, size, position='c', force=False): url = "%s/%s" % (config.root_path, url.lstrip('/')) path = gen_thumb_url(url, size, position=position) width, height = size try: image = Image.open(url) except IOError: logging.error('cannot open %s' % url) return w, h = image.size if (w, h) == (width, height): return save_image(image, path) if force and (width >= w or height >= h): return save_image(image, path) hr = height * 1.0 / h wr = width * 1.0 / w if hr > wr: wf = rcd(w * hr) hf = height else: wf = width hf = rcd(h * wr) resize = (wf, hf) image = image.resize(resize, Image.ANTIALIAS) if width * height == 0: return save_image(image, path) coo = None if wf > width: if position == 't': coo = (0, 0, width, height) elif position == 'b': coo = (wf - width, 0, wf, height) else: coo = (rcd((wf - width) / 2.0), 0, rcd((wf + width) / 2.0), height) elif hf > height: if position == 't': coo = (0, 0, width, height) elif position == 'b': coo = (0, hf - height, width, hf) else: coo = (0, rcd((hf - height) / 2.0), width, rcd((hf + height) / 2.0)) if coo: image = image.crop(coo) return save_image(image, path)
def post(self, node_id): category = self.get_argument('category', None) node = Node.get(id=node_id) if not node: return self.redirect_next_url() if not self.request.files or 'myimage' not in self.request.files: self.write({"status": "error", "message": "对不起,请选择图片"}) return image_type_list = ['image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp', 'image/x-png'] icon_type_list = ['image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp', 'image/x-png', 'image/ico .ico', 'image/x-icon'] send_file = self.request.files['myimage'][0] if category != 'icon' and send_file['content_type'] not in image_type_list: self.write({"status": "error", "message": "对不起,仅支持 jpg, jpeg, bmp, gif, png\ 格式的图片"}) return if category == 'icon' and send_file['content_type'] not in icon_type_list: self.write({"status": "error", "message": "对不起,仅支持 ico, jpg, jpeg, bmp, gif, png\ 格式的图片"}) return if len(send_file['body']) > 6 * 1024 * 1024: self.write({"status": "error", "message": "对不起,请上传6M以下的图片"}) return tmp_file = tempfile.NamedTemporaryFile(delete=True) tmp_file.write(send_file['body']) tmp_file.seek(0) try: image_one = Image.open(tmp_file.name) except IOError, error: logging.info(error) logging.info('+' * 30 + '\n') logging.info(self.request.headers) tmp_file.close() self.write({"status": "error", "message": "对不起,此文件不是图片"}) return
def post(self): user = self.current_user x = int(self.get_argument('x', 0)) y = int(self.get_argument('y', 0)) w = int(self.get_argument('w', 128)) h = int(self.get_argument('h', 128)) box = (x, y, x + w, y + h) avatar = get_asset_path(user.avatar_tmp) image_format = avatar[avatar.rfind('.'):] save_path = avatar[:avatar.rfind('.')] image = Image.open(avatar).crop(box) tmp_name = save_path + '_crop' + image_format image.save(tmp_name) size_set = ((48, 48), (60, 60), (128, 128)) def resize(size): image_ = Image.open(avatar).crop(box).resize(size, Image.ANTIALIAS) tmp_name_ = '%sx%d%s' % (save_path, size[0], image_format) image_.save(tmp_name_) with futures.ThreadPoolExecutor(max_workers=len(size_set)) as exe: list(exe.map(resize, size_set)) if user.avatar: try: os.system('rm -f %s*' % get_asset_path(user.avatar[:user.avatar.rfind('.')])) except: pass user.avatar = user.avatar_tmp try: orm.commit() except: pass src = self.current_user.avatar_tmp avatar = self.current_user.get_avatar(size=128) data = {"src": src, "avatar": avatar} return self.send_success_result(msg=u'头像设置成功', **data)
def post(self, node_id): category = self.get_argument("category", None) node = Node.get(id=node_id) if not node: return self.redirect_next_url() if not self.request.files or "myimage" not in self.request.files: self.write({"status": "error", "message": "对不起,请选择图片"}) return image_type_list = ["image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/bmp", "image/x-png"] icon_type_list = [ "image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/bmp", "image/x-png", "image/ico .ico", "image/x-icon", ] send_file = self.request.files["myimage"][0] if category != "icon" and send_file["content_type"] not in image_type_list: self.write( { "status": "error", "message": "对不起,仅支持 jpg, jpeg, bmp, gif, png\ 格式的图片", } ) return if category == "icon" and send_file["content_type"] not in icon_type_list: self.write( { "status": "error", "message": "对不起,仅支持 ico, jpg, jpeg, bmp, gif, png\ 格式的图片", } ) return if len(send_file["body"]) > 6 * 1024 * 1024: self.write({"status": "error", "message": "对不起,请上传6M以下的图片"}) return tmp_file = tempfile.NamedTemporaryFile(delete=True) tmp_file.write(send_file["body"]) tmp_file.seek(0) try: image_one = Image.open(tmp_file.name) except IOError as error: logging.info(error) logging.info("+" * 30 + "\n") logging.info(self.request.headers) tmp_file.close() self.write({"status": "error", "message": "对不起,此文件不是图片"}) return width = image_one.size[0] height = image_one.size[1] if width < 20 or height < 20 or width > 30000 or height > 30000: tmp_file.close() self.write({"status": "error", "message": "对不起,请上传长宽在20px~30000px之间的图片!"}) return user = self.current_user upload_path = sys.path[0] + "/static/upload/" + get_year() + "/" + get_month() + "/" if not os.path.exists(upload_path): try: os.system("mkdir -p %s" % upload_path) except: pass timestamp = ( str(int(time.time())) + "".join(random.sample("ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba", 6)) + "_" + str(user.id) ) image_format = send_file["filename"].split(".").pop().lower() tmp_name = upload_path + timestamp + "." + image_format image_one.save(tmp_name) tmp_file.close() path = "/" + "/".join(tmp_name.split("/")[tmp_name.split("/").index("static") :]) del_path = None if category == "head": del_path = node.head_img node.head_img = path msg = "节点头部背景设置成功" data = {"path": path, "category": "head"} elif category == "icon": image_two = image_one.resize((75, 75), Image.ANTIALIAS) tmp_name2 = upload_path + timestamp + "x75." + image_format image_two.save(tmp_name2) try: os.system("rm -f %s%s" % (sys.path[0], path)) except: pass path = "/" + "/".join(tmp_name2.split("/")[tmp_name2.split("/").index("static") :]) del_path = node.icon_img node.icon_img = path msg = "节点图标设置成功" data = {"path": path, "category": "icon"} elif category == "background": del_path = node.background_img node.background_img = path msg = "节点背景设置成功" data = {"path": path, "category": "background"} else: msg = "图片上传成功" data = {"path": path} if del_path: try: os.system("rm -f %s%s" % (sys.path[0], del_path)) except: pass return self.send_success_result(msg=msg, **data)
def post(self): if not self.request.files or 'myimage' not in self.request.files: self.write({"status": "error", "message": "对不起,请选择图片"}) return image_type_list = ['image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp', 'image/x-png'] send_file = self.request.files['myimage'][0] if send_file['content_type'] not in image_type_list: self.write({"status": "error", "message": "对不起,仅支持 jpg, jpeg, bmp, gif, png\ 格式的图片"}) return if len(send_file['body']) > 100 * 1024 * 1024: self.write({"status": "error", "message": "对不起,请上传100M以下的图片"}) return tmp_file = tempfile.NamedTemporaryFile(delete=True) tmp_file.write(send_file['body']) tmp_file.seek(0) try: img = Img.open(tmp_file.name) except IOError as error: logging.info(error) logging.info('+' * 30 + '\n') logging.info(self.request.headers) tmp_file.close() self.write({"status": "error", "message": "对不起,此文件不是图片"}) return width, height = img.size if width < 80 or height < 80 or width > 30000 or height > 30000: tmp_file.close() self.write({"status": "error", "message": "对不起,请上传长宽在80px~30000px之间的图片!"}) return user = self.current_user suffix = img.format.lower() upload_path = gen_upload_path(suffix=suffix) img.save(upload_path, img.format or 'JPEG') tmp_file.close() path = '/%s' % get_relative_path(upload_path).lstrip('/') album_id = self.get_argument('album_id', '') if not album_id: album = user.default_album else: album = Album.get(id=album_id) if not (album and album.user_id != user.id): album = user.default_album image = Image(user_id=user.id, album_id=album.id, path=path, width=width, height=height).save() image.crop() return self.send_success_result(msg='上传成功', **image.to_dict())
def post(self): if not self.has_permission: return user = self.current_user if not user: return self.redirect_next_url() if self.request.files == {} or 'myimage' not in self.request.files: self.write({"status": "error", "message": "对不起,请选择图片"}) return image_type_list = ['image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp', 'image/x-png'] send_file = self.request.files['myimage'][0] if send_file['content_type'] not in image_type_list: self.write({"status": "error", "message": "对不起,仅支持 jpg, jpeg, bmp, gif, png\ 格式的图片"}) return if len(send_file['body']) > 6 * 1024 * 1024: self.write({"status": "error", "message": "对不起,请上传6M以下的图片"}) return tmp_file = tempfile.NamedTemporaryFile(delete=True) tmp_file.write(send_file['body']) tmp_file.seek(0) try: image_one = Image.open(tmp_file.name) except IOError as error: logging.info(error) logging.info('+' * 30 + '\n') logging.info(self.request.headers) tmp_file.close() self.write({"status": "error", "message": "对不起,此文件不是图片"}) return width = image_one.size[0] height = image_one.size[1] if width < 80 or height < 80 or width > 30000 or height > 30000: tmp_file.close() self.write({"status": "error", "message": "对不起,请上传长宽在80px~30000px之间的图片!"}) return user = self.current_user upload_path = os.path.join(config.upload_path, get_year(), get_month()) mkdir_p(upload_path) timestamp = str(int(time.time())) + gen_random_str() + '_' + str(user.id) image_format = send_file['filename'].split('.').pop().lower() filename = timestamp + '.' + image_format tmp_path = os.path.join(upload_path, filename) image_one.save(tmp_path) tmp_file.close() path = '/' + get_relative_path(tmp_path) category = self.get_argument('category', None) del_path = None if category == 'head': del_path = user.head_img user.head_img = path data = {'path': path, 'category': 'head'} elif category == 'background': del_path = user.background_img user.background_img = path data = {'path': path, 'category': 'background'} else: data = {'path': path, 'category': 'other'} if del_path: remove_file(get_asset_path(del_path)) return self.send_success_result(**data)
def resize(size): image_ = Image.open(avatar).crop(box).resize(size, Image.ANTIALIAS) tmp_name_ = '%sx%d%s' % (save_path, size[0], image_format) image_.save(tmp_name_)
def post(self): if self.request.files == {} or 'myavatar' not in self.request.files: self.write({"status": "error", "message": "请选择图片!"}) return image_type_list = ['image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp', 'image/x-png'] send_file = self.request.files['myavatar'][0] if send_file['content_type'] not in image_type_list: self.write({"status": "error", "message": "仅支持 jpg, jpeg, bmp, gif, png\ 格式的图片!"}) return if len(send_file['body']) > 100 * 1024 * 1024: self.write({"status": "error", "message": "请上传100M以下的图片!"}) return tmp_file = tempfile.NamedTemporaryFile(delete=True) tmp_file.write(send_file['body']) tmp_file.seek(0) try: image_one = Image.open(tmp_file.name) except IOError as error: logging.info(error) logging.info('+' * 30 + '\n') logging.info(self.request.headers) tmp_file.close() self.write({"status": "error", "message": "图片不合法!"}) return width = image_one.size[0] height = image_one.size[1] if width < 24 or height < 24 or width > 20000 or height > 20000: tmp_file.close() self.write({"status": "error", "message": "图片长宽在24px~20000px之间!"}) return timestamp = str(int(time.time())) user = self.current_user upload_path = os.path.join(config.upload_path, 'avatar') mkdir_p(upload_path) if user: timestamp += '_' + str(user.id) else: timestamp += '_' + gen_random_str() image_format = send_file['filename'].split('.').pop().lower() filename = timestamp + '.' + image_format tmp_path = os.path.join(upload_path, filename) if os.path.exists(tmp_path): while True: if os.path.exists(tmp_path): timestamp += '_' + gen_random_str() filename = timestamp + '.' + image_format tmp_path = os.path.join(upload_path, filename) else: break image_one.save(tmp_path) tmp_file.close() src = '/' + get_relative_path(tmp_path) if user: user.avatar_tmp = src data = {"src": src, "height": height, "width": width} return self.send_success_result(msg=u'成功上传头像', **data)
def post(self): if not self.request.files or 'myimage' not in self.request.files: self.write({"status": "error", "message": "对不起,请选择图片"}) return image_type_list = [ 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp', 'image/x-png' ] send_file = self.request.files['myimage'][0] if send_file['content_type'] not in image_type_list: self.write({ "status": "error", "message": "对不起,仅支持 jpg, jpeg, bmp, gif, png\ 格式的图片" }) return if len(send_file['body']) > 100 * 1024 * 1024: self.write({"status": "error", "message": "对不起,请上传100M以下的图片"}) return tmp_file = tempfile.NamedTemporaryFile(delete=True) tmp_file.write(send_file['body']) tmp_file.seek(0) try: img = Img.open(tmp_file.name) except IOError as error: logging.info(error) logging.info('+' * 30 + '\n') logging.info(self.request.headers) tmp_file.close() self.write({"status": "error", "message": "对不起,此文件不是图片"}) return width, height = img.size if width < 80 or height < 80 or width > 30000 or height > 30000: tmp_file.close() self.write({ "status": "error", "message": "对不起,请上传长宽在80px~30000px之间的图片!" }) return user = self.current_user suffix = img.format.lower() upload_path = gen_upload_path(suffix=suffix) img.save(upload_path, img.format or 'JPEG') tmp_file.close() path = '/%s' % get_relative_path(upload_path).lstrip('/') album_id = self.get_argument('album_id', '') if not album_id: album = user.default_album else: album = Album.get(id=album_id) if not (album and album.user_id != user.id): album = user.default_album image = Image(user_id=user.id, album_id=album.id, path=path, width=width, height=height).save() image.crop() return self.send_success_result(msg='上传成功', **image.to_dict())
def post(self, node_id): category = self.get_argument('category', None) node = Node.get(id=node_id) if not node: return self.redirect_next_url() if not self.request.files or 'myimage' not in self.request.files: self.write({"status": "error", "message": "对不起,请选择图片"}) return image_type_list = [ 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp', 'image/x-png' ] icon_type_list = [ 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp', 'image/x-png', 'image/ico .ico', 'image/x-icon' ] send_file = self.request.files['myimage'][0] if category != 'icon' and send_file[ 'content_type'] not in image_type_list: self.write({ "status": "error", "message": "对不起,仅支持 jpg, jpeg, bmp, gif, png\ 格式的图片" }) return if category == 'icon' and send_file[ 'content_type'] not in icon_type_list: self.write({ "status": "error", "message": "对不起,仅支持 ico, jpg, jpeg, bmp, gif, png\ 格式的图片" }) return if len(send_file['body']) > 6 * 1024 * 1024: self.write({"status": "error", "message": "对不起,请上传6M以下的图片"}) return tmp_file = tempfile.NamedTemporaryFile(delete=True) tmp_file.write(send_file['body']) tmp_file.seek(0) try: image_one = Image.open(tmp_file.name) except IOError as error: logging.info(error) logging.info('+' * 30 + '\n') logging.info(self.request.headers) tmp_file.close() self.write({"status": "error", "message": "对不起,此文件不是图片"}) return width = image_one.size[0] height = image_one.size[1] if width < 20 or height < 20 or width > 30000 or height > 30000: tmp_file.close() self.write({ "status": "error", "message": "对不起,请上传长宽在20px~30000px之间的图片!" }) return user = self.current_user upload_path = sys.path[0] + "/static/upload/" + get_year() + '/' +\ get_month() + "/" if not os.path.exists(upload_path): try: os.system('mkdir -p %s' % upload_path) except: pass timestamp = str(int(time.time())) + \ ''.join(random.sample('ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba', 6)) + '_' + str(user.id) image_format = send_file['filename'].split('.').pop().lower() tmp_name = upload_path + timestamp + '.' + image_format image_one.save(tmp_name) tmp_file.close() path = '/' +\ '/'.join(tmp_name.split('/')[tmp_name.split('/').index("static"):]) del_path = None if category == 'head': del_path = node.head_img node.head_img = path msg = '节点头部背景设置成功' data = {'path': path, 'category': 'head'} elif category == 'icon': image_two = image_one.resize((75, 75), Image.ANTIALIAS) tmp_name2 = upload_path + timestamp + 'x75.' + image_format image_two.save(tmp_name2) try: os.system('rm -f %s%s' % (sys.path[0], path)) except: pass path = '/' +\ '/'.join(tmp_name2.split('/')[tmp_name2.split('/').index("static"):]) del_path = node.icon_img node.icon_img = path msg = '节点图标设置成功' data = {'path': path, 'category': 'icon'} elif category == 'background': del_path = node.background_img node.background_img = path msg = '节点背景设置成功' data = {'path': path, 'category': 'background'} else: msg = '图片上传成功' data = {'path': path} if del_path: try: os.system('rm -f %s%s' % (sys.path[0], del_path)) except: pass return self.send_success_result(msg=msg, **data)