question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to locally save the external images from an API?

See original GitHub issue

I’m doing some research and making some demo using react-static.

I found a problem with the images, which is external images from the API instead of the images in CSS or local image for the UI.

For example, I’ve developed an API with Wordpress basic, there’re a lot of posts are returned by that API, sometimes there are a lot of images are returned as “Array” like the following JSON.

{
  "title": "testing title...",
  "content": "testing content...",
  "images": [
    "http://testing.abc.com/images/upload/testing1.jpg",
    "http://testing.abc.com/images/upload/testing2.jpg",
    "http://testing.abc.com/images/upload/testing3.jpg"
  ]
}

Is there any way to download the image and put them into the the <img tag as a relative path instead of absolute path, when we build a static website. So the result should be …

<img src="./public/images/upload/testing1.jpg"/>
or (based on the page url route)
<img src="../../public/images/upload/testing1.jpg"/>

instead of

<img src="http://testing.abc.com/images/upload/testing1.jpg"/>

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
Pixelatexcommented, Sep 4, 2019

Right… so thinking a bit on how you could do this flow in your static config.

  1. Fetch the posts from your api.
  2. Iterate over the posts
  3. Fetch the image(s) for that post and stream the result to a folder (fssync or something).
  4. Pass the resulting file path, .e.g. images/file1.jpg as a prop to the posts route.
  5. Profit!
0reactions
sytabaresacommented, Dec 5, 2019

This process is a genuine candidate to be implemented in a plugin, related to #1130 issue, maybe a component that automatically download images in a static directory depending of they use. Or an official guide to use webpack + React Static to do this with an api.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Save an image to localStorage – JavaScript Tutorial - YouTube
Source code: https://openjavascript.info/2022/10/11/how-to- save -and-retrieve- images -from-localstorage/⚡ Looking for high-performance, ...
Read more >
Saving images on an external server - Stack Overflow
Create a WebAPI (REST API) on the 2nd server you want images to be saved on. Send the images in Base64EncodedString to the...
Read more >
8 - Save and set an external link image programatically
I'm wondering how do Drupal save the image from an external link. I use the following code. $imageUri = 'https://api.statvoo.com/favicon/?url= ...
Read more >
Saving an Image from URL in PHP - GeeksforGeeks
It's easy to go to the page and use right click button and save the image. But what if you wanted to do...
Read more >
Programmatically Uploading Images, Videos, and Other Files
Learn how to upload images, videos, and other files with only a line or two of code - with cloud storage, CDN delivery,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found