Im trying to do a google search by image url using this link, it works for most websites(?), but not all
window.open("https://www.google.com/searchbyimage?image_url=" + image_url), 'popUpGoogle', 'height=400, width=600, left=10, top=1');
Here’s the issue, lets take this picture for example https://vladimirkhil.com/siserver/3/packages/0t%2BefNNidBXgG1am3LaK5nczmak%3D_kLhfz3q9duZyqOT%27x9qDZw%3D%3D/Images/quake.jpg
the link would be
https://www.google.com/searchbyimage?image_url=https://vladimirkhil.com/siserver/3/packages/0t%2BefNNidBXgG1am3LaK5nczmak%3D_kLhfz3q9duZyqOT%27x9qDZw%3D%3D/Images/quake.jpg
But when I open it I get :
The URL doesn’t refer to an image or the image is not publicly accessible.
However, if I open the image in a new tab, and then do a google search manually by right clicking it works, but whats more weird is that it also works if I go to a “Search by Image” on google and past the exact same image link there
Any ideas what this could be related to? The URL clearly refers to an image and it is publicly accessible, is it something with the URL encoding? It looks fine to me, also it works in 2nd method that I described so I’m really lost
Advertisement
Answer
You have to encode the url if you pass it as argument to another url.
Your base url is:
https://www.google.com/searchbyimage?image_url=
Your argument url is
https://vladimirkhil.com/siserver/3/packages/0t%2BefNNidBXgG1am3LaK5nczmak%3D_kLhfz3q9duZyqOT%27x9qDZw%3D%3D/Images/quake.jpg
Your encoded (encodeURI, escapes ‘%’ signs) argument url is
https://vladimirkhil.com/siserver/3/packages/0t%252BefNNidBXgG1am3LaK5nczmak%253D_kLhfz3q9duZyqOT%2527x9qDZw%253D%253D/Images/quake.jpg
Your encoded (encodeURIComponent, escapes ‘%’ and ‘/’ in your case) argument url is
https%3A%2F%2Fvladimirkhil.com%2Fsiserver%2F3%2Fpackages%2F0t%252BefNNidBXgG1am3LaK5nczmak%253D_kLhfz3q9duZyqOT%2527x9qDZw%253D%253D%2FImages%2Fquake.jpg
To actually do the encode call the javascript function encodeURI
MDN with the url to encode. The encoding process will escape chars that have a special function in urls (like the % sign in your case)
Your final search url should look like this
https://www.google.com/searchbyimage?image_url=https://vladimirkhil.com/siserver/3/packages/0t%252BefNNidBXgG1am3LaK5nczmak%253D_kLhfz3q9duZyqOT%2527x9qDZw%253D%253D/Images/quake.jpg
Tested. And worked. At least for me 🙂
EDIT:
Might be a better option to use encodeURIComponent
MDN over encodeURI