使用方法:直接上传图片,调用photo方法,会返回图片的id
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Models\Pic;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Intervention\Image\ImageManagerStatic as Image; //安装方法 composer require intervention/image
class PhotoController extends CommonController
{
//封面图
public function photo()
{
$file = Input::file('file');
$mimeType = $file->getMimeType(); //文件类型
$realPath = $file->getRealPath(); //临时文件的绝对路径
$entension = $file -> getClientOriginalExtension(); //上传文件的后缀.
$newName = $this->random_file_name().'.'.$entension;
$path = $file -> move(base_path().'/public/uploads/pic/'.date("Y").'/'.date("m").'/'.date("d"),$newName);//移动文件
$filepath = 'uploads/pic/'.date("Y").'/'.date("m").'/'.date("d").'/'.$newName;
$thumbnailpath = base_path().'/public/uploads/pic/'.date("Y").'/'.date("m").'/'.date("d").'/thumbnail';
if(!file_exists($thumbnailpath))//文件夹不存在,先生成文件夹
{
mkdir($thumbnailpath, 0777, true);
}
$img = Image::make($filepath)->resize(400, 400);
$img->save($thumbnailpath.'/'.$newName);
$data['title'] = $newName;
$data['size'] = $this->get_file_size($path);
$data['type'] = $mimeType;
$data['url'] = '/'.$filepath;
$data['delurl'] = 'file='.$newName.'&dt=uploads/pic/'.date("Y").'/'.date("m").'/'.date("d");
$data['thumb'] = '/uploads/pic/'.date("Y").'/'.date("m").'/'.date("d").'/thumbnail/'.$newName;
$data['add_time'] = time();
$uploadData = Pic::create($data);
if($uploadData){
$vdata['id'] = $uploadData->id;
$vdata['thumb'] = $uploadData->thumb;
$vdata['delurl'] = $uploadData->delurl;
$vdata['status'] = 1;
}else{
$vdata['status'] = -1;
$vdata['msg'] = '请求失败,请重试';
}
return response()->json($vdata);
}
//列表图
public function photoList(Request $request)
{
$file = $request->file('file');
$mimeType = $file->getMimeType(); //文件类型
$realPath = $file -> getRealPath(); //临时文件的绝对路径
$entension = $file -> getClientOriginalExtension(); //上传文件的后缀.
$newName = $this->random_file_name().'.'.$entension;
$path = $file -> move(base_path().'/public/uploads/pic'.date("Y").'/'.date("m").'/'.date("d"),$newName);//移动文件
$filepath = 'uploads/pic/'.date("Y").'/'.date("m").'/'.date("d").'/'.$newName;
$thumbnailpath = base_path().'/public/uploads/pic/'.date("Y").'/'.date("m").'/'.date("d").'/thumbnail';
if(!file_exists($thumbnailpath))//文件夹不存在,先生成文件夹
{
mkdir($thumbnailpath, 0777, true);
}
$img = Image::make($filepath)->resize(400, 400)->save($thumbnailpath.'/'.$newName);
$data['title'] = $newName;
$data['size'] = $this->get_file_size($path);
$data['type'] = $mimeType;
$data['url'] = '/'.$filepath;
$data['delurl'] = 'file='.$newName.'&dt=uploads/pic/'.date("Y").'/'.date("m").'/'.date("d");
$data['thumb'] = '/uploads/pic/'.date("Y").'/'.date("m").'/'.date("d").'/thumbnail/'.$newName;
$data['add_time'] = time();
$uploadData = Pic::create($data);
if($uploadData) {
$vdata['id'] = $uploadData->id;
$vdata['thumb'] = $uploadData->thumb;
$vdata['delurl'] = $uploadData->delurl;
$vdata['status'] = 1;
}else{
$vdata['status'] = -1;
$vdata['msg'] = '请求失败,请重试';
}
return response()->json($vdata);
}
public function delphoto(){
if(is_readable(Input::get('dt').'/'.Input::get('file')) && is_readable(Input::get('dt').'/thumbnail/'.Input::get('file'))){
if(unlink(Input::get('dt').'/'.Input::get('file')) && unlink(Input::get('dt').'/thumbnail/'.Input::get('file'))){
$re = Pic::where('id',Input::get('delid'))->delete();
if($re){
$vdata['status'] = 1;
$vdata['msg'] = '删除成功';
}else{
$vdata['status'] = -1;
$vdata['msg'] = '删除失败';
}
}else{
$vdata['status'] = -1;
$vdata['msg'] = '删除失败';
}
return response()->json($vdata);
}
}
// ADD by:era 添加随机命名
protected function random_file_name() {
$alphanum = 'abcdefghijklmnopqrstuvwxyz0123456789';
$len = 5;
$name= str_replace(".", "", microtime(true));
for ($i=0; $i<$len; $i++) {
$name .= $alphanum[rand(0,strlen($alphanum)-1)];
}
return strtolower($name);
}
//转换字节
protected function fix_integer_overflow($size) {
if ($size < 0) {
$size += 2.0 * (PHP_INT_MAX + 1);
}
return $size;
}
protected function get_file_size($file_path, $clear_stat_cache = false) {
if ($clear_stat_cache) {
clearstatcache(true, $file_path);
}
return $this->fix_integer_overflow(filesize($file_path));
}
}
*********************************************************
数据库字段