2

I am working on a project built in Laravel 5.8 where i need to look through 3 different image Sources for an img tag. For example first I will put src of "https://yw-web.yoworld.com/cdn/items/12/73/127305/127305.png" if this does not return an image then i will provide second address like "https://yw-web.yoworld.com/cdn/items/12/73/127305/127305_130_100.gif" if still no image found I will try third address like "https://yw-web.yoworld.com/cdn/items/12/73/127305/127305_60_60.gif" and if all three of these does not work then this image will be loaded "https://yoworld.info/assets/image_not_found.jpg"

recently I have implemented this snippet which seems to work fine but it is taking a lot of time to execute.

HTML:
<img class="my-auto mx-auto" src="{{$img}}">


PHP:
$src= 'https://yw-web.yoworld.com/cdn/items/12/73/127305/127305';
if(@getimagesize($src.'.png')){
$img=$src.'.png';
}
elseif(@getimagesize($src.'_130_100.gif')){
$img=$src.'_130_100.gif';
}
elseif(@getimagesize($src.'_60_60.gif')){
$img=$src.'_60_60.gif';
}
else{
$img=url('img/image_not_found.jpg');
}

file_exists() does not work because I am accessing these images from another domain and cURL is also taking a lot of time.

Kindly suggest me an approach which takes less time. I can also use Javascript it provides a solution.

2 Answers2

1

try this

$file = 'http://www.someAddress.com/somefile.jpg';
$file_headers = @get_headers($file);
if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $exists = false;
}
else {
    $exists = true;
}
Reza sh
  • 787
  • 10
  • 20
  • Taking more than 1 minute for 12 Images. – Hamza Saeed Jan 12 '20 at 13:58
  • @HamzaSaeed the CURL take time too much, you can do it in jquery, after page load you can run this function in jquery and get the image `this is my opinion, it maybe wrong` – Reza sh Jan 13 '20 at 05:47
0

Look through this answer:

Check if image exists on server using JavaScript?

Maybe it help.

Igris
  • 76
  • 1
  • 6