AE117 Posted May 18, 2010 Share Posted May 18, 2010 Here is what I am looking for. Facebook has this feature where when you put in a website url Example: www.mydomain.com it will ask you to choose an image from that website. I want to do a similar thing, Have user input a url then show all the images from the url. Can anyone point me in the right way I have been looking for hours on google and the php manual and have come up with nothing. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/202121-show-images-from-a-website/ Share on other sites More sharing options...
bibby Posted May 18, 2010 Share Posted May 18, 2010 Perhaps you're making it harder than it needs to be. If these images were on your server, you may elect to use a relative path, right? <img src="imgs/blah/some.png"> So when your users enter an image as a full url, your aim is simply to make that the source, no? <img src="http://notyourdomain.com/some.png"> There isn't much to that. Now, if you want to cache local copies of those remote images, that's a better question. Is that what you meant? If so, look at file_get_contents( $url ) and be sure to check that it's really an image and not a malicious script. Quote Link to comment https://forums.phpfreaks.com/topic/202121-show-images-from-a-website/#findComment-1059919 Share on other sites More sharing options...
AE117 Posted May 18, 2010 Author Share Posted May 18, 2010 Thats not what I am talking about. There is no way to know where the images are stored on any given website url, let alone know the name. Pretty much I want to be able to insert a Url into a textfeild hit submit then I want a php script to go to through the url given, so go to that website and find all the images that are wrapped in the img tag and then display them on my website with out downloading them. I know its hard but I also know is possible.... Once again any help is appreciated I will update this when I get closer or atleast hit the road. Quote Link to comment https://forums.phpfreaks.com/topic/202121-show-images-from-a-website/#findComment-1060165 Share on other sites More sharing options...
jdavidbakr Posted May 18, 2010 Share Posted May 18, 2010 You're wanting to do some sort of a screen scraper, then? Not overly trivial, but I think what you'll want to do is load the html of the page into a variable with file_get_contents() and run preg_match() to extract all the images with a regular expression. You'll then need to check those images to see if you need to pepend the directory you're pulling the html from, just the domain (if the src tag starts with "/"), or keep it as is. Quote Link to comment https://forums.phpfreaks.com/topic/202121-show-images-from-a-website/#findComment-1060169 Share on other sites More sharing options...
AE117 Posted May 19, 2010 Author Share Posted May 19, 2010 You're wanting to do some sort of a screen scraper, then? Not overly trivial, but I think what you'll want to do is load the html of the page into a variable with file_get_contents() and run preg_match() to extract all the images with a regular expression. You'll then need to check those images to see if you need to pepend the directory you're pulling the html from, just the domain (if the src tag starts with "/"), or keep it as is. I think I get what your saying I will give it a shot and see how it works and let you all know Quote Link to comment https://forums.phpfreaks.com/topic/202121-show-images-from-a-website/#findComment-1060369 Share on other sites More sharing options...
AE117 Posted May 19, 2010 Author Share Posted May 19, 2010 Thanks alot jdavidbakr you pointed me in the right direction and allowed me to get the information I needed. Here is the code that will do what I am talking about which is given a url show the images from that page. If you still dont understand when you run the code you will <?php $url = "http://www.domain.com/"; $baseurl = preg_replace("/[^\/]+$/","",$url); $page = file_get_contents($url); $parts = explode("<",$page); $images = array(); foreach($parts as $part){ if(preg_match("/img/",$part)){ $part = preg_replace("/^img.+src=\"([^\"]+)\".+$/m","$1",$part); if(!preg_match("/http:/",$part)){ $part = preg_replace("/^\//","",$part); $part = $baseurl . $part; } $images[] = $part; } } foreach($images as $img){ print '<img src="'.$img.'">'; } ?> Dealing with relative or absolute is another issue but its a great start Once again thanks. Quote Link to comment https://forums.phpfreaks.com/topic/202121-show-images-from-a-website/#findComment-1060372 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.