create new image after crop
This commit is contained in:
parent
6b3ee76b80
commit
822b18e2f4
|
|
@ -1,8 +1,9 @@
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.validators import MinValueValidator, MaxValueValidator
|
from django.core.validators import MinValueValidator, MaxValueValidator
|
||||||
|
from django.core.files.base import ContentFile
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from sorl.thumbnail.parsers import parse_crop
|
from sorl.thumbnail import get_thumbnail
|
||||||
from sorl.thumbnail.parsers import ThumbnailParseError
|
from sorl.thumbnail.parsers import parse_crop, ThumbnailParseError
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from . import models
|
from . import models
|
||||||
|
|
@ -88,15 +89,23 @@ class CropImageSerializer(ImageSerializer):
|
||||||
quality = validated_data.pop('quality')
|
quality = validated_data.pop('quality')
|
||||||
crop = validated_data.pop('crop')
|
crop = validated_data.pop('crop')
|
||||||
|
|
||||||
|
crop_params = {
|
||||||
|
'geometry': f'{width}x{height}',
|
||||||
|
'quality': quality,
|
||||||
|
'crop': crop,
|
||||||
|
}
|
||||||
|
cropped_image = self._image.get_cropped_image(**crop_params)
|
||||||
image = self._image
|
image = self._image
|
||||||
|
image.pk = None
|
||||||
|
crop_params['geometry_string'] = crop_params.pop('geometry')
|
||||||
|
resized = get_thumbnail(self._image.image, **crop_params)
|
||||||
|
image.image.save(resized.name, ContentFile(resized.read()), True)
|
||||||
|
image.save()
|
||||||
|
|
||||||
if image and width and height:
|
if image and width and height:
|
||||||
setattr(image,
|
setattr(image,
|
||||||
'cropped_image',
|
'cropped_image',
|
||||||
image.get_cropped_image(
|
cropped_image)
|
||||||
geometry=f'{width}x{height}',
|
|
||||||
quality=quality,
|
|
||||||
crop=crop))
|
|
||||||
return image
|
return image
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user