#django #django-models
#django #django-модели
Вопрос:
Если я хочу image_width
и image_height
для экземпляров Original
, которые будут взяты из ResizedImageField
, как я могу это сделать?
class Original(models.Model):
image = ResizedImageField(
blank=True, null=True,
verbose_name = _('Original image'),
upload_to = upload_image,
width_field = 'image_width',
height_field = 'image_height',
validators = [dimension_validator],
)
image_width = models.PositiveIntegerField(
blank=True, null=True,
verbose_name = _('Image width'),
editable = False,
default = 0,
)
image_height = models.PositiveIntegerField(
blank=True, null=True,
verbose_name = _('Image height'),
editable = False,
default = 0,
)
class ResizedImageFieldFile(ImageFieldFile):
def save(self, name, content, save=True):
super(ResizedImageFieldFile, self).save(name, content, save)
img = Image.open(self.path)
x1 = img.size[0]
y1 = img.size[-1]
max_width = 800
max_height = 1200
if x1 > max_width and y1 > max_height:
x2 = max_width
y2 = int(float(x2)/float(x1) * y1) - 1
elif x1 > max_width and y1 <= max_height:
x2 = max_width
y2 = int(float(x2)/float(x1) * y1) - 1
elif x1 <= max_width and y1 > max_height:
y2 = max_height
x2 = int(float(y2)/float(y1) * x1) - 1
if x2 > max_width:
x2 = max_width
y2 = int(float(x2)/float(x1) * y1) - 1
elif x1 <= max_width and y1 <= max_height:
y2 = y1
x2 = x1
if y2 != y1 and x2 != x1:
img = img.resize((x2, y2), Image.ANTIALIAS)
img.save(self.path)
def delete(self, save=True):
os.remove(self.path)
super(ResizedImageFieldFile, self).delete(save)
class ResizedImageField(ImageField):
attr_class = ResizedImageFieldFile
def __init__(self, *args, **kwargs):
super(ResizedImageField, self).__init__(*args, **kwargs)
мне нужно, чтобы они указывали правильную ширину и высоту изображения, когда пользователь выбирает область для обрезки (нельзя просто использовать свойство overflow из-за IE):
<table>
<tbody>
<tr>
<td style="max-width: 800px; overflow-x: auto; position: relative">
<img style="position: relative;" width="{{ original.image_width }}" height="{{ original.image_height }}" src="{{ original.image.url }}" id="cropbox" alt="" />
</td>
<td>
<p style="text-align: left; margin-left: 10px;">Your avatar</p>
<div style="width: 180px; height: 180px; margin-left: 10px; margin-bottom: 10px; overflow: hidden;">
<img src="{{ original.image.url }}" id="preview" alt="" />
</div>
</td>
</tr>
</tbody>
</table>
Ответ №1:
Я собираюсь предположить, что проблема в том, что image_height и image_width заполняются ИСХОДНОЙ высотой и шириной перед изменением размера. Если это другое, пожалуйста, обновите свой вопрос.
Не глядя на код ImageField, я также предполагаю, что это будет исправлено, если вы переместите super(ResizedImageField, self).save() в нижнюю часть вашего нового метода после изменения размера изображения, а не в верхнюю.
Комментарии:
1. Да, <img width=»{{ original.width }}» height=»{{ original.height }}»> в качестве таблицы для обрезки изображения.
Ответ №2:
Что-то вроде этого работает для меня (это небольшой фиксированный update_dimension_fields
метод ImageField):
class MyImageField(ImageField):
def update_dimension_fields(self, instance, force=False, *args, **kwargs):
"""
Updates field's width and height fields, if defined.
This method is hooked up to model's post_init signal to update
dimensions after instantiating a model instance. However, dimensions
won't be updated if the dimensions fields are already populated. This
avoids unnecessary recalculation when loading an object from the
database.
Dimensions can be forced to update with force=True, which is how
ImageFileDescriptor.__set__ calls this method.
"""
# Nothing to update if the field doesn't have have dimension fields.
has_dimension_fields = self.width_field or self.height_field
if not has_dimension_fields:
return
# getattr will call the ImageFileDescriptor's __get__ method, which
# coerces the assigned value into an instance of self.attr_class
# (ImageFieldFile in this case).
file = getattr(instance, self.attname)
# Nothing to update if we have no file and not being forced to update.
if not file and not force:
return
dimension_fields_filled = not(
(self.width_field and not getattr(instance, self.width_field))
or (self.height_field and not getattr(instance, self.height_field))
)
# When both dimension fields have values, we are most likely loading
# data from the database or updating an image field that already had
# an image stored. In the first case, we don't want to update the
# dimension fields because we are already getting their values from the
# database. In the second case, we do want to update the dimensions
# fields and will skip this return because force will be True since we
# were called from ImageFileDescriptor.__set__.
if dimension_fields_filled and not force:
return
# file should be an instance of ImageFieldFile or should be None.
if file:
width = file.width
height = file.height
else:
# No file, so clear dimensions fields.
width = None
height = None
max_width = 800
max_height = 1200
x1 = width
y1 = height
if x1 > max_width and y1 > max_height:
x2 = max_width
y2 = int(float(x2)/float(x1) * y1) - 1
elif x1 > max_width and y1 <= max_height:
x2 = max_width
y2 = int(float(x2)/float(x1) * y1) - 1
elif x1 <= max_width and y1 > max_height:
y2 = max_height
x2 = int(float(y2)/float(y1) * x1) - 1
if x2 > max_width:
x2 = max_width
y2 = int(float(x2)/float(x1) * y1) - 1
elif x1 <= max_width and y1 <= max_height:
y2 = y1
x2 = x1
# Update the width and height fields.
if self.width_field:
setattr(instance, self.width_field, x2)
if self.height_field:
setattr(instance, self.height_field, y2)
class ResizedImageField(MyImageField):
attr_class = ResizedImageFieldFile
def __init__(self, *args, **kwargs):
super(ResizedImageField, self).__init__(*args, **kwargs)