John Davidson

Check IMG URL status (PHP get_headers)

0 comments
Message:


In a nut-shell, I am trying to display feeds from other sites, that sometimes have IMG elements. Ideally, these IMG should be loaded by an external proxy, such as Aratech or Weserv. In my code below, I am trying to use Weserv. Then, if this is not working, I'd like to use Aratech. If this is also not working, I want to use the original IMG SRC. Finally, if this fails with any other status code then 200, I want to display another IMG.


I believe the code, as bad as it looks, will explain it better. This is what I have:


$proximage1 = rtrim(preg_replace ('/https:\/\/|http:\/\//','https://images.weserv.nl/?w=800&output=webp&url=',$img->getAttribute('src'),1),'/');  //proxied img
$arrayproximage1 = @get_headers($proximage1);
$stringproximage1 = $arrayproximage1[0];
$proximage1200 = strpos($stringproximage1, "200");


$proximage2 = rtrim(preg_replace ('/https:\/\/|http:\/\//','https://imagex.aratech.co/?w=800&q=75&il&output=webp&url=',$img->getAttribute('src'),1),'/'); //proxied img
$arrayproximage2 = @get_headers($proximage2);
$stringproximage2 = $arrayproximage2[0];
$proximage2200 = strpos($stringproximage2, "200");

$arrayproximage3 = @get_headers($img->getAttribute('src')); //original img headers
$stringproximage3 = $arrayproximage3[0];
$proximage3200 = strpos($stringproximage3, "200");

if (($proximage1200 !== false) && ($proximage2200 === false) && ($proximage3200 === false)) {
$img->setAttribute( 'src', $proximage1 );
}
if (($proximage1200 === false) && ($proximage2200 !== false) && ($proximage3200 === false)) {
$img->setAttribute( 'src', $proximage2 );
}
if (($proximage1200 === false) && ($proximage2200 === false) && ($proximage3200 !== false)) {
$img->setAttribute( 'src', $img->getAttribute('src') ); // this is the original IMG SRC
}
if (($proximage1200 === false) && ($proximage2200 === false) && ($proximage3200 === false)) {
$img->setAttribute( 'src', 'https://some-url-that-can-finally-load-an-img' );
}

What happens is that the page loads the original IMG SRC, skipping somehow the proxified images.weserv.nl and imagex.aratech.co. Any ideas, any way to fix this?


Again, my goal is to use Weserv, then if this doesn't URL doesn't display a 200 status use the Aratech service, then if this fails the original IMG SRC and finally a static IMG. All depends on the 200 status.



My Answer:
&url=', $img_url), '/');
$proximage2 = rtrim(preg_replace ('/https:\/\/|http:\/\//','https://aratech.io/?url=', $img_url), '/');

$headers = get_headers($proximage1);

if(strpos($headers[0], '200') !== false) {
$final_img = $proximage1;
} else {
$headers = get_headers($proximage2);

if(strpos($headers[0], '200') !== false) {
$final_img = $proximage2;
} else {
$final_img = $img_url;
}
}

echo '';

Rate this post

5 of 5 based on 8593 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us