Har följande script som hämtar alla bilder från en specifik hemsida, och sedan lägger dessa i en mapp.
<?php
function imageGrabber($url, $unique = 1)
{
$startTag = '<img';
$srcTag = 'src=';
$endTag = '>';
$counter = 0;
if(!is_array($url))
{
$url = array($url);
}
if ($unique !== 0 && $unique !== 1)
{
printf('Invalid parameter for $unique. The parameter must be either 1 or 0.');
exit();
}
foreach ($url as $value)
{
$contents = file_get_contents($value);
$domain = $value;
$domain = substr($domain, 7);
$pos = stripos($domain, '/');
if ($pos)
{
$domain = substr($domain, 0, stripos($domain, '/'));
}
while ($contents)
{
set_time_limit(0); # In case we have several large pages
$quotes = array('"', "'", "\n");
$contents = str_replace($quotes, '', $contents); # Strip " and ' as well as \n from input string
$contents = stristr($contents, $startTag); # Drop everything before the start tag '<img'
$contents = stristr($contents, $srcTag); # Drop everything before the 'src'
$endTagPosition = stripos($contents, $endTag); # Position of the end tag '>'
$src = substr($contents, 4, $endTagPosition - 4); # Get everything from src to end tag --> 'src="path" something>'
$spacePosition = stripos($src, ' '); # Position of space (if it exists)
if ($spacePosition !== false)
{
$src = substr($src, 0, $spacePosition); # Drop everything after space, keeping 'src="path"'
}
$questionMarkPosition = stripos($src, '?');
if ($questionMarkPosition !== false)
{
$src = substr($src, 0, $questionMarkPosition); # Remove any part after a '?'
}
$contents = stristr($contents, $endTag); # Drop everything before the end tag '>'
if ($src)
{
if (stripos($src, '/') === 0)
{
$src = 'http://'.$domain.$src; # Relative link, so add domain before '/'
}
else
{
if (stripos($src, 'http://') !== 0 && stripos($src, 'https://') !== 0 && stripos($src, 'ftp://') !== 0)
{
$src = 'http://'.$domain.'/'.$src; # Relative link, so add domain and '/'
}
}
$paths[] = $src;
}
}
if ($unique === 1)
{
$results[] = array_unique($paths); # Create final array with unique $paths
}
else
{
$results[] = $paths; # Create final array with all $paths
}
$paths = array(); # Reset links
$counter++; # Increment counter
}
return $results;
}
/**
* This function will downlaod and save all images on the specified directory.
*
* @param array $results
* @param sting $localPath
*/
function saveImages($results, $localPath = 'test/')
{
foreach ($results as $v)
{
foreach ($v as $value)
{
set_time_limit(0);
$path = $value;
if (!file_exists($localPath))
{
mkdir($localPath); # Create the dir if it doesn't exist
}
$localFile = $localPath.basename($path);
if (!copy($path, $localFile))
{
echo "<font color=red>Failed to copy $path</font><br>";
}
else
{
echo "<font color=blue>Successfully copied $path</font><br>";
}
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title> Hämta bilder</title>
<style type="text/css">
body{
background: #000000;
color: #FFFFFF;
}
input[type="text"]{
padding: 5px;
color: #c5c5c5;
border: 1px solid #000000;
font-size: 16px;
font-weight: bold
}
input[type="submit"]{
padding: 4px;
background: #000000;
color: #FFFFFF;
border: 1px solid #CCCCCC;
font-size: 16px;
font-weight: bold
}
</style>
</head>
<body>
<h3>Hämtade bilder</h3>
<?php
$lank = htmlspecialchars($_POST["lank"]);
$links = imageGrabber($lank);
saveImages($links);
///// VISA BILDER /////
$dir = "test";
foreach (glob($dir . '/*') as $filename) {
echo "<a href='" . $filename . "' target='_blank'><img src='" . $filename . "' height='60' border='0' /></a> ";
}
?>
<br /><br />
<hr />
<div style="text-align: center">
<form method="post" action="grab_images.php">
<input type="text" name="lank" size="50" />
<input type="submit" name="submit" value="hämta bilder" />
</form>
</div>
</body>
</html>
Nu undrar jag om det går att utöka scriptet ovan, så att man även kan hämta originalbilden, (och inte bara själva miniatyren).
Alltså att man följer länken från miniatyren, för att sedan även spara originalbilden.