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
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]." />";
}
?>
You will use to simplehtmldom. It is very useful to you. you can get the data and images.
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.
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;
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