def avatar_image_upload_handler(request): if request.method == "POST": try: file_img = request.FILES['avatar'] file_suffix = os.path.splitext( file_img.name)[len(os.path.splitext(file_img.name)) - 1] # 检查图片格式 if file_suffix not in image_type: return HttpResponse("请上传正确格式的图片文件") filename = uuid.uuid1().__str__() + file_suffix # 把头像压缩成90大小 img = Image.open(file_img) img = ThumbnailTool.constrain_len_thumbnail(img, 90) path = MEDIA_ROOT + "/avatar/" if not os.path.exists(path): os.makedirs(path) file_name = path + filename img.save(file_name) file_img_url = "http://" + request.META[ 'HTTP_HOST'] + MEDIA_URL + "avatar/" + filename user = request.user user.avatar_path = file_img_url user.save() except Exception, e: print e return HttpResponseRedirect(request.META.get('HTTP_REFERER', "/"))
def post(self, request, *args, **kwargs): # 将文件路径和其余信息存入数据库 title = request.POST.get("title", "") post = request.POST.get("post", "") post_foreignkey = Post.objects.get(pk=post) image_link = request.POST.get("image_link", "") if not image_link: filename = "" try: file_img = request.FILES['files'] file_suffix = os.path.splitext( file_img.name)[len(os.path.splitext(file_img.name)) - 1] filename = uuid.uuid1().__str__() + file_suffix # 把过大的图像压缩成合适的轮播图大小 img = Image.open(file_img) img = ThumbnailTool.constrain_len_thumbnail(img, 865) path = MEDIA_ROOT + "/carousel/" if not os.path.exists(path): os.makedirs(path) file_name = path + filename img.save(file_name) except Exception, e: print e file_img_url = "http://" + request.META[ 'HTTP_HOST'] + MEDIA_URL + "carousel/" + filename Carousel.objects.create( title=title, post=post_foreignkey, img=file_img_url, )
def post(self, request, *args, **kwargs): # 将文件路径和其余信息存入数据库 title = request.POST.get("title", "") post = request.POST.get("post", "") post_foreignkey = Post.objects.get(pk=post) image_link = request.POST.get("image_link", "") pkey = self.kwargs.get('pk') carousel = Carousel.objects.get(id=pkey) if not image_link: try: file_img = request.FILES['files'] file_suffix = os.path.splitext(file_img.name)[len(os.path.splitext(file_img.name)) - 1] filename = uuid.uuid1().__str__() + file_suffix # 把过大的图像压缩成合适的轮播图大小 img = Image.open(file_img) img = ThumbnailTool.constrain_len_thumbnail(img, 865) path = MEDIA_ROOT + "/carousel/" if not os.path.exists(path): os.makedirs(path) file_name = path + filename img.save(file_name) except Exception, e: print e file_img_url = "http://" + request.META['HTTP_HOST'] + MEDIA_URL + "carousel/" + filename carousel.title = title carousel.post = post_foreignkey carousel.img = file_img_url carousel.save()
def avatar_image_upload_handler(request): if request.method == "POST": try: file_img = request.FILES['avatar'] file_suffix = os.path.splitext(file_img.name)[len(os.path.splitext(file_img.name)) - 1] # 检查图片格式 if file_suffix not in image_type: return HttpResponse("请上传正确格式的图片文件") filename = uuid.uuid1().__str__() + file_suffix # 把头像压缩成90大小 img = Image.open(file_img) img = ThumbnailTool.constrain_len_thumbnail(img, 90) path = MEDIA_ROOT + "/avatar/" if not os.path.exists(path): os.makedirs(path) file_name = path + filename img.save(file_name) file_img_url = "http://" + request.META['HTTP_HOST'] + MEDIA_URL + "avatar/" + filename user = request.user user.avatar_path = file_img_url user.save() except Exception, e: print e return HttpResponseRedirect(request.META.get('HTTP_REFERER', "/"))
def tinymce_image_upload_handler(request): if request.method == "POST": try: file_img = request.FILES['tinymce-image-file'] file_suffix = os.path.splitext( file_img.name)[len(os.path.splitext(file_img.name)) - 1] # 检查图片格式 if file_suffix not in image_type: return HttpResponse("请上传正确格式的图片文件") filename = uuid.uuid1().__str__() + file_suffix # 图片宽大于824的时候,将其压缩到824px,刚好适合13吋pc的大小 img = Image.open(file_img) width, height = img.size if width > 824: img = ThumbnailTool.constrain_thumbnail(img, times=width / 824.0) path = MEDIA_ROOT + "/post/" if not os.path.exists(path): os.makedirs(path) file_name = path + filename img.save(file_name) file_img_url = "http://" + request.META[ 'HTTP_HOST'] + MEDIA_URL + "post/" + filename context = { 'result': "file_uploaded", 'resultcode': "ok", 'file_name': file_img_url } except Exception, e: context = { 'result': e, 'resultcode': "failed", } print e return TemplateResponse(request, "admin/plugin/ajax_upload_result.html", context)
def markdown_image_upload_handler(request): # 要返回的数据字典,组装好后,序列化为json格式 if request.method == "POST": result = {} try: file_img = request.FILES['editormd-image-file'] file_suffix = os.path.splitext( file_img.name)[len(os.path.splitext(file_img.name)) - 1] filename = uuid.uuid1().__str__() + file_suffix # 检查图片格式 if file_suffix not in image_type: result['success'] = 0 result['message'] = "上传失败,图片格式不正确" else: path = MEDIA_ROOT + "/post/" if not os.path.exists(path): os.makedirs(path) # 图片宽大于825的时候,将其压缩到824px,刚好适合13吋pc的大小 img = Image.open(file_img) width, height = img.size if width > 824: img = ThumbnailTool.constrain_thumbnail(img, times=width / 824.0) file_name = path + filename img.save(file_name) file_img_url = "http://" + request.META[ 'HTTP_HOST'] + MEDIA_URL + "post/" + filename result['success'] = 1 result['message'] = "上传成功" result['url'] = file_img_url except Exception, e: result['success'] = 0 result['message'] = e print e return HttpResponse(json.dumps(result))
def tinymce_image_upload_handler(request): if request.method == "POST": try: file_img = request.FILES['tinymce-image-file'] file_suffix = os.path.splitext(file_img.name)[len(os.path.splitext(file_img.name)) - 1] # 检查图片格式 if file_suffix not in image_type: return HttpResponse("请上传正确格式的图片文件") filename = uuid.uuid1().__str__() + file_suffix # 图片宽大于824的时候,将其压缩到824px,刚好适合13吋pc的大小 img = Image.open(file_img) width, height = img.size if width > 824: img = ThumbnailTool.constrain_thumbnail(img, times=width / 824.0) path = MEDIA_ROOT + "/post/" if not os.path.exists(path): os.makedirs(path) file_name = path + filename img.save(file_name) file_img_url = "http://" + request.META['HTTP_HOST'] + MEDIA_URL + "post/" + filename context = { 'result': "file_uploaded", 'resultcode': "ok", 'file_name': file_img_url } except Exception, e: context = { 'result': e, 'resultcode': "failed", } print e return TemplateResponse(request, "admin/plugin/ajax_upload_result.html", context)
def markdown_image_upload_handler(request): # 要返回的数据字典,组装好后,序列化为json格式 if request.method == "POST": result = {} try: file_img = request.FILES['editormd-image-file'] file_suffix = os.path.splitext(file_img.name)[len(os.path.splitext(file_img.name)) - 1] filename = uuid.uuid1().__str__() + file_suffix # 检查图片格式 if file_suffix not in image_type: result['success'] = 0 result['message'] = "上传失败,图片格式不正确" else: path = MEDIA_ROOT + "/post/" if not os.path.exists(path): os.makedirs(path) # 图片宽大于825的时候,将其压缩到824px,刚好适合13吋pc的大小 img = Image.open(file_img) width, height = img.size if width > 824: img = ThumbnailTool.constrain_thumbnail(img, times=width / 824.0) file_name = path + filename img.save(file_name) file_img_url = "http://" + request.META['HTTP_HOST'] + MEDIA_URL + "post/" + filename result['success'] = 1 result['message'] = "上传成功" result['url'] = file_img_url except Exception, e: result['success'] = 0 result['message'] = e print e return HttpResponse(json.dumps(result))