首页 > 解决方案 > 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"
        }

标签: javascriptnode.jsapiexpressejs

解决方案


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="">

推荐阅读