Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Image unavalible script?? check images and then if not show other?
Old 09-04-2007, 05:37 PM Image unavalible script?? check images and then if not show other?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
hey,

how could i include a script, which is a image im trying to display isnt avalible it shows a "image unavalible" picture?

Dan


(Btw this is the 7,501th THREAD!)
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
 
Register now for full access!
Old 09-04-2007, 06:41 PM Re: Image unavalible script?? check images and then if not show other?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Do you want to check if an image on another hosts exists, or you try to generate an image when a script on your server had an error ?
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 09-04-2007, 06:46 PM Re: Image unavalible script?? check images and then if not show other?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
i mean like some kind of function i can jsut include which checks all the image urls so if its going to display a red x it changes the src to one of my own predefiend images
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-04-2007, 06:52 PM Re: Image unavalible script?? check images and then if not show other?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
You should better use ajax for that.
There is 1 kind of request that's just perfect for this job, the HEAD request.
http://www.jibbering.com/2002/4/httprequest.html#head
read this chapter, and notice the "Does a url exist?" a bit below

Basically, it serve just to get the headers served back by the server on the ajax call end point.
Simply add a js function to be fierd upon onload, get every img tags, do check on the src, and if the return code is 40x, change the src to an generic error image.

The downside of this is that you must have your page loaded for the function to run, and that may let the user view an "red cross" in the mean time.

There is the code to get every images in the page:
Code:
var aryImg=document.getElementsByTagName('img');
for (var cptImg=0;cptImg<aryImg.length;cptImg++){
  chkImg(aryImg[cptImg]);
}
Do the header code check in a function named chkImg().
Right now, this function receive a reference to the img element currently checked, so simply do the check, and update the src property if the code is 404.

For the ajax call that makes the check, I let you look for it.
It depend too much if you do everything by hand, or rely on a framework, like prototype (my favourite!)
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 09-04-2007 at 06:56 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 09-04-2007, 06:57 PM Re: Image unavalible script?? check images and then if not show other?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Can it be done in PHP?
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-04-2007, 07:10 PM Re: Image unavalible script?? check images and then if not show other?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
you would need to use output buffering for that.
1) Get the page content
2) extract every img src values
3) make file_get_content, or better, use curl to check the http status returned by the destination server
4) update the src if needed
5) echo the updated html

But, you will have to parse a big string, and extract bits of text from it.
I'd recommend you to rather use the javascript and it's DOM abilities.
It's been designed from ground to do jobs like that.

Or maybe there is a way for PHP to generate a DOM tree from an html string?
On there, I don't know.
Edit:
I see in the php reference this:
http://www.php.net/manual/en/functio...t-loadhtml.php
but it looks very experimental for now. PHP version supporting it are not reported.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 09-04-2007 at 07:12 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 09-05-2007, 04:32 AM Re: Image unavalible script?? check images and then if not show other?
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
Surely you can just use the file_exists function.

<img src="if (file_exists($image)) {
echo $image
}else{
echo $no_image }?>"

Works for me
maxxximus is offline
Reply With Quote
View Public Profile
 
Old 09-05-2007, 12:03 PM Re: Image unavalible script?? check images and then if not show other?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
would this work tho for external images?

great one tho im gonna test that! if it works Ill defiantly send soem TP your way

Sorry, tripy as you can tell from the the Javascript Vs PHP thread, if it can be done in PHP or CSS ill do it that way.

Thanks
Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-05-2007, 12:26 PM Re: Image unavalible script?? check images and then if not show other?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
ok this is weird.

im trying to use
PHP Code:
echo (file_exists('http://calm.dansgalaxy.co.uk/images/calmtort.jpg')) ? 'http://calm.dansgalaxy.co.uk/images/calmtort.jpg' 'http://calm.dansgalaxy.co.uk/images/warning.gif'
the above SHOULD show the file because it does exist, but it shows the warning one.

but if the file dont exist it shows what it shouldnt?

im confused
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Reply     « Reply to Image unavalible script?? check images and then if not show other?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.59799 seconds with 12 queries