Here is an example of scraping images from the coppermine main site & displaying 5 of them at random:
global $sourcedir;
require_once($sourcedir . '/Subs-Package.php');
$url = 'http://coppermine-gallery.net/demo/cpg15x';
$classname = 'image';
$amount = 5;
$html = fetch_web_data($url) ? fetch_web_data($url) : false;
$myimages = array();
if ($html)
{
$dom = new domDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_use_internal_errors(false);
$dom->preserveWhiteSpace = false;
$xpath = new DOMXPath($dom);
$images = $xpath->query('//img[@class="'.$classname.'"]');
foreach ($images as $image)
$myimages[] = $image->getAttribute('src');
}
if (!empty($myimages))
{
$myimages = array_unique($myimages);
shuffle($myimages);
echo '
<table style="width:100%">
<tr class="centertext">';
for ($i = 0; $i < ($amount); $i++)
{
$base = basename($myimages[$i]);
$pos = strpos($base,'thumb_');
if ($pos !== false)
$base = substr_replace($base,'',$pos,strlen('thumb_'));
$ext = '.' . pathinfo($base, PATHINFO_EXTENSION);
echo '
<td>
<img style="width:128px;height:128px;" src="',$url, '/', $myimages[$i], '" alt="" title="'.str_replace($ext, '', $base).'" />
</td>';
}
echo '
</tr>
</table>';
}
Since you wanted it at random I made it scrape all the images that met a specific class attribute. The problem with scraping is that when a lot of data exists on the page there is a noticeable delay especially when the page contains images (in the case there are many). One could attempt to use cURL to see if there is less lag.
\\Edit -> Added array_unique in case of duplicate images & omitted thumb_ prefix for file names (1st occurrence).