Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why decoded blurhash is so big? #154

Open
rabinpoudyal opened this issue Jan 20, 2022 · 2 comments
Open

Why decoded blurhash is so big? #154

rabinpoudyal opened this issue Jan 20, 2022 · 2 comments
Labels
algorithm Relates to the BlurHash algorithm question Further information is requested

Comments

@rabinpoudyal
Copy link

I tried to create a blurhash from 50x50 image and tried decoding it into base64. I got very lengthly string as output (400-2000):
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAFA3PEY8MlBGQUZaVVBfeMiCeG5uePWvuZHI////////////////////////////////////////////////////2wBDAVVaWnhpeOuCguv/////////////////////////////////////////////////////////////////////////wAARCAAgACADAREAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAQMCBP/EABgQAQEBAQEAAAAAAAAAAAAAAAEAAhES/8QAFgEBAQEAAAAAAAAAAAAAAAAAAAEC/8QAFREBAQAAAAAAAAAAAAAAAAAAAAH/2gAMAwEAAhEDEQA/AM5YioxQtRntESzZiqjUC1GFoihiNHzAJBhzRHSFpSkE9EByg//Z

Is there any way to shorten this string? It seems airbnb also has similar blurhash implementation but they have only 179character long encoded base64 string for images. I am trying to decode in server since the page has lot of images and not feasible to decode in client side.

@jerry-git jerry-git added algorithm Relates to the BlurHash algorithm question Further information is requested labels Jun 10, 2022
@predaytor
Copy link

predaytor commented Jul 12, 2022

Not sure why it takes such long string in your example, I'm using sharp with 16px sizes, blur 4px, and you should pick lossless for better compression (great quality, less artifacts).

export async function resolveBlurhashURI(hash: string, width = 16, height = 16, punch = 1) {
	if (!isBlurhashValid(hash)) throw Error('invalid hash');

	const pixels = decode(hash, width, height, punch);

	const resizedImageBuffer = await sharp(Buffer.from(pixels), {
		raw: {
			channels: 4,
			width,
			height,
		},
	})
		.webp({
			lossless: true,
			smartSubsample: true,
			quality: 100,
		})
                .blur(4)
		.toBuffer();

	return `data:image/webp;base64,${resizedImageBuffer.toString('base64')}`;
}

@ahmadkhalaf1
Copy link

Not sure why it takes such long string in your example, I'm using sharp with 16px sizes, blur 4px, and you should pick lossless for better compression (great quality, less artifacts).

export async function resolveBlurhashURI(hash: string, width = 16, height = 16, punch = 1) {
	if (!isBlurhashValid(hash)) throw Error('invalid hash');

	const pixels = decode(hash, width, height, punch);

	const resizedImageBuffer = await sharp(Buffer.from(pixels), {
		raw: {
			channels: 4,
			width,
			height,
		},
	})
		.webp({
			lossless: true,
			smartSubsample: true,
			quality: 100,
		})
                .blur(4)
		.toBuffer();

	return `data:image/webp;base64,${resizedImageBuffer.toString('base64')}`;
}

Can i use this with nodejs ? can you please show an example how would this function work ?
can i pass an image url and it will hash it for me + sharp optimization ?
where does decode function comes from?

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
algorithm Relates to the BlurHash algorithm question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants