@@ -47,7 +47,7 @@ def get_thumbnail_url(self):
4747 thumbnail_path = self .get_thumbnail_path (self .thumbnail_size , self .thumbnail_size )
4848 if str (self .id ) == 'XXX-561d6907-5fd7-40b9-a6f2-5db45a67eda6' or not default_storage .exists (thumbnail_path ):
4949 try :
50- self .crop (thumbnail_path , self .thumbnail_size , self .thumbnail_size * 2 )
50+ self .crop (thumbnail_path , self .thumbnail_size , self .thumbnail_size * 1.667 )
5151 except Exception :
5252 # thumbnail image could not be created
5353 return self .fallback_thumbnail_url
@@ -84,6 +84,7 @@ def crop(self, thumbnail_path, width, height):
8484 self .meta_data .get ('crop_size' ),
8585 self .meta_data .get ('gravity' ),
8686 )
87+ next_crop_size = None
8788 if crop_x is None or crop_y is None or crop_size is None :
8889 # crop in the center of the image
8990 if image .width > image .height :
@@ -100,29 +101,52 @@ def crop(self, thumbnail_path, width, height):
100101 # crop_x = max(crop_x - (width - crop_size) / 2, 0)
101102 # crop_y = max(crop_y - (height - crop_size) / 2, 0)
102103 # crop_size = width
103- # else:
104- # if height > crop_size * aspect_ratio:
105- # # extend the crop size to prevent blurry images
106- # crop_x = max(crop_x - (image.width - crop_size) / 2, 0)
107- # crop_size = min(max(width, height), image.width, image.height)
108- # crop_y = max(crop_y - (image.height - crop_size) / 2, 0)
104+ elif aspect_ratio > 1 :
105+ if width > crop_size :
106+ offset = width - crop_size
107+ next_crop_size = width
108+ else :
109+ if height > crop_size :
110+ offset = height - crop_size
111+ next_crop_size = height
112+ if next_crop_size is not None :
113+ # extend the crop size to prevent blurry images
114+ if gravity in ('e' , 'ne' , 'se' ):
115+ crop_x = max (crop_x - (width - crop_size ) / 2 , 0 )
116+ # crop_x = max(crop_x - (width - next_crop_size) / 2, 0)
117+ elif gravity in ('w' , 'nw' , 'sw' ):
118+ crop_x = max (crop_x - width + crop_size , 0 )
119+ else :
120+ crop_x = max (crop_x - (next_crop_size - crop_size ) / 2 , 0 )
121+ if gravity in ('n' , 'ne' , 'nw' ):
122+ crop_y = max (crop_y - height + crop_size , 0 )
123+ elif gravity in ('s' , 'se' , 'sw' ):
124+ crop_y = max (crop_y - (height - crop_size ) / 2 , 0 )
125+ # crop_y = max(crop_y - (height - next_crop_size) / 2, 0)
126+ else :
127+ crop_y = max (crop_y - (next_crop_size - crop_size ) / 2 , 0 )
128+ crop_size = next_crop_size
109129
110130 print (f"Crop parameters: crop_x={ crop_x } , crop_y={ crop_y } , crop_size={ crop_size } , gravity={ gravity } " )
111131
112132 if orig_aspect_ratio > aspect_ratio :
113- if image .width / crop_size > aspect_ratio :
114- # we can't fill the thumbnailed image with the cropped part
115- min_width = min (crop_size , image .height * aspect_ratio )
116- else :
117- min_width = image .height * aspect_ratio
118- min_height = min (crop_size , image .height )
133+ # if image.width / crop_size > aspect_ratio:
134+ # # we can't fill the thumbnailed image with the cropped part
135+ # min_width = min(crop_size, image.height * aspect_ratio)
136+ # else:
137+ # min_width = image.height * aspect_ratio
138+ # min_height = min(crop_size, image.height)
139+ min_width = min (crop_size , image .height * aspect_ratio )
140+ min_height = min_width / aspect_ratio
119141 else :
120- min_width = min (crop_size , image .width )
121- if image .height / crop_size < aspect_ratio :
122- # we can't fill the thumbnailed image with the cropped part
123- min_height = min (crop_size , image .width / aspect_ratio )
124- else :
125- min_height = image .width / aspect_ratio
142+ # min_width = max(crop_size, image.width)
143+ # if image.height / crop_size < aspect_ratio:
144+ # # we can't fill the thumbnailed image with the cropped part
145+ # min_height = min(crop_size, image.width / aspect_ratio)
146+ # else:
147+ # min_height = image.width / aspect_ratio
148+ min_height = min (crop_size , image .width / aspect_ratio )
149+ min_width = min_height * aspect_ratio
126150 min_x = max (crop_x + (crop_size - min_width ) / 2 , 0 )
127151 if min_x + min_width > image .width :
128152 min_x = max (image .width - min_width , 0 )
0 commit comments