<?php
/*

http://184.82.221.56/xxx999/tpl42/d2.jpg

http://184.82.221.56/xxx999/tpl42/logo.gif
http://184.82.221.56/xxx999/tpl42/vod/vod_0.jpg


http://123.goodlk.com/xxx999/tpl5412542/d2.jpg
http://123.goodlk.com/xxx999/tpl5412542/vod/vod_0.jpg


rz -y 
unzip a.zip


cd /var/www/123.goodlk.com/xxx999/tpl5412542

wget http://184.82.221.56/xxx999/tpl42/201301.zip
unzip 201301.zip
rm -f *.zip


*/
set_time_limit(0);
$url = "http://u.iirt.info/msoso/#n.jpg";$end = 15;$prefix = 'iirt_';

$url = "http://740593982.any2000.com/7/4/740593982/up/#n.jpg";$end = 35;$prefix = '39rtys_';

$url = "http://www.se567.info/images/#n.jpg";$end = 15;$prefix = 'se567_';

//http://61.164.126.165:100/pic/7/29.jpg
$url = "http://shandong12.any2000.com/s/h/shandong12/up/#n.jpg";$end = 25;$prefix = 'any2000_';
$url = "http://61.164.126.165:100/pic/7/#n.jpg";$end = 100;$prefix = 'small_';


$start=1;





function msg($msg = '', $color = 'rand', $sp = '<br>')
{

	static $cnt = 1;
	//if (0 && $cnt == 1)
	if ($cnt == 1)
	{
		echo '
		<SCRIPT>
/*function sc()
{
var e=document.getElementById("div1")
e.scrollTop=e.scrollHeight;
}*/
function sc()
{
window.scrollTo(0,document.body.scrollHeight)
}
var s=setInterval("sc()",3000);
</script>
';
	}
	!$msg && $msg = '处理完毕';
	if ($color == 'rand')
	{
		static $_color = array('red','blue','#B40000', '#35A22A', '#005AB4', '#333', '#B45A00', '#6100C2', '#A29E10');
		$_colorNum = count($_color)-1;
		$color = $_color[rand(0,  $_colorNum)];
	}
	echo $cnt . '.<b '."style='color:{$color}'".'>' . date('H:i:s', time()) . " {$msg}</b>{$sp}";
	$cnt++;
	flush();
	@ob_flush();
}

function getDir($dir)
{
	if(!$dp = opendir($dir))
	{
		exit('打开目录失败');
	}
	$dirs = $files = array();
	while($file = readdir($dp))
	{
		if($file{0} == '.')
		{
			continue;
		}
		if(is_dir($dir . $file))
		{
			$dirs[] = $file;
		}
		else
		{
			$files[] = $file;
		}
	}
	natsort($dirs);
	natsort($files);
	closedir($dp);
	return array('file' => $files, 'dir' => $dirs);
}
//////////////////////#程序开始
$action = isset($_GET['a']) ? $_GET['a'] : '';

if($action == 'down')
{
	
	for($i = $start; $i <  $end; ++$i)
	{
		$oldpath = str_replace('#n', $i, $url);
		$newpath = "{$prefix}{$i}.jpg";
		if(file_exists($newpath))
		{
			msg(" $newpath 文件已经存在");
			continue;
		}
		if($data = @file_get_contents($oldpath))
		{
			file_put_contents($newpath, $data);
			msg(" 下载: $newpath 文件成功");
		}
		else
		{
			msg("下载文件:{$oldpath}失败");
		}
	}

	$dir = str_replace('_', '', $prefix);
	if(!is_dir($dir))
	{
		mkdir($dir);
		msg("创建目录:{$dir}成功");
	}
	msg();
}
elseif($action == 'scan')
{
	$topdir = getDir('./');
	@unlink('all.txt');
	foreach($topdir['dir'] as $k => $v)
	{
		$rs = getDir("{$v}");
		
		$path = "{$v}/list.txt";
		$rec = array();
		foreach($rs['file'] as $kk => $vv)
		{
			if($vv !== 'list.txt')
			{
				$rec[] = $v . "/" . $vv;
			}
		}

		file_put_contents($path,  implode("\n", $rec));
		file_put_contents("{$v}.txt",  implode("\n", $rec));

		msg("扫描:{$v}目录成功");

		$path = "all.txt";
		$fp = fopen($path, 'a+');
		if($k)
		{
			fwrite($fp, "\n" . implode("\n", $rec));
		}
		else
		{
			fwrite($fp, implode("\n", $rec));
		}
		
	}
	fclose($fp);
	msg("操作成功");	
}
elseif($action == 'thumb')
{
	set_time_limit(600);
	$rs = getDir('tmp/');
	require('Image.class.php');
	$img = new image;
	
	foreach($rs['file'] as $k => $v)
	{
		

		$img->thumb("tmp/{$v}","tmp2/{$v}",'',800,800);

		
		msg("生成水印:{$v}成功");

		usleep(100000);


	}
	msg("操作成功");	
}
else
{
	#打印操作菜单
	$menu = array(
	'down'	 => "下载图片",
	'scan'	 => "生成TXT",
	'thumb'	 => "生成水印",
	);
	$kk = 1;
	foreach($menu as $k =>  $v)
	{
		echo $kk++, "<a href='?a={$k}' target='_blank'>{$v}</a>&nbsp;&nbsp;&nbsp;";

	}
}


