Skip to content

Commit 3001667

Browse files
Fix/add auto dpr support (#138)
* Add support for dpr_auto in image components * Add a responsive condition to dpr_auto
1 parent 7706bd7 commit 3001667

File tree

4 files changed

+41
-15
lines changed

4 files changed

+41
-15
lines changed

src/components/CldImage/CldImage.vue

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { compute } from '../../mixins/compute';
44
import { register } from '../../mixins/registerTransformation'
55
import { computePlaceholder } from '../../helpers/computeOptions'
66
import { getCldPlaceholder, isCldPlaceholder } from '../../helpers/findComponent'
7-
import {
8-
ACCESSIBILITY_TRANSFORMATIONS,
9-
PLACEHOLDER_TRANSFORMATIONS,
10-
COMPONENTS,
7+
import {
8+
ACCESSIBILITY_TRANSFORMATIONS,
9+
PLACEHOLDER_TRANSFORMATIONS,
10+
COMPONENTS,
1111
LAZY_LOADING,
1212
IMAGE_CLASSES,
1313
IMAGE_WITH_PLACEHOLDER_CSS,
@@ -17,6 +17,7 @@ import {
1717
} from '../../constants';
1818
import { size } from "../../mixins/size";
1919
import { lazy } from "../../mixins/lazy";
20+
import {getDevicePixelRatio} from '../../utils/getDevicePixelRatio';
2021
2122
/**
2223
* Deliver images and specify image transformations using the cld-image (CldImage) component,
@@ -49,7 +50,7 @@ export default {
4950
},
5051
/**
5152
* **Deprecated**
52-
*
53+
*
5354
* The placeholder image to use while the image is loading. Possible values:
5455
* - `"blur"` to use blur placeholder
5556
* - `"lqip"` to use a low quality image
@@ -92,14 +93,14 @@ export default {
9293
}
9394
9495
return (
95-
<img
96+
<img
9697
attrs={this.$attrs}
9798
src={src}
9899
loading={this.hasLazyLoading ? LAZY_LOADING : null}
99100
class={imgClass}
100101
onload={this.load}
101102
style={style}
102-
/>
103+
/>
103104
)
104105
},
105106
renderComp(children) {
@@ -114,7 +115,13 @@ export default {
114115
const lazyModeInvisible = this.hasLazyLoading && !this.visible
115116
const options = this.computeURLOptions()
116117
117-
const src = responsiveModeNoSize || lazyModeInvisible ? '' : this.cloudinary.url(this.publicId, this.toSnakeCase(options))
118+
let src = responsiveModeNoSize || lazyModeInvisible ? '' : this.cloudinary.url(this.publicId, this.toSnakeCase(options));
119+
// Update dpr_auto to dpr_1.0, 2.0 etc but only for responsive mode
120+
// This matches the behaviour in other SDKs
121+
if (this.responsive) {
122+
src = src.replace(/\bdpr_(1\.0|auto)\b/g, 'dpr_' + getDevicePixelRatio(true));
123+
}
124+
118125
const cldPlaceholder = getCldPlaceholder(children)
119126
const cldPlaceholderType = cldPlaceholder ? (cldPlaceholder.componentOptions?.propsData?.type || 'blur') : ''
120127
const placeholderType = cldPlaceholderType || this.placeholder
@@ -159,6 +166,6 @@ export default {
159166
}
160167
161168
return this.renderComp(children)
162-
}
169+
}
163170
};
164171
</script>

src/utils/getDevicePixelRatio.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export function getDevicePixelRatio(roundDpr) {
2+
roundDpr = roundDpr == null ? true : roundDpr;
3+
let dpr = (typeof window !== "undefined" && window !== null ? window.devicePixelRatio : void 0) || 1;
4+
if (roundDpr) {
5+
dpr = Math.ceil(dpr);
6+
}
7+
if (dpr <= 0 || dpr === (0/0)) {
8+
dpr = 1;
9+
}
10+
let dprString = dpr.toString();
11+
if (dprString.match(/^\d+$/)) {
12+
dprString += '.0';
13+
}
14+
return dprString;
15+
}

tests/unit/Image/img.spec.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,15 @@ describe("Tests for CldImage", () => {
6666
});
6767
});
6868

69-
it("Supports transformation props", () => {
69+
it("Supports transformation props, dpr_auto should not be changed when non responsive", () => {
7070
let {wrapper, imgSrc} = mountImageComponent({
7171
cloudName: 'demo',
7272
publicId: 'face_top',
73-
effect:'sepia'
73+
effect:'sepia',
74+
dpr: 'auto'
7475
});
7576

76-
expect(imgSrc).toBe(`http://res.cloudinary.com/demo/image/upload/e_sepia/face_top`);
77+
expect(imgSrc).toBe(`http://res.cloudinary.com/demo/image/upload/dpr_auto,e_sepia/face_top`);
7778
});
7879

7980
it("Cascades non-cloudinary configuration attributes to the HTML img tag", () => {

tests/unit/Image/img_responsive_boolean.spec.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ describe("CldImage::responsive", () => {
66
it("boolean true", async () => {
77
const wrapper = mount({
88
template: `
9-
<cld-image cloudName="demo" publicId="face_top" :responsive="responsive" />
9+
<cld-image cloudName="demo" publicId="face_top" :responsive="responsive" :dpr="dpr" />
1010
`,
1111
components: { CldImage },
1212
data() {
13-
return { responsive: true };
13+
return {
14+
responsive: true,
15+
dpr: 'auto'
16+
};
1417
}
1518
});
1619
const image = wrapper.find('img');
@@ -21,7 +24,7 @@ describe("CldImage::responsive", () => {
2124
await Vue.nextTick();
2225

2326
expect(image.attributes("src")).toEqual(
24-
`http://res.cloudinary.com/demo/image/upload/c_scale,w_100/face_top`
27+
`http://res.cloudinary.com/demo/image/upload/c_scale,dpr_1.0,w_100/face_top`
2528
);
2629
});
2730

0 commit comments

Comments
 (0)