<?php
/*
code by afen
www.afen.cn 2009-7-23
Note: This thumbnail creation script requires the GD PHP Extension.
If GD is not installed correctly PHP does not render this page correctly
and SWFUpload will get "stuck" never calling uploadSuccess or uploadError
// Get the session Id passed from SWFUpload. We have to do this to work-around the Flash Player Cookie Bug
if (isset($_POST["PHPSESSID"])) {
session_id($_POST["PHPSESSID"]);
}
session_start();
ini_set("html_errors", "0");*/
$updir="./upload/";//上传目录
$dirtype="2";//目录保存方式1:年/月/日;2:年/月;默认:年
$renamed="1";//是否重命名1表示重命名0表示用原来的文件名
$overwrite="1";//是否覆盖1表示覆盖0表示不覆盖
if (isset($_FILES["Filedata"]) && is_uploaded_file($_FILES["Filedata"]["tmp_name"]) && $_FILES["Filedata"]["error"] == 0) {
//上传文件赋值给$upload_file
$upload_file=$_FILES["Filedata"];
//获取文件类型
$file_info=pathinfo($upload_file["name"]);
//获取文件扩展名
$file_ext=$file_info["extension"];
//设置路径方式
switch($dirtype){
case '1':
$m_dir=date(Y)."/".date(m)."/".date(d)."/";
break;
case '2':
$m_dir=date(Y)."/".date(m)."/";
break;
default:
$m_dir=date(Y)."/";
break;
}
//设置上传的路径
$upload_path=$updir.$m_dir;
//建立文件夹
create($upload_path);
//需要重命名的
if($renamed){
list($usec, $sec) = explode(" ",microtime());
$upload_file['name']=substr($usec,2).'.'.$file_ext;
//unset($usec);
unset($sec);
}
//设置默认服务端文件名
$upload_file['filename']=$upload_path.$upload_file['name'];
//缩略图文件名(无后辍名)
$upload_file['smallname']=$upload_path."sm_".substr($usec,2);
//检查文件是否存在
if(file_exists($upload_file['filename'])){
if($overwrite){
@unlink($upload_file['filename']);
}else{
$j=0;
do{
$j++;
$temp_file=str_replace('.'.$file_ext,'('.$j.').'.$file_ext,$upload_file['filename']);
}while (file_exists($temp_file));
$upload_file['filename']=$temp_file;
unset($temp_file);
unset($j);
}
}
if($upload_file['size']){
$im = imagecreatefromjpeg($upload_file["tmp_name"]);
if(!$im){
$im = imagecreatefrompng($upload_file["tmp_name"]);
}
if(!$im){
$im = imagecreatefromgif($upload_file["tmp_name"]);
}
if($im){
if(file_exists($upload_file['smallname'])){
@unlink($upload_file['smallname']);
}
ResizeImage($im,100,100,$upload_file['smallname']);
ImageDestroy ($im);
}else{
echo "ERROR:缩略图创建失败,可能您上传的类型不对";
exit(0);
}
}
if(@move_uploaded_file($upload_file["tmp_name"],$upload_file["filename"])){
//下面插入一段把路径保存到数据库中的代码;
// Create a pretend file id, this might have come from a database.
//产生一个上传文件id,这可能来自数据库;
//这里必需echo内容可以是文件id或许数据库中的id,不然程序会出现错误,没有内容传回到index.php表单中的hidFileID中
echo "FILEID:" .$upload_file['smallname'].".jpg";
//echo "FILEID:" . $file_id; // Return the file id to the script
}else{
echo '';
}
} else {
echo ' '; // I have to return something or SWFUpload won't fire uploadSuccess
}
//建立文件夹
function create($dir)
{
if (!is_dir($dir))
{
$temp = explode('/',$dir);
$cur_dir = '';
for($i=0;$i<count($temp);$i++)
{
$cur_dir .= $temp[$i].'/';
if (!is_dir($cur_dir))
{
@mkdir($cur_dir,0777);
//@fopen("$cur_dir/index.htm","a");
}
}
}
}
//生成缩略图
function ResizeImage($im,$maxwidth,$maxheight,$name){
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$RESIZEWIDTH=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$RESIZEHEIGHT=true;
}
if($RESIZEWIDTH && $RESIZEHEIGHT){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($RESIZEWIDTH){
$ratio = $widthratio;
}elseif($RESIZEHEIGHT){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
imagejpeg($newim,$name . ".jpg");
imagedestroy ($newim);
}else{
imagejpeg($im,$name . ".jpg");
}
}
?>