<?php
/*
* 名稱:PHP單頁面圖片批量上傳管理系統 by zozi
* 功能:批量上傳圖片,自動讀取目錄,批量刪除圖片,自動獲得圖片地址,
* 生成復制連接,支持圖片水印 (水印支持圖片或文字)
* 水印參數:
* $groundImage 背景圖片,即需要加水印的圖片,暫只支持GIF,JPG,PNG格式;
* $waterPos 水印位置,有10種狀態,0為隨機位置;
* 1為頂端居左,2為頂端居中,3為頂端居右;
* 4為中部居左,5為中部居中,6為中部居右;
* 7為底端居左,8為底端居中,9為底端居右;
* $waterImage 圖片水印,即作為水印的圖片,暫只支持GIF,JPG,PNG格式;
* $waterText 文字水印,即把文字作為為水印,支持ASCII碼,不支持中文;
* $textFont 文字大小,值為1、2、3、4或5,默認為5;
* $textColor 文字顏色,值為十六進制顏色值,默認為#FF0000(紅色);
*
* 注意:Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG 、PNG
* $waterImage 和 $waterText 最好不要同時使用,選其中之一即可,優先使用 $waterImage。
* 當$waterImage有效時,參數$waterString、$stringFont、$stringColor均不生效。
* 加水印后的圖片的文件名和 $groundImage 一樣。
*/
?>
<?php
$zpass='123456'; //登陸密碼
$zurl='www.mycodes.net'; //使用地址
$zname='upimage.php'; //本頁面名稱
function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000")
{
$isWaterImage = FALSE;
$formatMsg = "暫不支持該文件格式,請用圖片處理軟件將圖片轉換為GIF、JPG、PNG格式。";
//讀取水印文件
if(!empty($waterImage) && file_exists($waterImage))
{
$isWaterImage = TRUE;
$water_info = getimagesize($waterImage);
$water_w = $water_info[0];//取得水印圖片的寬
$water_h = $water_info[1];//取得水印圖片的高
switch($water_info[2])//取得水印圖片的格式
{
case 1:$water_im = imagecreatefromgif($waterImage);break;
case 2:$water_im = imagecreatefromjpeg($waterImage);break;
case 3:$water_im = imagecreatefrompng($waterImage);break;
default:die($formatMsg);
}
}
//讀取背景圖片
if(!empty($groundImage) && file_exists($groundImage))
{
$ground_info = getimagesize($groundImage);
$ground_w = $ground_info[0];//取得背景圖片的寬
$ground_h = $ground_info[1];//取得背景圖片的高
switch($ground_info[2])//取得背景圖片的格式
{
case 1:$ground_im = imagecreatefromgif($groundImage);break;
case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
case 3:$ground_im = imagecreatefrompng($groundImage);break;
default:die($formatMsg);
}
}
else
{
die("需要加水印的圖片不存在!");
}
//水印位置
if($isWaterImage)//圖片水印
{
$w = $water_w;
$h = $water_h;
$label = "圖片的";
}
else//文字水印
{
$temp = imagettfbbox(ceil($textFont*2.5),0,"./cour.ttf",$waterText);//取得使用 TrueType 字體的文本的范圍
$w = $temp[2] - $temp[6];
$h = $temp[3] - $temp[7];
unset($temp);
$label = "文字區域";
}
if( ($ground_w<$w) || ($ground_h<$h) )
{
echo "需要加水印的圖片的長度或寬度比水印".$label."還小,無法生成水??!";
return;
}
switch($waterPos)
{
case 0://隨機
$posX = rand(0,($ground_w - $w));
$posY = rand(0,($ground_h - $h));
break;
case 1://1為頂端居左
$posX = 0;
$posY = 0;
break;
case 2://2為頂端居中
$posX = ($ground_w - $w) / 2;
$posY = 0;
break;
case 3://3為頂端居右
$posX = $ground_w - $w;
$posY = 0;
break;
case 4://4為中部居左
$posX = 0;
$posY = ($ground_h - $h) / 2;
break;
case 5://5為中部居中
$posX = ($ground_w - $w) / 2;
$posY = ($ground_h - $h) / 2;
break;
case 6://6為中部居右
$posX = $ground_w - $w;
$posY = ($ground_h - $h) / 2;
break;
case 7://7為底端居左
$posX = 0;
$posY = $ground_h - $h;
break;
case 8://8為底端居中
$posX = ($ground_w - $w) / 2;
$posY = $ground_h - $h;
break;
case 9://9為底端居右
$posX = $ground_w - $w-6;
$posY = $ground_h - $h-6;
break;
default://隨機
$posX = rand(0,($ground_w - $w));
$posY = rand(0,($ground_h - $h));
break;
}
//設定圖像的混色模式
imagealphablending($ground_im, true);
if($isWaterImage)//圖片水印
{
imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷貝水印到目標文件
}
else//文字水印
{
if( !empty($textColor) && (strlen($textColor)==7) )
{
$R = hexdec(substr($textColor,1,2));
$G = hexdec(substr($textColor,3,2));
$B = hexdec(substr($textColor,5));
}
else
{
die("水印文字顏色格式不正確!");
}
imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
}
//生成水印后的圖片
@unlink($groundImage);
switch($ground_info[2])//取得背景圖片的格式
{
case 1:imagegif($ground_im,$groundImage);break;
case 2:imagejpeg($ground_im,$groundImage);break;
case 3:imagepng($ground_im,$groundImage);break;
default:die($errorMsg);
}
//釋放內存
if(isset($water_info)) unset($water_info);
if(isset($water_im)) imagedestroy($water_im);
unset($ground_info);
imagedestroy($ground_im);
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>圖片上傳</title>
<style type="text/css">
<!--
body, table {
font-family: "宋體";
font-size: 14px;
padding:0px;
margin:0px;
}
-->
</style>
</head>
<body>
<table width="760" border="0" align="center">
<tr>
<td>
<?php
//當前時間
$nowtime=time();
$baseurl= $_SERVER['PHP_SELF'] ? dirname($_SERVER['PHP_SELF']) : dirname($_SERVER['SCRIPT_NAME']);
$baseurl='http://'.$_SERVER['HTTP_HOST'].$baseurl;
//檢查是否有提交圖片
if(!empty($_FILES['attachfile']['name'])){
//處理每個圖片
foreach($_FILES['attachfile']['name'] as $k=>$v){
//圖片名字不為空
if(!empty($v)){
//后綴必須是圖片名
if(eregi("/.(gif|jpg|jpeg|png|bmp)$",$v)){
//圖片不能大于8M
if($_FILES['attachfile']['size'][$k] > 8388608) jieqi_delfile($_FILES['attachfile']['tmp_name'][$k]);
else{
//解析圖片后綴
$tmpary=explode('.', $v);
$postfix=$tmpary[count($tmpary)-1];
$attachdir = date('Ym',$nowtime);
if (!file_exists($attachdir)) jieqi_createdir($attachdir);
$attachdir .= '/'.date('d',$nowtime);
if (!file_exists($attachdir)) jieqi_createdir($attachdir);
$runtime = explode(' ', microtime());
$attachname=$attachdir.'/'.date('His',$nowtime).round($runtime[0]*1000).$k.'.'.$postfix;
@move_uploaded_file($_FILES['attachfile']['tmp_name'][$k], $attachname);
@chmod($attachname, 0644);
$url=jieqi_htmlstr($baseurl.'/'.$attachname);
//打水印
if ($_REQUEST["water"]) {
$pic=$attachname;
$wpic="xhxsw.gif";
$info=getimagesize($pic);
$w=$info[0];
$h=$info[1];
if ($h>140) {
imageWaterMark($pic,1,"xhxsw.gif");
}
if ($h>400) {
imageWaterMark($pic,7,"xhxsw.gif");
}
if ($h>1600) {
imageWaterMark($pic,4,"xhxsw.gif");
}
}
echo '<a href="'.$url.'" target="_blank">'.$url.'</a>';
?><span title="復制圖片URL到剪貼板" onclick="setcopy('<?echo $url?>', '圖片地址已經復制到剪貼板')">[復制]</span><br>
<? }
}else{
jieqi_delfile($_FILES['attachfile']['tmp_name'][$k]);
}
}
}
}
function jieqi_htmlstr($str, $quote_style=ENT_QUOTES){
$str = htmlspecialchars($str, $quote_style);
$str = nl2br($str);
$str = str_replace(" ", " ", $str);
return $str;
}
// 讀文件
function jieqi_readfile($file_name){
if (function_exists("file_get_contents")) {
return file_get_contents($file_name);
}else{
$filenum = @fopen($file_name, "rb");
@flock($filenum, LOCK_SH);
$file_data = @fread($filenum, @filesize($file_name));
@flock($filenum, LOCK_UN);
@fclose($filenum);
return $file_data;
}
}
//寫文件
function jieqi_writefile($file_name, &$data, $method = "wb"){
$filenum = @fopen($file_name, $method);
if(!$filenum) return false;
@flock($filenum, LOCK_EX);
$ret = @fwrite($filenum, $data);
@flock($filenum, LOCK_UN);
@fclose($filenum);
@chmod($file_name, 0777);
return $ret;
}
//刪除文件
function jieqi_delfile($file_name){
return unlink($file_name);
}
// 刪除目錄
function jieqi_delfolder($dirname, $flag = true){
$handle = @opendir($dirname);
while ($file = @readdir($handle)) {
if($file != '.' && $file != '..'){
if (is_dir($dirname . DIRECTORY_SEPARATOR . $file)){
jieqi_delfolder($dirname . DIRECTORY_SEPARATOR . $file, true);
}else{
@unlink($dirname . DIRECTORY_SEPARATOR . $file);
}
}
}
@closedir($handle);
if ($flag) @rmdir($dirname);
}
//建立目錄
function jieqi_createdir($dirname, $mode=0777, $recursive = false){
if (version_compare(PHP_VERSION, '5.0.0', '>=')) {
return mkdir($dirname, $mode, $recursive);
}
if (!$recursive) {
$ret=mkdir($dirname, $mode);
if($ret) chmod($dirname, $mode);
return $ret;
}
return is_dir($dirname) or (jieqi_createdir(dirname($dirname), $mode, true) and mkdir($dirname, $mode));
}
//檢查目錄是否存在,不存在嘗試自動建立
function jieqi_checkdir($dirname, $autocreate=0){
if(is_dir($dirname)){
return true;
}else{
if(empty($autocreate)) return false;
else return jieqi_createdir($dirname);
}
}
?>
</td>
</tr>
</table>
<?
if ($_GET[z]=$zpass){?><table width="760" border="0" align="center">
<tr>
<td colspan="2" align="right"><form action="<?echo $zname?>" ><input type="submit" name="quit" value=" 退出 "></form>
</td>
</tr>
</table>
<form name="frmupload" method="post" action="<?echo $zname?>?z=<?echo $zpass?>" enctype="multipart/form-data">
<table width="760" border="1" align="center">
<tr>
<td colspan="2" align="center">圖片批量上傳程序</td>
</tr>
<tr>
<td width="114">圖片一:</td>
<td width="470"><input type='file' class='text' size='60' name='attachfile[]' id='attachfile[]' /></td>
</tr>
<tr>
<td>圖片二:</td>
<td><input type='file' class='text' size='60' name='attachfile[]' id='attachfile[]' /></td>
</tr>
<tr>
<td>圖片三:</td>
<td><input type='file' class='text' size='60' name='attachfile[]' id='attachfile[]' /></td>
</tr>
<tr>
<td>圖片四:</td>
<td><input type='file' class='text' size='60' name='attachfile[]' id='attachfile[]' /></td>
</tr>
<tr>
<td>圖片五:</td>
<td><input type='file' class='text' size='60' name='attachfile[]' id='attachfile[]' /></td>
</tr>
<tr>
<td>圖片六:</td>
<td><input type='file' class='text' size='60' name='attachfile[]' id='attachfile[]' /></td>
</tr>
<tr>
<td>圖片七:</td>
<td><input type='file' class='text' size='60' name='attachfile[]' id='attachfile[]' /></td>
</tr>
<tr>
<td>圖片八:</td>
<td><input type='file' class='text' size='60' name='attachfile[]' id='attachfile[]' /></td>
</tr>
<tr>
<td>圖片九:</td>
<td><input type='file' class='text' size='60' name='attachfile[]' id='attachfile[]' /></td>
</tr>
<tr>
<td>圖片十:</td>
<td><input type='file' class='text' size='60' name='attachfile[]' id='attachfile[]' /></td>
</tr>
<tr><input name="water" type="hidden" id="water" value="0" checked >
</tr>
<tr>
<td> </td>
<td width=600><input type="submit" name="Submit" value=" 提交 ">
</form></td>
</tr>
</table>
<table width="760" border="0" align="center">
<tr>
<td>
<?php
if(!empty($_REQUEST['delurl'])){
foreach($_REQUEST['delurl'] as $v){
if(empty($v)) continue;
if(!eregi("/.(gif|jpg|jpeg|png|bmp)$",$v)){
echo '<font color="red">您提交的不是圖片地址:</font><a href="'.urlencode($v).'" target="_blank">'.$v.'</a><br>';
}elseif(strpos($v,$baseurl) !== 0){
echo '<font color="red">您提交的圖片地址錯誤:</font><a href="'.urlencode($v).'" target="_blank">'.$v.'</a><br>';
}else{
$delpath=str_replace($baseurl,'.',$v);
if(!file_exists($delpath)){
echo '<font color="red">圖片不存在:</font><a href="'.urlencode($v).'" target="_blank">'.$v.'</a><br>';
}else{
$ret=jieqi_delfile($delpath);
if($ret) echo '<font color="blue">刪除完成:</font><a href="'.urlencode($v).'" target="_blank">'.$v.'</a><br>';
else echo '<font color="red">刪除失敗,可能權限不對:</font><a href="'.urlencode($v).'" target="_blank">'.$v.'</a><br>';
}
}
}
}
?>
</td>
</tr>
</table>
<script type="text/JavaScript">
function setcopy(text, alertmsg){
clipboardData.setData('Text', text);
alert(alertmsg);
}
</script>
<?php
$b=0;
$dir = './../uppic';
echo "<center>已經上傳的圖片如下:/n<div style='width:830px'>";
function view_dir($directory)
{
$handle = opendir( $directory );
while ( $file = readdir($handle) )
{
$bdir = $directory . '/' .$file ;
if ($file <> '.' && $file <> '..' && $file <> 'Thumbs.db' && $file <> $zname && is_dir($bdir))
{
view_dir( $directory .'/'. $file);
}
else if( $file <> '.' && $file <> '..' && $file <> 'Thumbs.db' && $file <> $zname)
{
$a = $file ;
?><div style='float:left;width:180px;' id=<?echo $b?> name=<?echo $b?>><a href='<?echo $directory?>/<?echo $a?>' target=_blank><?echo $a?></a>
<span title="復制圖片URL到剪貼板" onclick="setcopy('http://<?echo $zurl?>/1/2/.<?echo $directory?>/<?echo $a?>', '圖片地址已經復制到剪貼板')">[復制]</span>
</div>
<? $b=$b+1;
}
}
closedir( $handle );
}
view_dir($dir);
?>
<form name="frmdelete" action="<?echo $zname?>?z=<?echo $zpass?>" method="post">
<table width="760" border="1" align="center" cellpadding="3">
<tr>
<td colspan="2" align="center">刪除圖片</td>
</tr>
<tr>
<td width="114">圖片網址一:</td>
<td width="470"><input name="delurl[]" id="delurl[]" type="text" size="60"></td>
</tr>
<tr>
<td>圖片網址二:</td>
<td><input name="delurl[]" id="delurl[]" type="text" size="60"></td>
</tr>
<tr>
<td>圖片網址三:</td>
<td><input name="delurl[]" id="delurl[]" type="text" size="60"></td>
</tr>
<tr>
<td>圖片網址四:</td>
<td><input name="delurl[]" id="delurl[]" type="text" size="60"></td>
</tr>
<tr>
<td>圖片網址五:</td>
<td><input name="delurl[]" id="delurl[]" type="text" size="60"></td>
</tr>
<tr>
<td>圖片網址六:</td>
<td><input name="delurl[]" id="delurl[]" type="text" size="60"></td>
</tr>
<tr>
<td>圖片網址七:</td>
<td><input name="delurl[]" id="delurl[]" type="text" size="60"></td>
</tr>
<tr>
<td>圖片網址八:</td>
<td><input name="delurl[]" id="delurl[]" type="text" size="60"></td>
</tr>
<tr>
<td>圖片網址九:</td>
<td><input name="delurl[]" id="delurl[]" type="text" size="60"></td>
</tr>
<tr>
<td>圖片網址十:</td>
<td><input name="delurl[]" id="delurl[]" type="text" size="60"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit2" value=" 刪除 "></td>
</tr>
</table>
</form>
<?}else{?><br><br><br><center>
請輸入密碼:<form action="<?echo $zname?>" method="get"><input type=password name=z size=12><input type=submit value=" 提 交 "></form>
<?}?>
</body>
</html>