Skip to content

Instantly share code, notes, and snippets.

@abhij89
Created April 14, 2018 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhij89/6adc9774f774b4dc29b3d7029f3053b8 to your computer and use it in GitHub Desktop.
Save abhij89/6adc9774f774b4dc29b3d7029f3053b8 to your computer and use it in GitHub Desktop.
Quick php code for scrapping images
<?php
// For no time limit
set_time_limit(0);
$url = "url-to-external-page";
$pagecontent = file_get_contents($url);
$dom = new DOMDocument();
$dom->loadHTML($pagecontent);
// find image by class name
$finder = new DomXPath($dom);
$classname = "img-fluid"; // eg class name - img-fluid
$nodes = $finder->query("//*[contains(@class, '$classname')]");
// or use below to find by element
// $nodes = $dom->getElementsByTagName('img');
foreach ($nodes as $picture) {
foreach ($picture->attributes as $attr) {
if ($attr->nodeName == "src") {
$img = 'path-to-local-path-to-save-image';
file_put_contents($img, file_get_contents($attr->nodeValue));
} else {
continue;
}
}
$count++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment