def test_generate_thumbnail(photo_fixture_snow): width, height, crop, quality, _, _ = settings.THUMBNAIL_SIZES[0] assert width == 256 assert height == 256 assert crop == 'cover' assert quality == 50 # Should generate image thumbnail and return bytes as we specified in the return_type result = get_thumbnail(photo_fixture_snow.base_file.id, width=width, height=height, crop=crop, quality=quality, return_type='bytes') assert result[:10] == b'\xff\xd8\xff\xe0\x00\x10JFIF' # It should also have saved the thumbnail data down to disk path = get_thumbnail_path(photo_fixture_snow.base_file.id, width, height, crop, quality) assert os.path.exists(path) assert str(path).endswith( str( Path('cache') / 'thumbnails' / 'photofile' / '256x256_cover_q50' / '{}.jpg'.format(str(photo_fixture_snow.base_file.id)))) assert len(result) == os.stat(path).st_size assert os.stat(path).st_size > 5929 * 0.8 assert os.stat(path).st_size < 5929 * 1.2
def thumbnailer(request, type, id, width, height, crop, quality): width = int(width) height = int(height) quality = int(quality) thumbnail_size_index = None force_accurate = False for i, thumbnail_size in enumerate(settings.THUMBNAIL_SIZES): if width == thumbnail_size[0] and height == thumbnail_size[ 1] and crop == thumbnail_size[2] and quality == thumbnail_size[ 3]: thumbnail_size_index = i force_accurate = thumbnail_size[5] break if thumbnail_size_index is None: return HttpResponseNotFound('No photo thumbnail with these parameters') photo_id = None photo_file_id = None if type == 'photo': photo_id = id elif type == 'photofile': photo_file_id = id path = get_thumbnail(photo_file=photo_file_id, photo=photo_id, width=width, height=height, crop=crop, quality=quality, return_type='url', force_accurate=force_accurate) return HttpResponseRedirect(path)
def thumbnail_view(request, photo_id, width, height, crop, quality): width = int(width) height = int(height) quality = int(quality) thumbnail_size_index = None for i, thumbnail_size in enumerate(settings.THUMBNAIL_SIZES): if width == thumbnail_size[0] and height == thumbnail_size[1] and crop == thumbnail_size[2] and quality == thumbnail_size[3]: thumbnail_size_index = i break if thumbnail_size_index is None: return HttpResponseNotFound('No photo thumbnail with these parameters') img_bytes = get_thumbnail(photo_id, width, height, crop, quality, return_type='bytes') response = HttpResponse(img_bytes, content_type='image/jpeg') return response