收费
https://cloud.baidu.com/index.html?track=cp:npinzhuan|pf:pc|pp:left|ci:|pu:495
七牛云
https://www.qiniu.com/
创建存储空间
222.png图片裁剪
https://developer.qiniu.com/dora/manual/1270/the-advanced-treatment-of-images-imagemogr2
222.pngSDK
https://developer.qiniu.com/kodo/sdk/1241/php
halt($uploadMgr->putFile($token, $key, $file))
<pre><pre class='xdebug-var-dump' dir='ltr'>
<small>C:\wamp\www\thinkphp\library\think\Debug.php:165:</small>
<b>array</b> <i>(size=2)</i>
0 <font color='#888a85'>=></font>
<b>array</b> <i>(size=2)</i>
'hash' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'FqGbwpbXBw2xosMwNzorcFO6tTzY'</font> <i>(length=28)</i>
'key' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'2017/110201711061100259084.png'</font> <i>(length=30)</i>
1 <font color='#888a85'>=></font> <font color='#3465a4'>null</font>
</pre></pre>
Paste_Image.png
Paste_Image.png
public/index.php 入口文件
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// [ 应用入口文件 ]
// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
require_once __DIR__.'/../vendor/qiniu/php-sdk-7.2.1/autoload.php';
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';
require_once __DIR__.'/../vendor/qiniu/php-sdk-7.2.1/autoload.php';
qiniu.php
<?php
/**
* Created by PhpStorm.
* User: tong
* Date: 2017/11/3
* Time: 16:01
*/
return [
'ak' => '_KqNX6a-3mYyJrjEnc8kw3RZIwGg9ytxV0pmXXx6',
'sk' => 'JnzNwZ8vgx7lKvAH3ZMWM67LuWofCR7VOr9eXRTD',
'bucket' => 'imoocapp',
'image_url' => 'http://ox0v9otql.bkt.clouddn.com',
];
Upload.php
<?php
/**
* Created by PhpStorm.
* User: tong
* Date: 2017/11/3
* Time: 15:50
*/
namespace app\common\lib;
//引入鉴权类
use Qiniu\Auth;
//引入上传类
use Qiniu\Storage\UploadManager;
/**
* 七牛图片上传基础类库
* Class Upload
* @package app\common\lib
*/
class Upload
{
/**
* 图片上传
*/
public static function image()
{
//halt($_FILES['file']['tmp_name']);
if (!$_FILES['file']['tmp_name']) {
exception('您提交的图片数据不合法', 404);
}
//要上传的文件的
$file = $_FILES['file']['tmp_name'];
//$ext = explode('.', $_FILES['file']['name']);
//$ext = $ext[1];
$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
//构建一个鉴权对象
$config = config('qiniu');
$auth = new Auth($config['ak'], $config['sk']);
//生成上传的token
$token = $auth->uploadToken($config['bucket']);
//上传到七牛后保存的文件名
$key = date('Y') . '/' . date('m') . '/' /
substr(md5($file), 0, 5) .
date('YmdHis') . rand(0, 9999) . '.' . $ext;
//初始化UploadManager类
$uploadMgr = new UploadManager();
list($ret, $err) = $uploadMgr->putFile($token, $key, $file);
//halt($res);
if ($err != null) {
return null;
} else {
return $key;
}
}
}
Image.php
<?php
/**
* Created by PhpStorm.
* User: tong
* Date: 2017/11/3
* Time: 14:44
*/
namespace app\admin\controller;
use app\common\lib\Upload;
use think\Request;
class Image extends Base
{
public function upload0()
{
$file = Request::instance()->file('file');
$info = $file->move('upload');
if ($info && $info->getPathname()) {
$data = [
'status' => 1,
'message' => 'OK',
'data' => '/' . $info->getPathname(),
];
return json_encode($data);
}
return json_encode([['status'] => 0, 'message' => '上传失败']);
}
/**
* 七牛图片上传
*/
public function upload()
{
try {
$image = Upload::image();
} catch (\Exception $e) {
echo json_encode(['status' => 0, 'message' => $e->getMessage()]);
}
if ($image) {
$data = [
'status' => 1,
'message' => 'OK',
'data' => config('qiniu.image_url') . '/' . $image,
];
echo json_encode($data);
exit;
} else {
echo json_encode(['status' => 0, 'message' => '上传失败']);
}
}
}
Paste_Image.png
Paste_Image.png
网友评论