PHP上传图片OSS同步更新
RePHP上传图片OSS同步更新
下面附上我的代码,PHP用的是Thinkphp3.2的框架
public function add(){
if(IS_POST){
$oss_sdk_service = new \ALIOSS();
//设置是否打开curl调试模式
$oss_sdk_service->set_debug_mode(true);
$news_mod = D('News');
$data ['title'] = I('post.title','','htmlspecialchars');
$data['ord'] = 0;
$data['addtime'] = time();
$data['status'] = 0;
if (isset($_FILES['pic'])){
//文件上传配置,可以生成缩略图
$date11 =date('Ymd');
$upload = new \Think\UploadFile();// 实例化上传类
$upload->maxSize = 80000000 ;// 设置附件上传大小
$upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->allowTypes = array('image/jpg','image/jpeg','image/pjpeg','image/png','image/gif');
$upload->savePath = './Upload/news/'.$date11.'/';// 设置附件上传目录
$upload->saveRule = 'uniqid';
//缩略图配置
$upload->thumb = true;
$upload->thumbMaxWidth = '300';
$upload->thumbMaxHeight = '380';
$upload->thumbPrefix = '';
$upload->thumbSuffix ='.300';
$upload->thumbRemoveOrigin = false;
//开始上传
if($upload->upload()){
$info = $upload->getUploadFileInfo();
//大图
$data['picurl'] = '/Upload/news/'.$date11.'/'.$info[0]['savename'];
//上传到OSS
$picurl ='./Upload/news/'.$date11.'/'.$info[0]['savename'];
//缩略图
$thumbpic = getThumb($picurl,6);
$thumbfilename = end ( explode ( '/', $thumbpic ) );;
upload_by_file($oss_sdk_service,'Upload/news/'.$date11.'/'.$info[0]['savename'], $picurl);
upload_by_file($oss_sdk_service,'Upload/news/'.$date11.'/'.$thumbfilename, $thumbpic);
}
}
$result = $news_mod->add($data);
if(false !== $result){
$this->success ( '新增成功!', U('Home/Index/add') );
}
else{
$this->error ( '新增巡失败!' );
}
}else{
$this->display();
}
}
html代码
上传图片
标题:
上传图片:
-------------------------
RePHP上传图片OSS同步更新
//获取图片的缩略图名称
function getThumb($image,$lx) {
$filename = end ( explode ( '/', $image ) );
$filename1=explode('.',$filename);
if ($filename1) {
if($lx==1){
$lxtt = '.120.';
}else if($lx==2){
$lxtt = '.150.';
}else if($lx==3){
$lxtt = '.180.';
}else if($lx==4){
$lxtt = '.200.';
}else if($lx==5){
$lxtt = '.280.';
}else if($lx==6){
$lxtt = '.300.';
}
$files = $filename1[0].$lxtt.$filename1[1];
$newfile=str_replace($filename,$files,$image);
return $newfile;
}
}
-------------------------
RePHP上传图片OSS同步更新
希望这个帖子能加金啊
-------------------------
RePHP上传图片OSS同步更新
加个函数upload_by_file()
//通过路径上传文件
function upload_by_file($obj,$object,$file_path){
$bucket = 'dengwei1999';
// $object = '115001_1313.jpg';
// $file_path = '../pic/115001_1313.jpg';
$response = $obj->upload_file_by_file($bucket,$object,$file_path);
// _format($response);
}
赞0
踩0