Junal on the run

Checking if an image URL exist with PHP….

Posted on: July 22, 2008

To check if an image/file URL exist, first thing came in my mind was file_exist() function. Thought it would be the best way to find whether my parameter that is throwing an image url is right or wrong. So I had to set a default image if there was no image found from the given link. file_exist() has a problem that it reads the directory of the file/image so when i’m reading an URL it will always return false. Well, I could separate the directory from the URL but it would be lengthy and messy, so I was searching for a better way. After reading the PHP manual online version I have found that this task can be done by getimagesize(). It returns an Array() with 7 elements. So idea is simple, if I get an array from the given URL then I can say that image is found otherwise not!

An example could be like this:

$url=getimagesize(“http://www.flickr.com/photos/27505599@N07/2564389539/”);

if(!is_array($url))

{

$default_image =”…/directoryFolder/junal.jpg”;

}

About these ads

36 Responses to "Checking if an image URL exist with PHP…."

Did you miss the following line?

$url = getimagesize($url);

whoops ! Thanks Tanzil …..forgot a big thing he he ….that was a typo!

No Prob!
Always welcome. :)

But I get this warning message even though it works properly.

Warning: getimagesize(images/cars/4.thumb.jpg) [function.getimagesize]: failed to open stream: No such file or directory

For example, I have:

if (!is_array($imgcheck))
{
$imgurl = ‘images/cars/na.thumb.jpg';
}
else {
$imgurl = ‘images/cars/’ . $row["id"] . ‘.thumb.jpg';
}

So when the img src refers to $imgurl as the source, it displays na.thumb.jpg correctly, but there is still the warning message displayed above it…saying that it could not find the image.

How can I remove/avoid this?

Thanks

are you sure your directory (images/cars/4.thumb.jpg) is right one ? i have doubt ..please submit your full code. thanks

Yes the directory is fine. Like I said, it works and it displays the alternate image properly, it just also includes the Warning message.

For some reason, it worked fine when testing it on my own machine, but when I uploaded to the host, thats the warning I got. That is pretty much the entire code, at lease all of that deals with the “image exists” part.

Thanks

And even if the directly wasn’t the right one, isn’t the whole point of this to figure out whether the file (file path) exists?

Any suggestions?

Thanks

Sorry, directly = directory.

Hi,

Just thought I’d let you know, problem solved.

It was just including a warning message, so I included ‘@’ in front of getimagesize.

So:

$imgurl_check = ‘imgurl';

if (!is_array(@getimagesize($imgurl_check)))
{
..
}

well, hi admin adn people nice forum indeed. how’s life? hope it’s introduce branch ;)

dFjIaR hi nice site thanx http://peace.com

I found this function very slow so I came up with another solution to the problem. The function below only gets 1 byte of data from the image and it seems to be a bit quicker on my server at least:

function url_exists($url) {
if(@file_get_contents($url,0,NULL,0,1)){return 1;}else{ return 0;}
}

Cheers,
cron

Cron’s code above also worked much faster for me. Nice contributions on all sides.

I say this works…

function image_exists($url)
{

$info = @getimagesize(urlencode($url));

return((bool) $info);

}

thanks,

I prefer to use your code, pushpinder.


I say this works…

function image_exists($url)
{

$info = @getimagesize(urlencode($url));

return((bool) $info);

}

because, this one is take long time to load…


$url=@getimagesize($url);
if(!is_array($url))
{

What if an image doesn’t exist at the url…? How do I make it not show an error?

Don’t thing so far use this very simeple way:

img src=”xx.jpg” alt=”Anshul Srivastava” onerror=’this.src=”yy.gif”‘

wow. Elegant solution. Perfect. 10x Ravi

You could also do: img src=”xx.jpg” alt=”Anshul Srivastava” onerror=”this.height=’0′”

Perfetc ravi , save lot of time thanks

nice and fast, but alas… it puts browser into an infinite loop if yy.gif does not exist. And FF continues reading the page if images have been blocked in the browser setting.

In addition, the onerror attribute does not validate as XHTML 1.0 Transitional

Hey, thanks a lot for sharing this..

That is exactly what I needed! Thank you for sharing this – saved me a big headache.

css is probably much easier to define a max-width:

.theImg { max-width:100px; }

yeah this code sucks and doesn’t work right if the image isn’t in the directory it gives the warning everyone has been saying and this will affect your google ranking I just got a google alert today about it.

thanks,worked fine

Thanks, simple and easy solution!

thank you!
( onerror=”Javascript” )
Why complicate things when you can make simple xD

$essais = get_headers($url, 1); if (preg_match(“#OK#i”, $essais[0])) $img_url_big=$img_url= $url;

Thanks alot u all helped me.

Thanks bro..:)

I don’t know if it’s just me or if everyone else encountering issues
with your website. It appears as if some of the text in your posts are running off the screen. Can someone else please provide feedback
and let me know if this is happening to them too? This could be a problem with my internet
browser because I’ve had this happen before.
Appreciate it

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

View Junal Rahman's profile on LinkedIn
Subscribe to me on FriendFeed

Follow Me on Twitter

Archives

My photos

Downtown, Ottawa

Dream

Jasper, AB

Jasper, AB

Banff, AB

Folkfest, Bangladesh Pavilion Saskatoon

Lovely Sky

Stunt bikers on summer festival, Saskatoon

More Photos

Blog Stats

  • 389,591 hits
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: