I have this: if (getimagesize($x->image_url) !== false) { echo "img"; } PHP: The problem is that if the image url is badly formatted I am getting this: Warning: getimagesize(http:///images/crap.jpg) [function.getimagesize]: failed to open stream: operation failed in bla... Code (markup): Can you suggest a better code? Thanks!
It is read from a products feed. Some URLs are valid, some are not. Some have bad formating, some return a 404. I need to check for all these errors..
Have a look at this page where someone had a similar problem. http://stackoverflow.com/questions/2280394/how-can-i-check-if-a-url-exists-via-php
I can't fix the missing image, the problem is from an advertisers feed. All I can do is to prevent showing a missing image.
http:///images/crap.jpg Code (markup): is incorrectly formated as there is no domain. so either add your domain http://yourdomain.com/images/crap.jpg Code (markup): or remove the http:// /images/crap.jpg Code (markup):
So you think supressing notices, warnings and errors is a good tip for developing? Where did you learn that?
Well there are various ways to fix your error, but I require a context in No, it's never a good tip, as much as enabling notices and warnings in production, but in this issue, it would solve the problem user has encored.
it wouldn't solve the problem. It would only hide the problem. What you need to do is to add some code around your if condition and check what $x->image_url actually contains. And based on the content you either run the if or not. You could also wrap the whole thing in a try catch code.
I use this on my wordpress sites to show product datafeeds (from CJ). I use curl to go get the image - if curl fails then I use a local image. I cache the results of this query for a couple of days, since all these requests to external sites can slow yours down. Hope it helps, I struggled with this for some time. productImageCheck simply turns the check on or off. // begin check for broken images get_option('productImageCheck'); $productImage = $CurrentProduct->{'image-url'}; if (empty($productImage)) { $productImage = CJPLUGINURL . "/images/noimage.gif"; } elseif(get_option('productImageCheck')) { $chProductImage = curl_init(); curl_setopt($chProductImage, CURLOPT_URL,$productImage); // don't download content curl_setopt($chProductImage, CURLOPT_NOBODY, 1); curl_setopt($chProductImage, CURLOPT_FAILONERROR, 1); curl_setopt($chProductImage, CURLOPT_RETURNTRANSFER, 1); if(curl_exec($chProductImage )!==FALSE) { } else { $productImage = CJPLUGINURL . "/images/noimage.gif"; } } //end check for broken images PHP:
It is almost as bad as an error. There is a purpose for PHP showing Warnings and Notices. They are not to be ignored and hidden, but are there to be fixed.