这是我裁剪原始图像的信号。 它在上传时效果很好,但是当我在'wagtail.images.views.images.edit'中更改图像时不起作用。
找到解决方案: 更新
/wagtail.hooks.py/
@receiver(post_save, sender=get_image_model())
def resize_images_on_upload_or_edit(sender, instance, created=False, **kwargs):
if (instance.width and instance.height) > 2000:
old = sender.objects.get(pk=instance.pk)
if created or old:
croped_image = instance.get_rendition('max-2000x2000|jpegquality-80')
croped_file = croped_image.file.path
instance.width = croped_image.width
instance.height = croped_image.height
instance.file_size = os.path.getsize(croped_file)
original_file = instance.file.path
instance.save()
try:
os.rename(croped_file, original_file)
except FileExistsError:
os.replace(croped_file, original_file)
如果您希望代码在编辑现有图像时运行,而不是仅在创建时运行,则应删除
if created:
检查。