next-blurhash-previews 0.0.3-beta28 → 0.0.3-beta30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/generateBlurhash.js
CHANGED
@@ -21,17 +21,24 @@ export async function getBlurhash(path) {
|
|
21
21
|
const blurhashImage = await getSharpImage(path);
|
22
22
|
const dimensions = await blurhashImage.metadata();
|
23
23
|
|
24
|
-
|
24
|
+
const { width, height } = dimensions;
|
25
|
+
let blurhashW = width;
|
26
|
+
let blurhashH = height;
|
25
27
|
|
26
28
|
if (width > 200) {
|
27
|
-
|
28
|
-
|
29
|
-
|
29
|
+
const newBuffer = await blurhashImage.resize(200).toBuffer();
|
30
|
+
blurhashImage = sharp(newBuffer);
|
31
|
+
|
32
|
+
let newMetadata = await blurhashImage.metadata();
|
33
|
+
blurhashW = newMetadata.width;
|
34
|
+
blurhashH = newMetadata.height;
|
35
|
+
console.log("Adjustied", blurhashW, blurhashH);
|
36
|
+
} else {
|
37
|
+
console.log("Not adjusting", path, width);
|
30
38
|
}
|
31
39
|
|
32
40
|
return new Promise((res, rej) => {
|
33
41
|
blurhashImage
|
34
|
-
.resize(Math.min(width))
|
35
42
|
.raw()
|
36
43
|
.ensureAlpha()
|
37
44
|
.toBuffer((err, buffer) => {
|
@@ -41,13 +48,19 @@ export async function getBlurhash(path) {
|
|
41
48
|
} else {
|
42
49
|
const blurhash = encode(
|
43
50
|
new Uint8ClampedArray(buffer),
|
44
|
-
|
45
|
-
|
51
|
+
blurhashW,
|
52
|
+
blurhashH,
|
46
53
|
4,
|
47
54
|
4
|
48
55
|
);
|
49
56
|
if (isBlurhashValid(blurhash)) {
|
50
|
-
return res({
|
57
|
+
return res({
|
58
|
+
blurhash,
|
59
|
+
dw: width,
|
60
|
+
dh: height,
|
61
|
+
w: blurhashW,
|
62
|
+
h: blurhashH,
|
63
|
+
});
|
51
64
|
} else {
|
52
65
|
console.log("FAIL");
|
53
66
|
return rej("FAIL");
|
@@ -1,6 +1,12 @@
|
|
1
1
|
import { decode } from "../node_modules/blurhash/dist/esm/index";
|
2
2
|
|
3
|
-
type blurhash = {
|
3
|
+
type blurhash = {
|
4
|
+
w: number;
|
5
|
+
h: number;
|
6
|
+
dw: number;
|
7
|
+
dh: number;
|
8
|
+
blurhash: string;
|
9
|
+
};
|
4
10
|
|
5
11
|
class ImageWithPreview extends HTMLElement {
|
6
12
|
sd: ShadowRoot;
|
@@ -74,14 +80,14 @@ if (!customElements.get("blurhash-image")) {
|
|
74
80
|
}
|
75
81
|
|
76
82
|
function updateBlurHashPreview(canvasEl: HTMLCanvasElement, preview: blurhash) {
|
77
|
-
const { w
|
83
|
+
const { w, h, dw, dh } = preview;
|
78
84
|
|
79
|
-
canvasEl.width =
|
80
|
-
canvasEl.height =
|
85
|
+
canvasEl.width = dw;
|
86
|
+
canvasEl.height = dh;
|
81
87
|
|
82
|
-
const pixels = decode(preview.blurhash,
|
88
|
+
const pixels = decode(preview.blurhash, w, h);
|
83
89
|
const ctx = canvasEl.getContext("2d")!;
|
84
|
-
const imageData = ctx.createImageData(
|
90
|
+
const imageData = ctx.createImageData(w, h);
|
85
91
|
imageData.data.set(pixels);
|
86
92
|
ctx.putImageData(imageData, 0, 0);
|
87
93
|
}
|
package/imagePreviewBootstrap.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { createElement } from "react";
|
2
2
|
|
3
3
|
export const imagePreviewBootstrap = createElement("script", {
|
4
|
-
dangerouslySetInnerHTML: { __html: `(() => { "use strict";const
|
4
|
+
dangerouslySetInnerHTML: { __html: `(() => { "use strict";const R=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","#","$","%","*","+",",","-",".",":",";","=","?","@","[","]","^","_","{","|","}","~"],u=t=>{let e=0;for(let s=0;s<t.length;s++){const n=t[s],r=R.indexOf(n);e=e*83+r}return e},p=t=>{let e=t/255;return e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4)},v=t=>{let e=Math.max(0,Math.min(1,t));return e<=.0031308?Math.round(e*12.92*255+.5):Math.round((1.055*Math.pow(e,.4166666666666667)-.055)*255+.5)},D=t=>t<0?-1:1,x=(t,e)=>D(t)*Math.pow(Math.abs(t),e);class A extends Error{constructor(e){super(e),this.name="ValidationError",this.message=e}}const L=t=>{if(!t||t.length<6)throw new A("The blurhash string must be at least 6 characters");const e=u(t[0]),s=Math.floor(e/9)+1,n=e%9+1;if(t.length!==4+2*n*s)throw new A(\`blurhash length mismatch: length is \${t.length} but it should be \${4+2*n*s}\`)},G=t=>{const e=t>>16,s=t>>8&255,n=t&255;return[p(e),p(s),p(n)]},I=(t,e)=>{const s=Math.floor(t/361),n=Math.floor(t/19)%19,r=t%19;return[x((s-9)/9,2)*e,x((n-9)/9,2)*e,x((r-9)/9,2)*e]},T=(t,e,s,n)=>{L(t),n=n|1;const r=u(t[0]),a=Math.floor(r/9)+1,c=r%9+1,d=(u(t[1])+1)/166,g=new Array(c*a);for(let o=0;o<g.length;o++)if(o===0){const i=u(t.substring(2,6));g[o]=G(i)}else{const i=u(t.substring(4+o*2,6+o*2));g[o]=I(i,d*n)}const l=e*4,h=new Uint8ClampedArray(l*s);for(let o=0;o<s;o++)for(let i=0;i<e;i++){let C=0,q=0,y=0;for(let m=0;m<a;m++)for(let f=0;f<c;f++){const b=Math.cos(Math.PI*i*f/e)*Math.cos(Math.PI*o*m/s);let M=g[f+m*c];C+=M[0]*b,q+=M[1]*b,y+=M[2]*b}let B=v(C),E=v(q),P=v(y);h[4*i+0+o*l]=B,h[4*i+1+o*l]=E,h[4*i+2+o*l]=P,h[4*i+3+o*l]=255}return h};class O extends HTMLElement{sd;mo;static observedAttributes=["preview"];get#t(){return this.querySelector("img")}get#e(){return this.querySelector("canvas")}constructor(){super(),this.sd=this.attachShadow({mode:"open"}),this.sd.innerHTML='<slot name="preview"></slot>'}#s=()=>{if(this.#t&&this.#e)return this.mo?.disconnect(),this.#t.complete?this.#n():(this.#o(),this.#t.addEventListener("load",this.#n)),!0};connectedCallback(){this.#s()||(this.mo=new MutationObserver(this.#s),this.mo.observe(this,{subtree:!0,childList:!0,attributes:!1}))}#n=()=>{};attributeChangedCallback(e){if(this.#e&&e==="preview"){const s=this.#o();console.log(this.getAttribute("preview"),this.getAttribute("url"),s)}}#o(){const e=JSON.parse(this.getAttribute("preview")),s=+new Date;return S(this.#e,e),+new Date-s}}customElements.get("blurhash-image")||customElements.define("blurhash-image",O);function S(t,e){const{w:s,h:n,dw:r,dh:a}=e;t.width=r,t.height=a;const c=T(e.blurhash,s,n),w=t.getContext("2d"),d=w.createImageData(s,n);d.data.set(c),w.putImageData(d,0,0)} })();` },
|
5
5
|
});
|