MCPcopy Create free account
hub / github.com/Hashnode/starter-kit / _resizeImage

Function _resizeImage

packages/utils/renderer/image.ts:5–80  ·  view source on GitHub ↗
(src, resize, defaultImage)

Source from the content-addressed store, hash-verified

3const DEFAULT_PHOTO = `${DEFAULT_AVATAR},format&format=webp`;
4
5const _resizeImage = (src, resize, defaultImage) => {
6 if (!src) {
7 return defaultImage;
8 } else {
9 // temp
10 if (src === '?sz=200') {
11 return DEFAULT_PHOTO;
12 }
13
14 let newSrc = src.replace('hashnode.imgix.net', 'cdn.hashnode.com');
15 if (src.indexOf('//res.cloudinary.com/hashnode') !== -1 && src.indexOf('/upload/') !== -1) {
16 const parts = src.split('/upload/');
17 const format = parts[1].substring(parts[1].lastIndexOf('.') + 1);
18 if (parts[1].indexOf('ama_banners') !== -1) {
19 const version = parts[1].substring(1, parts[1].indexOf('/'));
20 const path = parts[1].substring(parts[1].indexOf('/') + 1, parts[1].lastIndexOf('.'));
21 newSrc = `${parts[0]}/upload/${path}/${version}.${format}?auto=compress,format&format=webp`;
22 } else {
23 const nextParts = parts[1].split('/');
24 if (nextParts[0].indexOf('v') === 0) {
25 nextParts[0] = nextParts[0].substring(1);
26 }
27 newSrc = `${parts[0]}/upload/${nextParts[1].substring(0, nextParts[1].lastIndexOf('.'))}/${
28 nextParts[0]
29 }.${format}?auto=compress,format&format=webp`;
30 }
31 newSrc = newSrc
32 .replace('//res.cloudinary.com', '//cdn.hashnode.com/res')
33 .replace('http://', 'https://');
34 } else if (src.indexOf('//cdn.hashnode.com') !== -1 && src.indexOf('/upload/') !== -1) {
35 const parts = src.split('/upload/');
36 if (parts[1].indexOf('v') !== 0) {
37 newSrc = `${parts[0]}/upload/${parts[1].substring(parts[1].indexOf('/') + 1)}`;
38 }
39 }
40
41 if (newSrc.indexOf('//cdn.hashnode.com') === -1) {
42 return newSrc;
43 }
44
45 let opts = '';
46 Object.keys(resize).forEach((prop) => {
47 if (prop === 'w' || prop === 'h' || prop === 'mask' || prop === 'corner-radius') {
48 opts += `${prop}=${resize[prop]}&`;
49 } else if (prop === 'fill') {
50 opts += `fit=fill&fill=${resize[prop]}&`;
51 } else if (prop === 'c') {
52 opts += `fit=crop&crop=${resize[prop] === 'face' ? 'faces' : 'entropy'}&`;
53 }
54 });
55
56 if (resize.q === 'none') {
57 return `${newSrc}?${opts}`;
58 }
59
60 if (newSrc.indexOf('?') !== -1) {
61 let newSrcSplit = newSrc.split('?');
62 newSrc = newSrcSplit[0];

Callers 1

image.tsFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected