javascript - Change dimensions of images from Twitch API
问题描述
I'm making a request to the Twitch API to get a list of top games and display their box art, but the image will only show if I change the width
and the height
value in the provided link. How can I change these value in ejs or should I change the values before I provide the context?
<% data.forEach(game => { %>
<img src= <%= game.box_art_url %> alt="">
<%})%>
Example API response
{
"id": "21779",
"name": "League of Legends",
"box_art_url": "https://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-{width}x{height}.jpg"
}
解决方案
In ejs
replace {width}
and {height}
with your own dimensions - example:
<% data.forEach(game => { %>
<img src= <%= game.box_art_url.replace('{width}', '138').replace('{height}', '190') %> alt="">
<%})%>
This will output:
<img src="https://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-138x190.jpg" alt="">
推荐阅读
- python - 将列表的 pandas 列转换为矩阵表示(一种热编码)
- java - 如何使用 Primefaces 在 oncomplete 中防止 Wizard next() 执行?
- python - 我如何在星号行之前放置星号的数量
- c - 文本文件中矩阵的维度
- react-native - 如何在 React Native 中的 Button Click onPress 上检索文本输入输入值?
- python - Unable to fetch HREF using beautiful soup
- javascript - 元素/对象的多个属性
- css - Sass – 连接到@at-root 选择器
- html - 在 vue.js 中;用 textarea 的“readonly”属性在 true 或 false 中切换是没有用的;
- php - Laravel 在 08:00、12:00、16:00 和 20:00、22:00 每小时运行一次任务