0

目前我正在做一个项目,让用户上传图片,他们应该缩小到 210 的宽度。

做这个的最好方式是什么?

    from django.db import models
    from django.contrib.auth.models import User
    from imagekit.models import ImageSpecField
    from imagekit.processors import ResizeToFill, Adjust

    class Photo(models.Model):
        photo_title = models.CharField(max_length=255, null=True, blank=True)
        original = models.ImageField(upload_to='photos/%Y/%m/%d')
        main_page = ImageSpecField([ResizeToFill(210)], image_field='original', format='JPEG', options={'quality': 100})

        def __unicode__(self):
            return self.photo_title

这不会生成 main_page 图像,因此 photo.main_page 不会进入模板。

4

2 回答 2

1

If what you need is only getting the resized photo to be displayed in a template, you'll be better off using easy-thumbnails.

With it, your model can simply be:

class Photo(models.Model):
    photo_title = models.CharField(max_length=255, null=True, blank=True)
    original = models.ImageField(upload_to='photos/%Y/%m/%d')

And in the template:

{% load thumbnail %}
<img src="{% thumbnail photo.original 210 crop quality=100 %}" alt="">
于 2012-11-03T22:46:23.267 回答
1

好吧,这就是没有任何额外功能的工作。

https://github.com/un1t/django-resized

一样容易:

from django_resized import ResizedImageField

class Photo(models.Model):
    photo_title = models.CharField(max_length=255, null=True, blank=True)
    original = ResizedImageField(max_width=210, upload_to='photos/%Y/%m/%d')

我将创建一个新模型,该模型将包含各种尺寸的原始图片和我的图片。

于 2012-11-04T00:36:43.753 回答