Jump to content

Show Images from A website


AE117

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.