Django简易缩略图MAX SIZE [英] Django easy-thumbnails MAX SIZE

查看:166
本文介绍了Django简易缩略图MAX SIZE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法可以使用 easy-thumbnails 设置我的django应用中上传的图像的最大尺寸 app?



在设置列表中,我没有看到任何内容。

解决方案

要限制文件大小,您可能需要在Web服务器中执行,而不是在Django中执行。



或者,您可以指定自定义文件处理程序,与如果文件太大,可以引发错误:

  from django.core.files.uploadhandler import TemporaryFileUploadHandler,StopUpload 

class SizeLimitUploadHandler(TemporaryFileUploadHandler):
def new_file(self,field_name,file_name,content_type,content_length,charset):
if content_length> MAX_FILE_SIZE:
raise StopUpload(True)

尽管这会导致连接重置错误停止处理大文件。



如果您想要限制图像大小,您可以按照自述文件中的说明保存图像的大小:


通过将 resize_source 参数传递给 ThumbnailerImageField ,您可以在保存源图像之前调整其大小:




  class Profile(models。模型):
user = models.ForeignKey('auth.User')
avatar = ThumbnailerImageField(
upload_to ='avatars',
resize_source = dict(size =(50, 50),crop ='smart'),


is there a way to set a max size for the images uploaded in my django app using easy-thumbnails app?

In the settings list I don't see anything about it.

解决方案

To cap file size, you might want to do it in the webserver, rather than in Django.

Alternatively, you can specify a custom file handler, with which you can raise an error if the file is too big:

from django.core.files.uploadhandler import TemporaryFileUploadHandler, StopUpload

class SizeLimitUploadHandler(TemporaryFileUploadHandler):
    def new_file(self, field_name, file_name, content_type, content_length, charset):
        if content_length > MAX_FILE_SIZE:
            raise StopUpload(True)

Though this will cause a connection reset error in order to stop processing the large file.

If you want to cap image size, you can resize the image before it is saved as stated in the readme:

By passing a resize_source argument to the ThumbnailerImageField, you can resize the source image before it is saved:

class Profile(models.Model):
    user = models.ForeignKey('auth.User')
    avatar = ThumbnailerImageField(
        upload_to='avatars',
        resize_source=dict(size=(50, 50), crop='smart'),
    )

这篇关于Django简易缩略图MAX SIZE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆