I want to programmatically add a class to an image if the image is portrait or landscape, the code that I have for now works fine if and the image is png or jpg but if the image source is SVG then it returns 0 for both width and height.
Is there any lightweight solution to support SVG type?
Here is my code:
<template>
<img class="header__logo" v-bind:class="[imageOrientation]" :src="Logo.primaryLogo" />
</template>
<script>
import Logo from "~/data/Logo.yml";
export default {
name: "FsHeaderLogo",
computed: {
Logo() {
return Logo;
},
imageOrientation() {
let img = new Image();
img.src = this.Logo.primaryLogo; // This will be the svg
console.log(img);
var width = img.width;
console.log(width);
var height = img.height;
console.log(height);
height = height + height;
if (width > height) {
return "header__logo--landscape";
} else {
return "header__logo--portrait";
}
}
}
};
</script>
<style lang="scss" scoped>
.header__logo {
&--portrait {
height: 100%;
max-height: 96px;
@include mediaQuery("tablet") {
height: 100%;
max-height: 144px;
}
}
&--landscape {
width: 100%;
max-width: 96px;
@include mediaQuery("tablet") {
width: 100%;
max-width: 144px;
}
}
}
</style>