Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

image doesn't show on web #903

Closed
fmchan opened this issue Apr 21, 2021 · 18 comments
Closed

image doesn't show on web #903

fmchan opened this issue Apr 21, 2021 · 18 comments
Labels

Comments

@fmchan
Copy link

fmchan commented Apr 21, 2021

i fetch posts by getMediasByTag and use to list the post image. it works perfect all the time but today i run it and images are not shown. when i paste the url to browser, the image works. it's a weird case and i wish to find out solution. thank you

@rikless
Copy link

rikless commented Apr 21, 2021

@Gugiman
Copy link

Gugiman commented Apr 21, 2021

Quick solution:
Load the image via cUrl and use it base64 encoded.
Like this:
<img src="data:image/png;base64, {BASE64_ENCODED_IMG}" >

@veuxx veuxx mentioned this issue Apr 23, 2021
@adrianosmateus
Copy link

Got the same problem here. Did the copy workaround until the plugin bypasses it.

@restyler
Copy link

restyler commented Apr 27, 2021

The same issue was reported recently on proxified fork of this scraper ( https://github.com/restyler/instagram-php-scraper ).

The StackOverflow link above is not exactly correct, it mentions different header related to iframes.

Instagram now sets cross-origin-resource-policy: same-origin if it doesn't like the "referer" which your browser sends to cdninstagram domain when loading image.

Base64 embedding suggested above will make your html page very heavy, especially if you render image feed with bigger thumbnails. It will also block the execution of PHP while it downloads each image, so if you have 10 images and downloading each takes 1 second, your page open time will become 10 seconds slower.
Better approach might be to create a simple image proxy which will download the image from instagram server to your server, and then output it to the browser. This way the headers issue will be mitigated.

Here is an example of CloudFlare image proxy:

https://gist.github.com/restyler/6c51e3ad20d7596e799d76e87cf93236

Not that efficient, but easier to setup, PHP implementation:

https://github.com/restyler/inwidget/blob/master/imgproxy.php

It might be a good idea to add additional caching layer on the proxy to reduce the amount of duplicate image requests to instagram cdn servers.

When you have your image proxy running, you just need to replace all instagram image srcs to the proxified versions.

@sagitaire123
Copy link

Same issue here, @raiym any update planned to work around this ?

@veuxx
Copy link

veuxx commented Apr 29, 2021

i'm usando @copy of php and hosting the images on my own server, in an improvised cache system. So I avoid multiple calls to the instagram server

@sagitaire123
Copy link

Quick solution:
Load the image via cUrl and use it base64 encoded.
Like this:
<img src="data:image/png;base64, {BASE64_ENCODED_IMG}" >

@Gugiman How to implement this on the library please ?

@veuxx
Copy link

veuxx commented Apr 29, 2021

Solução rápida:
carregue a imagem via cUrl e use-a codificada em base64.
Como isso:
<img src="data:image/png;base64, {BASE64_ENCODED_IMG}" >

@Gugiman Como implementar isso na biblioteca, por favor?

After obtaining the image link, simply encode it in base64.
Like:

$image = 'http://img.instagram.com/profile/myphoto.jpg';
$imageData = base64_encode(file_get_contents($image));

$src = 'data: '.mime_content_type($image).';base64,'.$imageData;

echo '<img src="'.$src.'">';

You can create a function, for facility...

function encodeimg($url){

	$imageData = base64_encode(file_get_contents($url));
	$src = 'data: '.mime_content_type($url).';base64,'.$imageData;
	return $src;
}

Use like:
<img src="<?=encodeimg($string_of_link_img);?>"

@Gugiman
Copy link

Gugiman commented Apr 30, 2021

@sagitaire123 Solution from @veuxx is indeed easier by using file_get_contents instead of cUrl

@sagitaire123
Copy link

@Gugiman @veuxx Perfect thank you, it works

@Frostbourn
Copy link

Any JS solution?

@Gugiman
Copy link

Gugiman commented May 6, 2021

Maybe something like this?
https://medium.com/front-end-weekly/fetching-images-with-the-fetch-api-fb8761ed27b2

But I'm not quite sure if js within a browser will be check for CORS again.

@esvikas
Copy link

esvikas commented May 17, 2021

And what is the solution for the displaying the video from Instagram?

@fmchan
Copy link
Author

fmchan commented Jun 15, 2021

Solução rápida:
carregue a imagem via cUrl e use-a codificada em base64.
Como isso:
<img src="data:image/png;base64, {BASE64_ENCODED_IMG}" >

@Gugiman Como implementar isso na biblioteca, por favor?

After obtaining the image link, simply encode it in base64.
Like:

$image = 'http://img.instagram.com/profile/myphoto.jpg';
$imageData = base64_encode(file_get_contents($image));

$src = 'data: '.mime_content_type($image).';base64,'.$imageData;

echo '<img src="'.$src.'">';

You can create a function, for facility...

function encodeimg($url){

	$imageData = base64_encode(file_get_contents($url));
	$src = 'data: '.mime_content_type($url).';base64,'.$imageData;
	return $src;
}

Use like:
<img src="<?=encodeimg($string_of_link_img);?>"

it works last month but this month it doesn't work anymore

@shammarafzal
Copy link

Solução rápida:
carregue a imagem via cUrl e use-a codificada em base64.
Como isso:
<img src="data:image/png;base64, {BASE64_ENCODED_IMG}" >

@Gugiman Como implementar isso na biblioteca, por favor?

After obtaining the image link, simply encode it in base64.
Like:

$image = 'http://img.instagram.com/profile/myphoto.jpg';
$imageData = base64_encode(file_get_contents($image));

$src = 'data: '.mime_content_type($image).';base64,'.$imageData;

echo '<img src="'.$src.'">';

You can create a function, for facility...

function encodeimg($url){

	$imageData = base64_encode(file_get_contents($url));
	$src = 'data: '.mime_content_type($url).';base64,'.$imageData;
	return $src;
}

Use like:
<img src="<?=encodeimg($string_of_link_img);?>"

Not Working!!!!!

@veuxx
Copy link

veuxx commented Jun 22, 2021

Here it still works as normal... Instagram must be blocking the IP of their servers. Test it on another server or use a proxy!
@shammarafzal @fmchan

@evolross
Copy link

evolross commented Aug 27, 2021

Won't the IP address of any proxy server you set up eventually get blocked? We were doing some scraping and got our server IP blocked. Then we started using Zyte which works for the scraping, but then the URLs are now blocked due to this ERR_BLOCKED_BY_RESPONSE error on each user's browser.

I understand setting up a simple proxy bypasses the headers, but unless that proxy also uses a varying IP pool, won't it just get blocked too? Once IG sees a ton of requests coming from the same server IP? Similar to the scraping block we had on our server IP?

It seems like any simple server image proxy must make its requests through another varying IP pool proxy, same when used when scraping, for it to ultimately stay working.

@stale
Copy link

stale bot commented Mar 2, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Mar 2, 2022
@stale stale bot closed this as completed Apr 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests