<?php
/******************************************************************************
参数说明:
$max_file_size : 上传文件大小限制, 单位BYTE
$destination_folder : 上传文件路径
$watermark : 是否附加水印(1为加水印,其他为不加水印);
使用说明:
//上传文件类型列表
$uptypes=array(
'image/jpg',
'image/jpeg',
'image/png',
'image/pjpeg',
'image/gif',
'image/bmp',
'image/x-png'
);
$max_file_size=; //上传文件大小限制, 单位BYTE
$destination_folder="uploadimg/"; //上传文件路径
$watermark=; //是否附加水印(1为加水印,其他为不加水印);
$watertype=; //水印类型(1为文字,2为图片)
$waterposition=; //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
$waterstring="http://www.xplore.cn/"; //水印字符串
$waterimg="xplore.gif"; //水印图片
$imgpreview=; //是否生成预览图(1为生成,其他为不生成);
$imgpreviewsize=/; //缩略图比例
?>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (!is_uploaded_file($_FILES["upfile"][tmp_name]))
//是否存在文件
{
echo "图片不存在!";
exit;
}
$file = $\_FILES\["upfile"\];
if($max\_file\_size < $file\["size"\])
//检查文件大小
{
echo "文件太大!";
exit;
}
if(!in\_array($file\["type"\], $uptypes))
//检查文件类型
{
echo "文件类型不符!".$file\["type"\];
exit;
}
if(!file\_exists($destination\_folder))
{
mkdir($destination\_folder);
}
$filename=$file\["tmp\_name"\];
$image\_size = getimagesize($filename);
$pinfo=pathinfo($file\["name"\]);
$ftype=$pinfo\['extension'\];
$destination = $destination\_folder.time().".".$ftype;
if (file\_exists($destination) && $overwrite != true)
{
echo "同名文件已经存在了";
exit;
}
if(!move\_uploaded\_file ($filename, $destination))
{
echo "移动文件出错";
exit;
}
$pinfo=pathinfo($destination);
$fname=$pinfo\[basename\];
echo " <font color=red>已经成功上传</font><br>文件名: <font color=blue>".$destination\_folder.$fname."</font><br>";
echo " 宽度:".$image\_size\[\];
echo " 长度:".$image\_size\[\];
echo "<br> 大小:".$file\["size"\]." bytes";
if($watermark==)
{
$iinfo=getimagesize($destination,$iinfo);
$nimage=imagecreatetruecolor($image\_size\[\],$image\_size\[\]);
$white=imagecolorallocate($nimage,,,);
$black=imagecolorallocate($nimage,,,);
$red=imagecolorallocate($nimage,,,);
imagefill($nimage,,,$white);
switch ($iinfo\[\])
{
case :
$simage =imagecreatefromgif($destination);
break;
case :
$simage =imagecreatefromjpeg($destination);
break;
case :
$simage =imagecreatefrompng($destination);
break;
case :
$simage =imagecreatefromwbmp($destination);
break;
default:
die("不支持的文件类型");
exit;
}
imagecopy($nimage,$simage,,,,,$image\_size\[\],$image\_size\[\]);
imagefilledrectangle($nimage,,$image\_size\[\]-,,$image\_size\[\],$white);
switch($watertype)
{
case : //加水印字符串
imagestring($nimage,,,$image\_size\[\]-,$waterstring,$black);
break;
case : //加水印图片
$simage1 =imagecreatefromgif("xplore.gif");
imagecopy($nimage,$simage1,,,,,,);
imagedestroy($simage1);
break;
}
switch ($iinfo\[\])
{
case :
//imagegif($nimage, $destination);
imagejpeg($nimage, $destination);
break;
case :
imagejpeg($nimage, $destination);
break;
case :
imagepng($nimage, $destination);
break;
case :
imagewbmp($nimage, $destination);
//imagejpeg($nimage, $destination);
break;
}
//覆盖原上传文件
imagedestroy($nimage);
imagedestroy($simage);
}
if($imgpreview==)
{
echo "<br>图片预览:<br>";
echo "<img src=\\"".$destination."\\" width=".($image\_size\[\]\*$imgpreviewsize)." height=".($image\_size\[\]\*$imgpreviewsize);
echo " alt=\\"图片预览:\\r文件名:".$destination."\\r上传时间:\\">";
}
}
?>
手机扫一扫
移动阅读更方便
你可能感兴趣的文章