Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions src/components/CldImage/CldImage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { compute } from '../../mixins/compute';
import { register } from '../../mixins/registerTransformation'
import { computePlaceholder } from '../../helpers/computeOptions'
import { getCldPlaceholder, isCldPlaceholder } from '../../helpers/findComponent'
import {
ACCESSIBILITY_TRANSFORMATIONS,
PLACEHOLDER_TRANSFORMATIONS,
COMPONENTS,
import {
ACCESSIBILITY_TRANSFORMATIONS,
PLACEHOLDER_TRANSFORMATIONS,
COMPONENTS,
LAZY_LOADING,
IMAGE_CLASSES,
IMAGE_WITH_PLACEHOLDER_CSS,
Expand All @@ -17,6 +17,7 @@ import {
} from '../../constants';
import { size } from "../../mixins/size";
import { lazy } from "../../mixins/lazy";
import {getDevicePixelRatio} from '../../utils/getDevicePixelRatio';

/**
* Deliver images and specify image transformations using the cld-image (CldImage) component,
Expand Down Expand Up @@ -49,7 +50,7 @@ export default {
},
/**
* **Deprecated**
*
*
* The placeholder image to use while the image is loading. Possible values:
* - `"blur"` to use blur placeholder
* - `"lqip"` to use a low quality image
Expand Down Expand Up @@ -92,14 +93,14 @@ export default {
}

return (
<img
<img
attrs={this.$attrs}
src={src}
loading={this.hasLazyLoading ? LAZY_LOADING : null}
class={imgClass}
onload={this.load}
style={style}
/>
/>
)
},
renderComp(children) {
Expand All @@ -114,7 +115,13 @@ export default {
const lazyModeInvisible = this.hasLazyLoading && !this.visible
const options = this.computeURLOptions()

const src = responsiveModeNoSize || lazyModeInvisible ? '' : this.cloudinary.url(this.publicId, this.toSnakeCase(options))
let src = responsiveModeNoSize || lazyModeInvisible ? '' : this.cloudinary.url(this.publicId, this.toSnakeCase(options));
// Update dpr_auto to dpr_1.0, 2.0 etc but only for responsive mode
// This matches the behaviour in other SDKs
if (this.responsive) {
src = src.replace(/\bdpr_(1\.0|auto)\b/g, 'dpr_' + getDevicePixelRatio(true));
}

const cldPlaceholder = getCldPlaceholder(children)
const cldPlaceholderType = cldPlaceholder ? (cldPlaceholder.componentOptions?.propsData?.type || 'blur') : ''
const placeholderType = cldPlaceholderType || this.placeholder
Expand Down Expand Up @@ -159,6 +166,6 @@ export default {
}

return this.renderComp(children)
}
}
};
</script>
15 changes: 15 additions & 0 deletions src/utils/getDevicePixelRatio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function getDevicePixelRatio(roundDpr) {
roundDpr = roundDpr == null ? true : roundDpr;
let dpr = (typeof window !== "undefined" && window !== null ? window.devicePixelRatio : void 0) || 1;
if (roundDpr) {
dpr = Math.ceil(dpr);
}
if (dpr <= 0 || dpr === (0/0)) {
dpr = 1;
}
let dprString = dpr.toString();
if (dprString.match(/^\d+$/)) {
dprString += '.0';
}
return dprString;
}
7 changes: 4 additions & 3 deletions tests/unit/Image/img.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ describe("Tests for CldImage", () => {
});
});

it("Supports transformation props", () => {
it("Supports transformation props, dpr_auto should not be changed when non responsive", () => {
let {wrapper, imgSrc} = mountImageComponent({
cloudName: 'demo',
publicId: 'face_top',
effect:'sepia'
effect:'sepia',
dpr: 'auto'
});

expect(imgSrc).toBe(`http://res.cloudinary.com/demo/image/upload/e_sepia/face_top`);
expect(imgSrc).toBe(`http://res.cloudinary.com/demo/image/upload/dpr_auto,e_sepia/face_top`);
});

it("Cascades non-cloudinary configuration attributes to the HTML img tag", () => {
Expand Down
9 changes: 6 additions & 3 deletions tests/unit/Image/img_responsive_boolean.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ describe("CldImage::responsive", () => {
it("boolean true", async () => {
const wrapper = mount({
template: `
<cld-image cloudName="demo" publicId="face_top" :responsive="responsive" />
<cld-image cloudName="demo" publicId="face_top" :responsive="responsive" :dpr="dpr" />
`,
components: { CldImage },
data() {
return { responsive: true };
return {
responsive: true,
dpr: 'auto'
};
}
});
const image = wrapper.find('img');
Expand All @@ -21,7 +24,7 @@ describe("CldImage::responsive", () => {
await Vue.nextTick();

expect(image.attributes("src")).toEqual(
`http://res.cloudinary.com/demo/image/upload/c_scale,w_100/face_top`
`http://res.cloudinary.com/demo/image/upload/c_scale,dpr_1.0,w_100/face_top`
);
});

Expand Down