<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('phpsso', 'phpsso', 0);
pc_base::load_app_class('messagequeue', 'admin' , 0);
pc_base::load_app_func('global', 'admin');
class upp{
private $username, $config;
/**
* 构造函数
*/
public function __construct() {
$this->db = pc_base::load_model('member_model');
pc_base::load_app_func('global');
}
/**
* 上传头像处理
* 传入头像压缩包,解压到指定文件夹后删除非图片文件
*/
public function init() {
$_postbody = file_get_contents('php://input');
$post_data = json_decode($_postbody, true);
$post_data = $_POST;
if ($post_data['user_id']) {
$typeArr = array("jpg", "png", "gif");//允许上传文件格式
$uid = $post_data['user_id'];
$wwwurl = "http://www.i-ev.com/phpsso_server/";
$dir1 = ceil($uid / 10000);
$dir2 = ceil($uid % 10000 / 1000);
$path = 'uploadfile/avatar/'.$dir1.'/'.$dir2.'/'.$uid.'/';
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$name_tmp = $_FILES['file']['tmp_name'];
if (empty($name)) {
echo json_encode(array("error"=>"您还未选择图片"));
exit;
}
$type = strtolower(substr(strrchr($name, '.'), 1)); //获取文件类型
if (!in_array($type, $typeArr)) {
echo json_encode(array("error"=>"清上传jpg,png或gif类型的图片!"));
exit;
}
if ($size > (500 * 1024)) {
echo json_encode(array("error"=>"图片大小已超过500KB!"));
exit;
}
$pic_name = time() . rand(10000, 99999) . "." . $type;//图片名称
//$pic_name = time() . rand(10000, 99999) . ".jpg";//图片名称
$pic_url = $path . $pic_name;//上传后图片路径+名称
if(!file_exists($path)){
mkdir($path, 0777, true);
}
if (move_uploaded_file($name_tmp, $pic_url)) { //临时文件转移到目标文件夹
//$img = imagecreatefromgif($pic_url);
//$pic_url = imagejpeg($img,$toFile.'2222.jpg');
$this->resize($pic_url,$path,180,180);
$this->resize($pic_url,$path,90,90);
$this->resize($pic_url,$path,45,45);
$this->resize($pic_url,$path,30,30);
$aa = $this->resize($pic_url,$path,120,120);
$uppp = $this->db->update(array('avatar'=>'1'), array('uid'=>$post_data['user_id']));
echo json_encode(array('code'=>'OK','info'=>'成功','data'=>$wwwurl.$path.$aa));
} else {
echo json_encode(array('code'=>"321",'info'=>'操作失败'));
}
}else{
echo json_encode(array('code'=>"321",'info'=>'操作失败'));
}
}
/**
* *
*等比缩放
* @param unknown_type $srcImage 源图片路径
* @param unknown_type $toFile 目标图片路径
* @param unknown_type $maxWidth 最大宽
* @param unknown_type $maxHeight 最大高
* @param unknown_type $imgQuality 图片质量
* @return unknown
*/
private function resize($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=99)
{
list($width, $height, $type, $attr) = getimagesize($srcImage);
if($width < $maxWidth || $height < $maxHeight) return ;
switch ($type) {
case 1: $img = imagecreatefromgif($srcImage); break;
case 2: $img = imagecreatefromjpeg($srcImage); break;
case 3: $img = imagecreatefrompng($srcImage); break;
}
$scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例
if($scale < 1) {
$newWidth = floor($scale*$width);
$newHeight = floor($scale*$height);
$newImg = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
$newName = $maxWidth.'x'.$maxHeight;
$toFile = preg_replace("/(.gif|.jpg|.jpeg|.png)/i","",$toFile);
switch($type) {
case 1: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
return "$newName.jpg"; break;
case 2: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
return "$newName.jpg"; break;
case 3: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
return "$newName.jpg"; break;
default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
return "$newName.jpg"; break;
}
imagedestroy($newImg);
}
imagedestroy($img);
return false;
}
}
?>