@@ -13,6 +13,7 @@ class PILImageModel(ImageFileModel):
1313 """
1414 accept_mime_types = ['image/jpeg' , 'image/webp' , 'image/png' , 'image/gif' ]
1515 exif_values = set (ExifTags .Base .__members__ .values ()) # TODO: some EXIF values can be removed
16+ MAX_STORED_IMAGE_WIDTH = 3840
1617
1718 class Meta :
1819 app_label = 'finder'
@@ -23,6 +24,15 @@ class Meta:
2324 def save (self , ** kwargs ):
2425 try :
2526 image = Image .open (default_storage .open (self .file_path ))
27+ image = self .orientate_top (image )
28+ if self .MAX_STORED_IMAGE_WIDTH and image .width > self .MAX_STORED_IMAGE_WIDTH :
29+ # limit the width of the stored image to prevent excessive disk usage
30+ height = round (self .MAX_STORED_IMAGE_WIDTH * image .height / image .width )
31+ image = image .resize ((self .MAX_STORED_IMAGE_WIDTH , height ))
32+ image .save (default_storage .open (self .file_path , 'wb' ), image .format )
33+ # recompute the file size and SHA1 hash
34+ self .file_size = default_storage .size (self .file_path )
35+ self .sha1 = self .digest_sha1 ()
2636 self .width = image .width
2737 self .height = image .height
2838 except Exception :
@@ -75,7 +85,9 @@ def orientate_top(self, image):
7585 def crop (self , thumbnail_path , width , height ):
7686 aspect_ratio = width / height
7787 image = Image .open (default_storage .open (self .file_path ))
78- image = self .orientate_top (image )
88+ assert width <= image .width and height <= image .height , \
89+ "The requested thumbnail size ({width}x{height}) is larger than the original image " \
90+ "({0}x{1})" .format (* image .size , width = width , height = height )
7991 orig_aspect_ratio = image .width / image .height
8092 crop_x , crop_y , crop_size , gravity = (
8193 self .meta_data .get ('crop_x' ),
@@ -137,7 +149,6 @@ def crop(self, thumbnail_path, width, height):
137149 crop_y = max (crop_y - max (min (crop_height - crop_size , crop_height ), 0 ), 0 )
138150 else : # centered crop
139151 crop_y = max (crop_y - (crop_height - crop_size ) / 2 , 0 )
140- crop_size = crop_resize
141152
142153 min_x = crop_x
143154 if min_x + crop_width > image .width :
0 commit comments