Api image link how to use?

60 Replies
snxxwyy
snxxwyy4mo ago
You’d put the link it gave you inside of src in an image tag. e.g.
<img src="https://static.wikia.nocookie.net/dragonball/images/4/42/AboSGAHFR.png/revision/latest/scale-to-width-down/350?cb=20100225121539" alt="alt text here" />
<img src="https://static.wikia.nocookie.net/dragonball/images/4/42/AboSGAHFR.png/revision/latest/scale-to-width-down/350?cb=20100225121539" alt="alt text here" />
lanszelot
lanszelot4mo ago
this is not working I have to delete the end till the ".png" only that time working as image src but because this is api I cannot delete
snxxwyy
snxxwyy4mo ago
hm, i've never encountered this issue before so i might not be of use, sorry, perhaps someone else will be able to help
snxxwyy
snxxwyy4mo ago
what's the reason you can't remove the end part up to .png? if you're simply looking to use this one image, i don't see any harm in doing so? Unless you're fetching the data from the api and implementing the link through js, in which case i'm sure you can slice off the unnecessary text?
lanszelot
lanszelot4mo ago
This is api, you request the link. There are more than 1500 links .....
snxxwyy
snxxwyy4mo ago
could you not store the links in an array or perhaps better a file and use for each to filter out the text you don't want?
lanszelot
lanszelot4mo ago
store all links make the site extremly slow. about 1 minutes load time every time
snxxwyy
snxxwyy4mo ago
i'm pretty sure you can cap the amount of data fetched no?
lanszelot
lanszelot4mo ago
ok, but how? this was my question, how can I use that link as an image on my website?
snxxwyy
snxxwyy4mo ago
i'm not sure why the full link doesn't work myself unfortunately, so i don't think i can help here, sorry
lanszelot
lanszelot4mo ago
did you watched my codepen link?
snxxwyy
snxxwyy4mo ago
yes i've looked at it but it's what i've already suggested, if i can't understand why the link isn't working then i really can't be of use unfortunately, perhaps it's something to do with the api site
lanszelot
lanszelot4mo ago
Here is the full api request: https://codepen.io/lanszelot/pen/dyBozrK
missymae
missymae4mo ago
It's like @snxxwyy said, you can use a pattern to remove the part of the links you don't need. For example in your codepen, change to
js
document.getElementById("lista").innerHTML = `<img src="`+charNames[1].image[0].default.split('png')[0]+'png'+`">`;
js
document.getElementById("lista").innerHTML = `<img src="`+charNames[1].image[0].default.split('png')[0]+'png'+`">`;
and the image appears. You can use RegEx or make a cleaner method to do the same thing.
MarkBoots
MarkBoots4mo ago
const url = "https://static.wikia.nocookie.net/dragonball/images/4/42/AboSGAHFR.png/revision/latest/scale-to-width-down/350?cb=20100225121539";
const cleanedUrl = url.replace(/(\.png).*/, '$1');

console.log(cleanedUrl)
// "https://static.wikia.nocookie.net/dragonball/images/4/42/AboSGAHFR.png"
const url = "https://static.wikia.nocookie.net/dragonball/images/4/42/AboSGAHFR.png/revision/latest/scale-to-width-down/350?cb=20100225121539";
const cleanedUrl = url.replace(/(\.png).*/, '$1');

console.log(cleanedUrl)
// "https://static.wikia.nocookie.net/dragonball/images/4/42/AboSGAHFR.png"
ἔρως
ἔρως4mo ago
this is a codepen issue copy-paste your code into google, in a console (but replace document.getElementById("lista") with document.body)
ἔρως
ἔρως4mo ago
you will see that the image loads normally
No description
ἔρως
ἔρως4mo ago
by the way, instead of setting the inner html, create an actual element:
let img = document.createElement('img');
img.src = charNames[1].image[0].default;
document.body.appendChild(img);
let img = document.createElement('img');
img.src = charNames[1].image[0].default;
document.body.appendChild(img);
lanszelot
lanszelot4mo ago
First of all thanks for help. These are not good solutions. There are png, PNG, jpg, JPG, files It is not codepen issue.
MarkBoots
MarkBoots4mo ago
i never checked it myself, maybe you know. is there a noticable difference (or caviats) in using the Image() constructor
const newImage = new Image();
newImage.src = "someImg.jpg";
const newImage = new Image();
newImage.src = "someImg.jpg";
then you can also use the onload functions etc...
lanszelot
lanszelot4mo ago
Please watch the codepen link after answer This is an api request. The link is not "someImage.jpg"
ἔρως
ἔρως4mo ago
it is a codepen issue: the image doesn't load in iframes
lanszelot
lanszelot4mo ago
Please do not write fake Copy paste and try it on your machine Not a codepen issue
ἔρως
ἔρως4mo ago
No description
ἔρως
ἔρως4mo ago
here's evidence that it works
ἔρως
ἔρως4mo ago
try it yourself
lanszelot
lanszelot4mo ago
On my machine, on servers, not working
ἔρως
ἔρως4mo ago
im out of ideas then
MarkBoots
MarkBoots4mo ago
No description
ἔρως
ἔρως4mo ago
the stuff after the .png make the image shrink to 350px or something
lanszelot
lanszelot4mo ago
I wrote, this is not good. Links are not only png files
MarkBoots
MarkBoots4mo ago
i show an example. if it are different image files, then check for them all
lanszelot
lanszelot4mo ago
How? How should "for loop" know which is png which is jpg which is PNG and which is JPG?
MarkBoots
MarkBoots4mo ago
image.src = charNames[1].image[0].default.replace(/(\.(?:png|jpe?g|jxl|webp|avif|svgz?|gif)).*$/i , '$1');
image.src = charNames[1].image[0].default.replace(/(\.(?:png|jpe?g|jxl|webp|avif|svgz?|gif)).*$/i , '$1');
ἔρως
ἔρως4mo ago
/(\.(?:png|jpe?g|jxl|webp|avif|svgz?|gif)).*$/i
MarkBoots
MarkBoots4mo ago
even nicer yes
lanszelot
lanszelot4mo ago
I don't understand. What this line do?
ἔρως
ἔρως4mo ago
match the pattern, then replace what matched with the replacement text https://regex101.com/r/HXtnXV/1
MarkBoots
MarkBoots4mo ago
so it looks for one of those extenstions and drops everything behind it
ἔρως
ἔρως4mo ago
it's more nuanced than that, but yes
lanszelot
lanszelot4mo ago
I see.
MarkBoots
MarkBoots4mo ago
i know, keep it understandable 😉
lanszelot
lanszelot4mo ago
Thank you so much I will try it tomorrow. Thank you so much for everyone
lanszelot
lanszelot4mo ago
Hello, I copy paste your code and something is not right. Did not show the image at all. https://codepen.io/lanszelot/pen/dyBozrK
lanszelot
lanszelot4mo ago
I got it. The first code working, the second not. I tried: if I use the full link in html directly it is show the image. But when I add the link with javascript it is not working. I have no idea why. There are the samples in the codepen
MarkBoots
MarkBoots4mo ago
No description
MarkBoots
MarkBoots4mo ago
these are 2 options you can use, with innerhtml or with a create elememt
// clean up the url
const characterImageUrl = charNames[1].image[0].default.replace(/(\.(?:png|jpe?g|jxl|webp|avif|svgz?|gif)).*$/i , '$1');
const characterName = charNames[1].name;

// add it with innerHTML and a template-string
document.getElementById("lista1").innerHTML = `
<img src="${characterImageUrl}" alt="${characterName}">
`
// add it with a createElement
const imageEl = document.createElement("img");
imageEl.src = characterImageUrl;
imageEl.alt = characterName;
document.getElementById("lista2").append(imageEl);
// clean up the url
const characterImageUrl = charNames[1].image[0].default.replace(/(\.(?:png|jpe?g|jxl|webp|avif|svgz?|gif)).*$/i , '$1');
const characterName = charNames[1].name;

// add it with innerHTML and a template-string
document.getElementById("lista1").innerHTML = `
<img src="${characterImageUrl}" alt="${characterName}">
`
// add it with a createElement
const imageEl = document.createElement("img");
imageEl.src = characterImageUrl;
imageEl.alt = characterName;
document.getElementById("lista2").append(imageEl);
lanszelot
lanszelot4mo ago
First of all, thanks for help. How I wrote, no problems with codepen at all. The servers do the same. I wrote: " if I use the full link in html directly it is show the image. But when I add the link with javascript it is not working. I have no idea why. There are the samples in the codepen" JavaScript couse the problem. So somehow JS have to fix it. Not cut the code. I understand it is working cut off, but this is just a cover the problem solution. Just try it: add the full link with JS to the html, and you will see it is not codepen problem, it is JS problem. I don't know how to fix it . The cutting process make the site extremely slow. This is why not good solution.
MarkBoots
MarkBoots4mo ago
okay, then it's not a codepen issue, but it is also not a js issue. in that case it might be the source having some limitiations the api returns an url with a ?cb parameter. when you copy that url into the browser, it is showing the placeholder image. so removing the cb from the url probably will work
No description
MarkBoots
MarkBoots4mo ago
it tried one and other, but i don't think this api is the most useful. it is doing some things serverside and even not all image excist anymore also, with you api query , you are retrieving 1522 characters. that will slow it down as well https://codepen.io/MarkBoots/pen/KKjpoxr?editors=1010 i looked for more documentation or even a github repository to find if there are know issues. but the creator of this api didnt make it public https://github.com/andrejaeger
GitHub
andrejaeger - Overview
work for ⭕⭕⭕⭕ as 💻. GitHub is where andrejaeger builds software.
lanszelot
lanszelot4mo ago
Codepen and all online emulator use JavaScript, and this is why not working there. Just try what I said, insert the src with js, and not working. Directly write in html it is working. Api return the url, but I cannot edit the api. So it give me what it give me. Doesn't matter I delete only ?cb or all end after file extension the site get extremely slow. I know it is api site issue, give me wrong link, but I thought it could be fix it . But looks like cannot. I tried to store the links, in array, but no change, site slow down. I stored in json file, but same slow down. Only when I store the link in json, and download all the images and keep next to the html file not slow down.
MarkBoots
MarkBoots4mo ago
its the same on my local machine. really trust me, this api is no good
lanszelot
lanszelot4mo ago
Yes, the api not good. Right. But this is the only api where all DB characters are in So no other options
MarkBoots
MarkBoots4mo ago
yea, i see your frustration. but i can not make that better. If there is none, you'll have to create it yourself
lanszelot
lanszelot4mo ago
Only problem with store images and links is : there are links which have no image. With api gives me blank image. But with stored images and links drop error and not working If there will be a way to found all broken links I can create images. Anyway, thank you so much for help.
MarkBoots
MarkBoots4mo ago
im not behind my pc anymore, but if you check my pen, you can see all the broken ones. the name of the character is in the alt attribute (you could list them out programmatically, but im not able to right now)
MarkBoots
MarkBoots4mo ago
i just notice that these url's belong to https://dragonball.fandom.com/wiki/ the creator of the api probably manually sourced all the image urls to his project. but, if something changes on the wikipages, his api will be outdated immediately. when looking at his personal page, it looks like a project from 2022 and he has not made the repository public. So i suspect he is not maintaining it any longer. im looking if there is way to collect the data yourself from the wikipages, but probably we'll need to scrape each and every page (and hope all have the same format)
lanszelot
lanszelot4mo ago
Thank you so much Your api request working fast. So my request is faulty this is why get slow. I have to study more about fetch.
Want results from more Discord servers?
Add your server