20, 'h' => 20),
array('w' => 30, 'h' => 30),
);
private $allow_type = array(
'image/png' => 'png',
'image/jpeg' => 'jpg',
'image/jpg' => 'jpg',
);
/**
* 以二进制流方式接收文件上传
* @param $buff 文件的二进制流
* @return bool|string 成功返回源文件的访问地址,失败返回false
*/
public function uploadimagebybuffone($buff)
{
if (!$buff || ($buff > $this->max_size)) { // 当文件为空或者大于允许上传的上限
return false;
}
$filename = $this->getfilenamefrombuff($buff); // 生成写入的文件名
if (!$filename) {
return false;
}
$file_path = $this->savebufftodisk($filename, $buff); // 保存文件
if (!$file_path) {
return false;
}
$files = $this->createthumb($file_path); // 生成文件的缩略图
return $this->postimagetofastdfsbyfilename($files);
}
/**
* 将图片文件上传的fastdfs
* @param $files 图片数组
* @return bool|string 成功返回源文件的访问地址,失败返回false
*/
private function postimagetofastdfsbyfilename($files)
{
$source = $files['source'];
unset($files['source']);
$tracker = fastdfs_tracker_get_connection();
if (!fastdfs_active_test($tracker)) {
error_log("errno: " . fastdfs_get_last_error_no() . ", error info: " . fastdfs_get_last_error_info());
return false;
}
$storage = fastdfs_tracker_query_storage_store();
if (!$storage) {
error_log("errno: " . fastdfs_get_last_error_no() . ", error info: " . fastdfs_get_last_error_info());
return false;
}
// 保存源文件
$original_uploaded_info = fastdfs_storage_upload_by_filename($source, null, array(), null, $tracker, $storage);
if ($original_uploaded_info) {
unlink($source); // 删除源文件
$group_name = $original_uploaded_info['group_name'];
$remote_filename = $original_uploaded_info['filename'];
foreach ($files as $size => $path) {
// 将文件缩略图以从文件方式上传
$thumbnail_info = fastdfs_storage_upload_slave_by_filename($path, $group_name, $remote_filename, '_' . $size);
unlink($path); // 删除缩略图
}
return '/' . $group_name . '/' . $remote_filename;
} else {
error_log("errno: " . fastdfs_get_last_error_no() . ", error info: " . fastdfs_get_last_error_info());
return false;
}
}
/**
* 根据文件流判断文件的mimes信息并生成要写入的文件名
* @param $buff
* @return bool|string
*/
private function getfilenamefrombuff($buff)
{
// 根据二进制流的mimes信息获取文件的类型
// 通过将过滤掉不符合mimes头的信息
$imagick = new \imagick();
$imagick->readimageblob($buff);
$mimes = $imagick->getimagemimetype();
if (isset($this->allow_type[$mimes]) == false) {
return false;
}
$file_name = md5($buff) . '.' . $this->allow_type[$mimes];
return $file_name;
}
/**
* 将二进制流写入磁盘文件
* @param $filename 要写入的文件名
* @param $buff 内容的二进制流
* @return bool|string
*/
private function savebufftodisk($filename, $buff)
{
$path = $this->image_dir . '/' . $filename;
$fp = fopen($path, 'w');
fwrite($fp, $buff);
fclose($fp);
return is_file($path) ? $path : false;
}
/**
* 根据配置文件创建不同尺寸的缩略图
* @param $file_path
* @return array('宽x高'=>'path/filename')
*/
private function createthumb($file_path)
{
$ret['source'] = $file_path; // 源文件路径
if (!is_file($file_path)) { // 如果没有配置缩略图尺寸或者文件不存在,则直接退出
return $ret;
}
foreach ($this->thumb_size as $size) {
$path = $this->createthumbbysize($file_path, $size['w'], $size['h']);
$key = $size['w'] . 'x' . $size['h'];
$ret[$key] = $path;
}
return $ret;
}
/**
* 根据参数生成缩略图
* @param $file_path 文件路径
* @param $weight 缩略图的宽
* @param $high 缩略图高
* @param int $quality 质量 默认80
* @return string 缩略图保存的路径
*/
private function createthumbbysize($file_path, $weight, $high, $quality = 80)
{
$image = new \think\image(2); # 1=gd, 2=imagick
$ext = pathinfo($file_path, pathinfo_extension); // 获取文件后缀名
$image->open($file_path);
$thumb = uniqid() . '.' . $ext;
$path = $this->image_dir . '/' . $thumb;
$image->thumb($weight, $high)->save($path, $image->type(), $quality);
return $path;
}
}