Jump to content

Extract image and save to directory.


mnybud
 Share

Recommended Posts

<?php

$website_contents = // the site's contents as string... maybe file_get_contents();

$has_matched = preg_match_all('/<img.*src="(.*)"/s',$website_contents,$matches);
if ($has_matched)
{
var_dump($matches); //you will know how to get them after you see the structure of the $matches var
}

?>

 

The code is not tested but it propably works... if it doesn't repost here and i will correct it...

 

Btw do not repost asking for an answer every 20 mins... Wait and you will get your answer...

 

The expressions can be improved... this was just to give you an idea of how is this done...

 

Edit: was posting did not see john's post he is right

Link to comment
Share on other sites

Ok sorry for the fast bump.  :-[

 

So I am sorry I am new to this. Can you please explain in a bit more detail how to configure your script.

For example if I wanted to grab this image:

http://i21.ebayimg.com/07/i/000/a7/63/131f_12.JPG

 

and save it to a folder on my server.

 

What would I need to change? Can you give me a working example?

Sorry I am a bit slow  :P

Link to comment
Share on other sites

you 're not slow you 're lazy....

 

the code i gave would be responsible for extracting the url of the images tha exist in the <img /> tag of a provided text... it is not responsible for saving the images locally...

 

In few words all you need to do is extract the urls and then using the same way that you got the page's source code (file_get_contents(),file(),cURL,whatever) take the image's content and save it locally...

 

if i were you i would take a look into preg_match_all() and generally regular expressions and then to the file system functions like fopen() or whichever you like...

 

Those can be found in the php manual

 

if you have any more questions or there is anything you don't understand ask here again... but dont ask for a working example before at least you try to make one yourself...

 

Kathas

Link to comment
Share on other sites

Well I have already grabbed the image url with my script, now I just need to save the image locally...this is the part I am confused by. I honestly dont get how your example works and I have tried it using other ways I found by searching the forums with no success.....lastly using this format...

 

file_put_contents('file_name.jpg', file_get_contents('http://host.com/image_file.png'));

 

not trying to be lazy just getting frustrated.... I will continue looking for the solution.....

Link to comment
Share on other sites

well I still dont get it but I guess I am just to green for this forum since you only point me in a vague direction which is where i was to begin with......remember I am a newbie  :-\

 

thanks anyways, I will just keep reading.......

Link to comment
Share on other sites

ok I 'll give u the answer u want... although the point was to find it yourself...

<?php 

// you said u have the urls so
$image = file_get_contents($url);
$i = strrpos($url,'/');
$name = substr($url,$i+1);
file = fopen($name);
if (!$file)
    // action for problem opening file
$write = fwrite($file,$image);
if ($write === FALSE)
     // action problem writing to file
fclose($file);

?>

 

now u can use this code but you learned nothing...

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

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.

 Share

×
×
  • 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.