def get_middle_thumb_url(self): obj = Images.objects.filter(phobj_id=self.pk) if obj.exists(): return utils.get_prefix_path(obj.first().img.url, 'mid') else: return None
def save(self, *args, **kwargs): # Сначала - обычное сохранение super(Images, self).save(*args, **kwargs) # Проверяем, указана ли картинка if self.img: self.img.name = utils.to_webp(self.img.path, self.img.name) filepath = self.img.path new_db_img = utils.get_rename_path(self.img.name, self.pk) new_file_name = utils.get_rename_path(filepath, self.pk) thumb_file_name = utils.get_prefix_path(new_file_name) middle_file_name = utils.get_prefix_path(new_file_name, 'mid') # Создадим превьюшку image = PIL.Image.open(filepath) image = utils.get_box_thumb(image, settings.THUMB_IMAGE_SIZE) image.save(os.path.normpath(thumb_file_name)) utils.to_jpg(thumb_file_name) # Создадим обложку image = PIL.Image.open(filepath) image.thumbnail(settings.MIDDLE_SIZE_IMAGE, resample=PIL.Image.LANCZOS) image.save(os.path.normpath(middle_file_name)) utils.to_jpg(middle_file_name) # Просто уменьшим картинку до заданного MAX_SIZE_IMAGE image = PIL.Image.open(filepath) width, height = image.size if (width > settings.MAX_SIZE_IMAGE[0]) or ( height > settings.MAX_SIZE_IMAGE[1]): image.thumbnail(settings.MAX_SIZE_IMAGE, resample=PIL.Image.LANCZOS) # Наложение водяного знака image = utils.add_watermark(image) image.save(os.path.normpath(new_file_name)) self.img.name = new_db_img super(Images, self).save(*args, **kwargs) image.close() utils.to_jpg(self.img.path) if filepath != new_file_name: os.remove(filepath)
def get_middle_thumb_url(self): return utils.get_prefix_path(self.img.url, 'mid')
def get_thumb_url(self): return utils.get_prefix_path(self.img.url)