Skip to content

Instantly share code, notes, and snippets.

@summic
Created December 1, 2010 07:33
Show Gist options
  • Save summic/723116 to your computer and use it in GitHub Desktop.
Save summic/723116 to your computer and use it in GitHub Desktop.
upload.php
<?php
/*
* 图片上传处理
* todo: 防止post大量图片攻击
*/
ini_set('gd.jpeg_ignore_warning', 1);
$config=array();
$config['img'] = array("jpg","bmp","gif","png","jpeg"); //文件类型
$config['size'] = 10240; //大小
$config['width'] = 400; //定义宽度,高度按等比缩放。
$config['dir'] = getcwd() . "/tmp/"; //目录
if(isset($_FILES['avatar']['tmp_name']) && is_uploaded_file($_FILES['avatar']['tmp_name'])){
$filearr=pathinfo($_FILES['avatar']['name']);
$filetype=$filearr["extension"];
if(!in_array($filetype, $config["img"])){
die("FAILE:错误的文件类型!");
}
if($_FILES['avatar']['size']> $config['size']*1024){
die("FAILE:上传的文件不能超过".$config["size"]."KB!");
}
$newfile = gen_name();
$res = resize($_FILES['avatar']['tmp_name'], $config["dir"].$newfile);
if($res){
list($width, $heigh, $image_type) = getimagesize($config["dir"].$newfile);
$msg=array(
'status' => 1,
'width'=>$width,
'height'=>$heigh,
'file'=>$newfile,
);
if(isset($_GET['mode']) && $_GET['mode'] == 'debug'){
echo '<img src="/tmp/'.$newfile.'" />';
}else{
echo json_encode($msg);
die;
}
}else{
die('FAILE:上传失败');
}
}elseif(isset($_GET['mode']) && $_GET['mode'] == 'debug'){
// 测试
echo'
<form action="" method="post" enctype="multipart/form-data">
<input name="avatar" size="40" type="file" />
<input name="su" size="40" type="submit" />
';
}else{
die(json_encode(array('code'=>400)));
}
//生成唯一图片名
function gen_name()
{
$server_name = $_SERVER['SERVER_NAME'];
$pid = (function_exists('posix_getpid')) ? posix_getpid() : rand(16384, 32767);
$seed = rand(0, 32767);
$t_stamp = str_replace(' ', '', microtime());
$tmp_1 = md5($seed . '-' . $t_stamp);
$tmp_2 = md5($pid . '-' . $server_name);
$tmp_3 = md5($tmp_1 . $tmp_2);
$key = substr($tmp_3, 4, 16);
$user_id = $key . '_' . substr($t_stamp, -7);
return $user_id.'.jpg';
}
/*
裁切图片函数
$image 原图
$dst 裁切后的小图
$dw, $dh 新的宽度和高度
$type 1=等比缩小,裁掉部分区域 2=等比缩小 3=等比缩小,保留全部,补空白
*/
function resize($image,$dst, $dw=400,$dh=400,$type=3){
if(!file_exists($image)){
return false;
}
#取得文件的类型,根据不同的类型建立不同的对象
$imginfo=getimagesize($image);
switch($imginfo[2]){
case 1:
$img = @imagecreatefromgif($image);
break;
case 2:
$img = @imagecreatefromjpeg($image);
break;
case 3:
$img = @imagecreatefrompng($image);
break;
}
if(empty($img)){
if($type!=1){unlink($image);}
return false;
}
#如果是执行调整尺寸操作则
if($type==1){
$w=imagesx($img);
$h=imagesy($img);
$width = $w;
$height = $h;
$nimg = imagecreatetruecolor($dw,$dh);
if($h/$w>$dh/$dw){ #高比较大
$width=$dw;
$height=$h*$dw/$w;
$intnh=$height-$dh;
imagecopyresampled($nimg, $img, 0, -$intnh/1.8, 0, 0, $dw, $height, $w, $h);
}else{ #宽比较大
$height=$dh;
$width=$w*$dh/$h;
$intnw=$width-$dw;
imagecopyresampled($nimg, $img, -$intnw/1.8, 0, 0, 0, $width, $dh, $w, $h);
}
imagejpeg ($nimg,$image);
return true;
}elseif(($type==2 || $type==3)){
$w=imagesx($img);
$h=imagesy($img);
$width = $w;
$height = $h;
if($width>$dw){
$par=$dw/$width;
$width=$dw;
$height=$height*$par;
if($height>$dh){
$par=$dh/$height;
$height=$dh;
$width=$width*$par;
}
}elseif($height>$dh){
$par=$dh/$height;
$height=$dh;
$width=$width*$par;
if($width>$dw){
$par=$dw/$width;
$width=$dw;
$height=$height*$par;
}
}else{
$width=$width;
$height=$height;
}
if($type==3){
$nimg = imagecreatetruecolor($dw,$dh); #新建一个真彩色画布
imagealphablending($nimg, false);
imagesavealpha($nimg, true);
$white = imagecolorallocatealpha($nimg,255,255,255,127);
imagefill($nimg,0,0,$white);
$tmpw=($dw-$width)/2;
$tmph=($dh-$height)/2;
imagecopyresampled($nimg,$img,$tmpw,$tmph,0,0,$width,$height,$w,$h);#重采样拷贝部分图像并调整大小
}
elseif($type==1){
$nimg = imagecreatetruecolor($width,$height); #新建一个真彩色画布
imagecopyresampled($nimg,$img,0,0,0,0,$width,$height,$w,$h);#重采样拷贝部分图像并调整大小
}
imagejpeg ($nimg,$dst); #以jpeg格式将图像输出到浏览器或文件
return $image;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment