Skip to content

Commit f3c5079

Browse files
authored
Merge pull request #2 from BuildFire/cloudImg-fix
fix for new cloudimage urls
2 parents 8bac1ea + 0e97e33 commit f3c5079

File tree

4 files changed

+35
-49
lines changed

4 files changed

+35
-49
lines changed

src/control/content/components/LazyImg.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@ export default class Image extends PureComponent {
1818
if (removeImage) removeImage(id, type);
1919
};
2020

21-
optimizeImage = url => {
22-
const size = url.match(/\/\d+\D\d+\//g);
23-
if (!url.includes('cloudimg.io') || !size || /png-lossy-\d\d.q\d\d/g.test(url)) {
24-
return url;
25-
}
26-
return url.replace(/\/s\/crop\/\d+\D\d+\//g, `/crop${size[0]}png-lossy-65.q65.i1/`);
27-
};
28-
2921
handleImgError = e => {
3022
if (e.target) {
3123
e.target.src = e.target.attributes
@@ -60,19 +52,18 @@ export default class Image extends PureComponent {
6052
const { imageLib } = window.buildfire;
6153

6254
const placeholderSrc = '../../../../../styles/media/holder-1x1.gif';
63-
const croppedSrc = imageLib.cropImage(src, { width: 125, height: 125 });
64-
const finalSrc = this.optimizeImage(croppedSrc);
55+
const croppedSrc = imageLib.cropImage(src, { width: 125, height: 125, compression: 65 });
6556

6657
return (
6758
<div className={`image ${selected ? 'selected' : ''}`} onClick={this.handleClick}>
6859
<img
69-
src={fid ? placeholderSrc : finalSrc}
60+
src={fid ? placeholderSrc : croppedSrc}
7061
alt={alt}
7162
className="lazy"
72-
data-src={finalSrc}
63+
data-src={croppedSrc}
7364
data-srcset={srcset}
7465
data-sizes={sizes}
75-
data-fallbacksrc={croppedSrc}
66+
data-fallbacksrc={src}
7667
width={width}
7768
height={height}
7869
onError={this.handleImgError}

src/control/content/containers/Content.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ class Content extends Component {
3131
if (!croppedImg.includes('cloudimg.io')) {
3232
return src;
3333
}
34-
return croppedImg.replace(/\/s\/crop\/\d+\D\d+\//g, '/cdno/n/q1/');
34+
35+
let regex = /\/crop\/\d+\D\d+\/n\//g;
36+
37+
if (croppedImg.indexOf('/s/crop/') > -1) {
38+
regex = /\/s\/crop\/\d+\D\d+\//g;
39+
}
40+
return croppedImg.replace(regex, '/cdno/n/q1/');
3541
};
3642

3743
const imagePromises = selectedFiles.map(

src/widget/components/Image.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@ export default class Image extends PureComponent {
1313
viewImage(image.src);
1414
};
1515

16-
optimizeImage = url => {
17-
const size = url.match(/\/\d+\D\d+\//g);
18-
if (!url.includes('cloudimg.io') || !size || /png-lossy-\d\d.q\d\d/g.test(url)) {
19-
return url;
20-
}
21-
return url.replace(/\/s\/crop\/\d+\D\d+\//g, `/crop${size[0]}png-lossy-65.q65.i1/`);
22-
};
23-
2416
handleImgError = e => {
2517
if (e.target) {
2618
e.target.src = e.target.attributes
@@ -43,16 +35,19 @@ export default class Image extends PureComponent {
4335
const { image } = this.props;
4436
const { src } = image;
4537

46-
const croppedSrc = window.buildfire.imageLib.cropImage(src, { width: 125, height: 125 });
47-
const finalSrc = this.optimizeImage(croppedSrc);
38+
const croppedSrc = window.buildfire.imageLib.cropImage(src, {
39+
width: 125,
40+
height: 125,
41+
compression: 65
42+
});
4843

4944
return (
5045
<div className="img__holder" onClick={this.openImage}>
5146
<LazyLoad height={125} overflow offset={window.innerHeight} throttle={0}>
5247
<img
5348
ref={this.image}
54-
src={finalSrc}
55-
data-fallbacksrc={croppedSrc}
49+
src={croppedSrc}
50+
data-fallbacksrc={src}
5651
alt="placeholder"
5752
onError={this.handleImgError}
5853
onLoad={this.handleOnLoad}

src/widget/containers/Widget.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,32 +74,26 @@ class Widget extends Component {
7474
preload: [1, 1]
7575
};
7676

77-
const optimizeImage = config => {
78-
const { croppedSrc, width, height, compression } = config;
79-
if (!croppedSrc.includes('cloudimg.io')) {
80-
return croppedSrc;
81-
}
82-
83-
return croppedSrc.replace(
84-
/\/s\/crop\/\d+\D\d+\//g,
85-
`/crop/${Math.floor(width)}x${Math.floor(height)}/${compression || 'n'}/`
86-
);
87-
};
88-
8977
const galleryItems = (folderImages || images).map(img => {
90-
const croppedSrc = imageLib.cropImage(img.src, { width: img.width, height: img.height });
9178
const { width, height } = img;
79+
const croppedSrc = imageLib.cropImage(img.src, {
80+
width,
81+
height,
82+
disablePixelRatio: true
83+
});
84+
const msrc = imageLib.cropImage(img.src, {
85+
width: width / 2,
86+
height: height / 2,
87+
compression: 20,
88+
disablePixelRatio: true
89+
});
90+
9291
return {
93-
w: img.width,
94-
h: img.height,
95-
msrc: optimizeImage({
96-
croppedSrc,
97-
width: width / 2,
98-
height: height / 2,
99-
compression: 'q20.fgaussian4'
100-
}),
101-
src: optimizeImage({ croppedSrc, width, height, compression: 'q100' }),
102-
sourceImg: img.src
92+
src: croppedSrc,
93+
sourceImg: img.src,
94+
msrc,
95+
w: width,
96+
h: height
10397
};
10498
});
10599
this.gallery = new PhotoSwipe(pswpEle, photoSwipeUIdefault, galleryItems, options);

0 commit comments

Comments
 (0)