Extract Images from a URL with PHP

 in this post i will show you how to extract images from a URL with PHP Script, Also will extract all images elements src,title and alt tags from the html code .

scraping images from url using php code example :


The images scraping php source code :

<style>

.myurl{
color: red;
border:1px solid #fd0;
background: #fee;
font-size: 12px;
text-align: center;
border: 2px solid green;
}

</style>

<form method=”post”><input type=”url” name=”pageurl” placeholder=”Please enter url” reuired=”reuired” /><input type=”submit” name=”submit” value=”Check HTTP Status” /></form>

<?php

echo “<p/>”;

if (isset($_POST[‘pageurl’]))
{
$pageurl = $_REQUEST[‘pageurl’];
$url = $pageurl;

$htmlString = file_get_contents($url);

$htmlDom = new DOMDocument;

//Load the HTML string
@$htmlDom->loadHTML($htmlString);

//Extract all images elements
$imageTags = $htmlDom->getElementsByTagName(‘img’);

//Loop
foreach($imageTags as $imgTag){

$imgSrc = $imgTag->getAttribute(‘src’);

$imgTitle = $imgTag->getAttribute(‘title’);

$imgAlt = $imgTag->getAttribute(‘alt’);

echo $imgSrc;

echo $imgTitle;

echo $imgAlt;
}

echo “<pre>”;
}
?>

Download Extract images using php code in zip file