1

I want to check programmatically if image exists on the given URL. I am trying to get the image size and it would be great if I can store the response into variable by using getimagesize("http://my.cdn.com/5147.jpg").

If image exists I get the image size, but if it doesn't I get an error failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found. How can I get something like boolean false if it failed to open stream?

harunB10
  • 4,823
  • 15
  • 63
  • 107
  • You could access the page URL, download it, then scan that variable for attribs – clearshot66 Aug 29 '17 at 16:16
  • Actually I need to render all the images on the page and I do not want to see broken due to non existing images. That's why I want to check for image existence in my Controller. – harunB10 Aug 29 '17 at 16:19
  • _"How can I get something like boolean false if it failed to open stream?"_ - you got that already, because that _is_ what getimagesize returns when it fails for whatever reason. So you either just suppress the warning (`@` operator; please go research yourself why that might not be the best option), or you replace it with something that makes the HTTP request first (so that you can actually catch such errors properly), and then feed the read data to getimagesize some other way (like f.e. via a temp file or a in-memory stream. – CBroe Aug 29 '17 at 16:23

0 Answers0