How can I get image from url in PHP or Jquery or in both?

前端 未结 5 1547
萌比男神i
萌比男神i 2021-01-29 14:24

Can anybody tell me, how can I get image for a particular site by its url only? As that is done on the google search page. I have searched web but I didn\'t get any satisfactory

相关标签:
5条回答
  • 2021-01-29 15:11

    I'm Not Experience Developer so i just try This Code can fetch only first image

    <?php
    $url = 'http://stackoverflow.com/questions/7994340/how-can-i-get-image-from-url-in-php-or-jquery-or-in-both';
    $data = file_get_contents($url);
    
    if(strpos($data,"<img"))
    {
        $imgpart1 = explode('<img src=',$data);
        $imgpart2 = explode('"',$imgpart1[1]);
        echo "<img src=".$imgpart2[1]." />";
    }
    ?>
    
    0 讨论(0)
  • 2021-01-29 15:12

    You will use to simplehtmldom. It is very useful to you. you can get the data and images.

    0 讨论(0)
  • 2021-01-29 15:13

    I would suggest that you make an http request to the page then you make a regular expression on the response to get the src attribute vale of img tag.

    0 讨论(0)
  • 2021-01-29 15:25

    Get the page with file_get_contents($url) then you can parse the content and download the images and store it at your server:

    $url = 'http://example.com/image.php';
    $img = '/my/folder/flower.gif';
    file_put_contents($img, file_get_contents($url));
    

    See this related Question.

    Update: To get the image you could use SimpleXMLElement:

    $obj = new SimpleXMLElement($htmlCode);
    $url = $obj->body->img->src;
    
    0 讨论(0)
  • 2021-01-29 15:29

    If you mean that you want to download a JPG/PNG/etc image from a URL then this PHP will do it:

    file_put_contents('image',file_get_contents('http://www.benjiegillam.com/files/img/eye-150.jpg'));
    

    If you mean that you want to take a screenshot of a remote web page then have a look at PhantomJS - see a previous answer of mine here: get webpage print screen, php/unix

    0 讨论(0)
提交回复
热议问题