Консультация № 175498
21.12.2009, 21:30
35.00 руб.
0 1 1
Доброе время суток)
Есть ли у кого-то скрипт который скачивает больше 1го файла(т.е все картинки) с определенной страницы (как пример -- http://nnm.ru/blogs/malecat/k_130-letiyu_i_v_stalina/#cut) и закачивает из на определенный адрес в инете.
Спасибо)

Обсуждение

Неизвестный
23.12.2009, 17:51
общий
это ответ
Здравствуйте, Zolotarevsky.
Код:

<?php
// Пишем функцию для сбора ссылок
function harvest ($link) {
// Открываем файл по линку

/* раскомментировать, если хостинг не поддерживает кУРЛ (ессно, закомментировать секцию кУРЛ
$fd = fopen($link, 'r');
echo $fd."\n";
$buffer = "";
while (!feof($fd)) {
$buffer .= fgets($fd, 4096);
}
fclose($fd);
*/
// $buffer = file_get_contents ($link); // это ещё один способ получить сурс, крайний случай

$curl = curl_init(); // самый быстрый способ
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($curl, CURLOPT_URL, $link);
$buffer = curl_exec($curl);
curl_close ($curl);

// в буффере хтмл страницы

// пишем регэксп для выделения линков

$pattern = "|<img[^(src)]+src[\s]*=[\s]*[\'"]*([\d\w\.\:\/\,\&\=\-\_\+\%\;\?]+[^\'"\s>]+)[\'"\s>]+|i";

preg_match_all ($pattern, $buffer, $matches);

$l = pathinfo($link."?");

foreach ($matches[1] AS &$value) {

$value = trim($value);

if (substr($value, 0, 4) <> "http") $value = $l['dirname']."/".$value;

$picname = basename($value);

if (save_file(scaleImageFileToBlob($value), "harvest/".$picname)) {

echo "$picname saved\n";

} else {

echo "saving $picname failed\n";

};

};

return;

};

function save_file($file, $filename) {

$fd = fopen($filename, 'xb');

if (!$fd) return false;// save_file($file, $filename.rand(0,9));

fwrite($fd, $file);

fclose($fd);

return true;
}

function scaleImageFileToBlob($file) {

$source_pic = $file;
$max_width = 500;
$max_height = 500;

list($width, $height, $image_type) = getimagesize($file);

switch ($image_type)
{
case 1: $src = imagecreatefromgif($file); break;
case 2: $src = imagecreatefromjpeg($file); break;
case 3: $src = imagecreatefrompng($file); break;
default: return ''; break;
}

$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;

if( ($width <= $max_width) && ($height <= $max_height) ){
$tn_width = $width;
$tn_height = $height;
}elseif (($x_ratio * $height) < $max_height){
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
}else{
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}

$tmp = imagecreatetruecolor($tn_width,$tn_height);

/* Check if this image is PNG or GIF to preserve its transparency */
if(($image_type == 1) OR ($image_type==3))
{
imagealphablending($tmp, false);
imagesavealpha($tmp,true);
$transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
imagefilledrectangle($tmp, 0, 0, $tn_width, $tn_height, $transparent);
}
imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);

/*
* imageXXX() has only two options, save as a file, or send to the browser.
* It does not provide you the oppurtunity to manipulate the final GIF/JPG/PNG file stream
* So I start the output buffering, use imageXXX() to output the data stream to the browser,
* get the contents of the stream, and use clean to silently discard the buffered contents.
*/
ob_start();

switch ($image_type)
{
case 1: imagegif($tmp); break;
case 2: imagejpeg($tmp, NULL, 100); break; // best quality
case 3: imagepng($tmp, NULL, 0); break; // no compression
default: echo ''; break;
}

$final_image = ob_get_contents();

ob_end_clean();

return $final_image;
};


echo "\n\n==========================================================================\n\n";
harvest("http://nnm.ru/blogs/malecat/k_130-letiyu_i_v_stalina/");
echo "\n\n==========================================================================\n\n";

?>
Форма ответа