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”;

}

51 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)))
{
..
}

Thank you…it works..

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

I like reading through an article that will make people think.
Also, thanks for allowing for me to comment!

Does your blog have a contact page? I’m havving a tough time locating
it but, I’d like to shoot yoou an email. I’ve ggot sopme creative ideas
for your blog you might be interested in hearing.
Either way, greeat blog and I look forward to seeing it improve over
time.

Yes! Finally someone writes about offering kiosk machine.

This web site certainly has all the info I wanted about this
subject and didn’t know who to ask.

昔を思えば、ワキ脱毛はずいぶん安くなったものです。どうしてかというと、ワキ脱毛を脱毛の入門的な位置づけに設定していて何が何でも安心価格で体験してもらおうと願っているからです。

三十路にもなってくると、顔のこめかみやおでこにシミやくすみ、そばかすが気になってきますよね。そんななやみに効くなのが、シミ漂白石鹸「エンジェルプレミアマジックソープ」なんです。
ブレンドされた美白成分の効果で、洗うだけでシミやくすみが取れちゃうんだとか!
絶対一度は使ってみたいですね〜!

皮膚が弱くて化粧水をつけると皮膚が赤くなってしまう方。敏感肌でお化粧もできない。敏感用のものを使ってもヒリヒリして痒くなってしまって、自分に合う化粧水がどうしても見つからない人はオージュンヌという化粧水を試してみるといいかもしれません。もちろんパラベンなどの不要なものは一切入っていないですから安心して使えます。アトピーでお化粧ができなかったという人でも、お化粧できるようになったケースがあります。ヒリヒリしてかゆみが出てしまったり、保湿タイプを使うと逆にニキビができてしまったりという方に向いています。

包皮輪が狭くて亀頭を露出できない状態を真性包茎といいます。仮性であれば手で皮をめくれるし、洗浄して清潔に出来るので問題ないのですが、真性包茎の場合は日常生活やエッチのときなどに不都合が生じるため改善が必要です。一番確実なのはドクターさんに診てもらうことです。泌尿器科であれば保険が使えますから3割負担で治療できますすし、仕上がりの美しさを求めるのであれば美容外科という道もあります。しかし癒着もなく、ただ包皮口が狭いだけであれば、手術の必要すらありません。真性包茎を改善する専用の器具を使えば治すことができます。キトー君という真性包茎改善グッズを使って狭いペニスの先端を広げるトレーニングをしていきます。

乳首が茶色くなってしまう理由はメラニンという色素が関与しているようです。メラノサイトというところから出てきてお肌を守る大事な役目を果たしているけれど、紫外線にあたったり摩擦などの外部刺激があると、それに反応して皮膚を黒くしてしまうのだとか。なので乳首の黒ずみ対策をするにはメラニンの生成を抑えるのがポイントになります。そしてメラニンの生成を抑制してくれる効果があるのがリン酸アスコルビルMgという成分なんですね。リン酸アスコルビルMgにはマグネシウムなどが含まれているのでお肌に浸透しやすく、角質層を超えてメラニン生成の要因となるメラノサイトまで届くため、美白効果やシミなどの老化現象の防止効果も期待されています。

掌に汗をかいてしまうのは好きな人の前だったり初対面の人と会ったり、緊張しているからですよね。こういうのを精神性発汗というのですが、この汗は気合でどうにかなるものではありません。がんばって汗をかかないようにしよう、緊張しないようにしようというのは無理だからです。汗は文字通り汗腺というところから出ています。この汗腺に物理的に蓋をして汗を出ないようにするというのが掌の汗を抑制する有効な対策になります。このような作用は、しゅうれん作用といいます。もっともお手軽に手汗を抑制する方法としてデオドラントを使う方法があります。ファリネという手汗専用の制汗パウダーや、フレナーラという手汗対策用の制汗ジェルクリームなどがあります。

美白が効きにくい乾燥肌にも優しいクリームがCLEAFORTです。黒ずみを作るメラニンの生成を抑え、そばかすを防ぐ働きがあります。温めた手のひらで1円玉くらいのクリームを塗ってあげることで、肌になじんで浸透力がより高まります。

Thank you a bunch for sharing this with all people you actually realize what you’re speaking about!

Bookmarked. Please also talk over with my website =).
We will have a link exchange contract between us

[…] 信用:http : //junal.wordpress.com/2008/07/22/checking-if-an-image-url-exist/ […]

Leave a comment

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

Follow Me on Twitter

Archives

My photos

Blog Stats

  • 428,791 hits