html代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
<!--
* Created on 2015-5-23
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
-->
<head>
<title> 文件上传</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body>
<form action="Thumb.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="500000"/>
请选择要上传的文件:<input type="file" id="upfile" name="upfile"/>
<input type="submit" value="上传" id="submit">
</form>
</body>
</html>
#############################################
##########################################
<?php
/*
* Created on 2015-5-23
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
header("Content-Type:text/html;charset=utf-8");
$upload="Thumbnail.html";
$dir=dirname(realpath(__FILE__)).DIRECTORY_SEPARATOR;
$maxUploadSize=ini_get('upload_max_filesize');
$err_msg=false;
if(!isset($_FILES['upfile'])){
$err_msg="表单不完全!";
}else{
$fileImg=$_FILES['upfile'];
}
echo $fileImg['error'];
switch($fileImg['error']){
case UPLOAD_ERR_INI_SIZE:
$err_msg="文件超过最大上传限制:$maxUploadSize\n";
break;
case UPLOAD_ERR_PARTIAL:
$err_msg="文件上传不完全,请重新<a href='{$upload}'>上传</a>\n";
break;
case UPLOAD_ERR_NO_FILE:
$err_msg="没有选择文件,请重新<a href='{$upload}'>上传</a>\n";
break;
// case UPLOAD_ERR_FROM_SIZE:
// $err_msg="文件超过页面最大上传限制.";
// echo "okkoko";
// break;
case UPLOAD_ERR_CANT_WRITE:
$err_msg="文件写入失败,请重新<a href='{$upload}'>上传</a>\n";
break;
case UPLOAD_ERR_NO_TMP_DIR:
$err_msg="没有临时文件夹,请重新<a href='{$upload}'>上传</a>\n";
break;
case UPLOAD_ERR_OK:
break;
default:
$err_msg="未知错误.请重新<a href='{$upload}'>上传</a>\n";
}
//echo $fileImg['type'];
// if(!in_array($fileImg['type'],array('image/jpeg','image/pjpeg','image/png','image/gif'))){
// $err_msg="只允许上传.png或.jpeg图片,请重新<a href='{$upload}'>上传</a>\n";
//}
if(!$err_msg){
if(!move_uploaded_file($fileImg['tmp_name'],$dir.$fileImg['name'])){
$err_msg="移动文件失败,请重新<a href='{$upload}'>上传</a>\n";
}
}
$src_file=$fileImg['name'];
$dest_file="small_".$fileImg['name'];
$imginfo=getimagesize($src_file);
$srcWidth=$imginfo[0];
$srcHeight=$imginfo[1];
$quantity="80";
$new_size=array('width'=>100,'height'=>78);
$radio=max(($srcWidth/$new_size['width']),($srcHeight/$new_size['height']));
$destWidth=(int)$srcWidth/$radio;
$destHeight=(int)$srcHeight/$radio;
if($imginfo[2]==2){
$src_img=imagecreatefromjpeg($src_file);
}elseif($imginfo[2]==3){
//echo "png";
$src_img=imagecreatefrompng($src_file);
}else{
$src_img=imagecreatefromgif($src_file);
}
if(!$src_img){
$err_msg="生成缩略图失败!请重新<a href='{$upload}'>上传</a>\n";
}else{
$dst_img=imagecreatetruecolor($destWidth,$destHeight);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$destWidth,$destHeight,$srcWidth,$srcHeight);
imagejpeg($dst_img,$dest_file,$quantity);
imagedestroy($src_img);
imagedestroy($dst_img);
}
// echo $fileImg['size'];
if($err_msg){
echo $err_msg;
}else{
echo "<img src='{$fileImg['name']}' alt='上传的原始文件' title='上传的原始文件' />";
echo "<img src='{$dest_file}' alt='缩略图' title='缩略文件'/>";
echo "上传成功,生成缩略图成功.";
}
?>