Skip to content

Commit 948df8a

Browse files
committed
添加支付存储七牛云
1 parent b27603e commit 948df8a

File tree

5 files changed

+220
-174
lines changed

5 files changed

+220
-174
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"require": {
1919
"php": ">=7.0",
2020
"anam/phantommagick": "~2.0",
21-
"anam/phantomjs-linux-x86-binary": "~2.1.1"
21+
"anam/phantomjs-linux-x86-binary": "~2.1.1",
22+
"overtrue/laravel-filesystem-qiniu": "^1.0"
2223
},
2324
"require-dev": {
2425
"phpunit/phpunit": "~6.0",

config/config.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,25 @@
1010
*/
1111

1212
return [
13+
'default' => [
14+
'storage' => env('DEFAULT_POSTER_STORAGE', 'qiniu'),
15+
],
1316
//图片存储位置
1417
'disks' => [
18+
'qiniu' => [
19+
'driver' => 'qiniu',
20+
//七牛云access_key
21+
'access_key' => env('QINIU_ACCESS_KEY', ''),
22+
//七牛云secret_key
23+
'secret_key' => env('QINIU_SECRET_KEY', ''),
24+
//七牛云文件上传空间
25+
'bucket' => env('QINIU_BUCKET', ''),
26+
//七牛云cdn域名
27+
'domain' => env('QINIU_DOMAIN', ''),
28+
//与cdn域名保持一致
29+
'url' => env('QINIU_DOMAIN', ''),
30+
'root' => storage_path('app/public/qiniu'),
31+
],
1532
'MiniProgramShare' => [
1633
'driver' => 'local',
1734
'root' => storage_path('app/public/share'),
@@ -27,6 +44,6 @@
2744
'quality' => 9,
2845
//是否压缩图片
2946
'compress' => true,
30-
//是否删除废弃图片文件
31-
'delete'=>true,
47+
//是否删除废弃图片文件
48+
'delete' => true,
3249
];

src/MiniProgramShareImg.php

Lines changed: 135 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -17,133 +17,139 @@
1717

1818
class MiniProgramShareImg
1919
{
20-
public static $conv = null;
21-
22-
public static function init()
23-
{
24-
if (is_null(self::$conv) || !self::$conv instanceof Converter) {
25-
self::$conv = new Converter();
26-
}
27-
28-
return self::$conv;
29-
}
30-
31-
/**
32-
* 生成海报.
33-
*
34-
* @param $url
35-
*
36-
* @return array|bool
37-
*/
38-
public static function generateShareImage($url)
39-
{
40-
if (!$url) {
41-
return false;
42-
}
43-
44-
$options = [
45-
'dimension' => config('ibrand.miniprogram-poster.width', '575px'),
46-
'zoomfactor' => config('ibrand.miniprogram-poster.zoomfactor', 1.5),
47-
'quality' => config('ibrand.miniprogram-poster.quality', 100),
48-
];
49-
50-
$saveName = date('Ymd').'/'.md5(uniqid()).'.png';
51-
$file = config('ibrand.miniprogram-poster.disks.MiniProgramShare.root').'/'.$saveName;
52-
53-
$converter = self::init();
54-
55-
$converter->source($url)->toPng($options)->save($file);
56-
57-
if (config('ibrand.miniprogram-poster.compress', true)) {
58-
self::compress($file);
59-
}
60-
61-
return [
62-
'url' => Storage::disk('MiniProgramShare')->url($saveName),
63-
'path' => $saveName,
64-
];
65-
}
66-
67-
/**
68-
* 压缩图片.
69-
*
70-
* @param $file
71-
*/
72-
public static function compress($file)
73-
{
74-
list($width, $height, $type) = getimagesize($file);
75-
$new_width = $width * 1;
76-
$new_height = $height * 1;
77-
78-
$resource = imagecreatetruecolor($new_width, $new_height);
79-
$image = imagecreatefrompng($file);
80-
imagecopyresampled($resource, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
81-
imagepng($resource, $file, config('ibrand.miniprogram-poster.quality', 9));
82-
imagedestroy($resource);
83-
}
84-
85-
/**
86-
* 绑定关系.
87-
*
88-
* @param Model $model
89-
* @param array $path
90-
*
91-
* @return Poster
92-
*/
93-
public static function attach(Model $model, array $path)
94-
{
95-
$poster = new Poster(['content' => $path]);
96-
$model->posters()->save($poster);
97-
98-
return $poster;
99-
}
100-
101-
/**
102-
* 关系是否存在.
103-
*
104-
* @param \Illuminate\Database\Eloquent\Model $model
105-
*
106-
* @return mixed
107-
*/
108-
public static function exists(Model $model)
109-
{
110-
$poster = Poster::where('posterable_id', $model->id)->where('posterable_type', get_class($model))->first();
111-
if ($poster) {
112-
return $poster;
113-
}
114-
115-
return false;
116-
}
117-
118-
/**
119-
* 生成海报.
120-
*
121-
* @param \Illuminate\Database\Eloquent\Model $model
122-
* @param $url
123-
* @param bool $rebuild
124-
*
125-
* @return array|bool
126-
*/
127-
public static function run(Model $model, $url, $rebuild = false)
128-
{
129-
$poster = self::exists($model);
130-
131-
$path = [];
132-
133-
if (!$poster || $rebuild) {
134-
$path = self::generateShareImage($url);
135-
self::attach($model, $path);
136-
}
137-
138-
if ($poster && $rebuild) {
139-
$old = $poster->content;
140-
if (config('ibrand.miniprogram-poster.delete', true) && !empty($old) && isset($old['path']) && Storage::disk('MiniProgramShare')->exists($old['path'])) {
141-
Storage::disk('MiniProgramShare')->delete($old['path']);
142-
}
143-
$poster->content = $path;
144-
$poster->save();
145-
}
146-
147-
return $path;
148-
}
20+
public static $conv = null;
21+
22+
public static function init()
23+
{
24+
if (is_null(self::$conv) || !self::$conv instanceof Converter) {
25+
self::$conv = new Converter();
26+
}
27+
28+
return self::$conv;
29+
}
30+
31+
/**
32+
* 生成海报.
33+
*
34+
* @param $url
35+
*
36+
* @return array|bool
37+
*/
38+
public static function generateShareImage($url)
39+
{
40+
if (!$url) {
41+
return false;
42+
}
43+
44+
$options = [
45+
'dimension' => config('ibrand.miniprogram-poster.width', '575px'),
46+
'zoomfactor' => config('ibrand.miniprogram-poster.zoomfactor', 1.5),
47+
'quality' => config('ibrand.miniprogram-poster.quality', 100),
48+
];
49+
50+
$saveName = date('Ymd') . '/' . md5(uniqid()) . '.png';
51+
$storage = config('ibrand.miniprogram-poster.default.storage');
52+
$file = config('ibrand.miniprogram-poster.disks.' . $storage . '.root') . '/' . $saveName;
53+
54+
$converter = self::init();
55+
56+
$converter->source($url)->toPng($options)->save($file);
57+
58+
if (config('ibrand.miniprogram-poster.compress', true)) {
59+
self::compress($file);
60+
}
61+
62+
if ('qiniu' == $storage) {
63+
Storage::disk($storage)->put($saveName, file_get_contents($file));
64+
}
65+
66+
return [
67+
'url' => Storage::disk($storage)->url($saveName),
68+
'path' => $saveName,
69+
];
70+
}
71+
72+
/**
73+
* 压缩图片.
74+
*
75+
* @param $file
76+
*/
77+
public static function compress($file)
78+
{
79+
list($width, $height, $type) = getimagesize($file);
80+
$new_width = $width * 1;
81+
$new_height = $height * 1;
82+
83+
$resource = imagecreatetruecolor($new_width, $new_height);
84+
$image = imagecreatefrompng($file);
85+
imagecopyresampled($resource, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
86+
imagepng($resource, $file, config('ibrand.miniprogram-poster.quality', 9));
87+
imagedestroy($resource);
88+
}
89+
90+
/**
91+
* 绑定关系.
92+
*
93+
* @param Model $model
94+
* @param array $path
95+
*
96+
* @return Poster
97+
*/
98+
public static function attach(Model $model, array $path)
99+
{
100+
$poster = new Poster(['content' => $path]);
101+
$model->posters()->save($poster);
102+
103+
return $poster;
104+
}
105+
106+
/**
107+
* 关系是否存在.
108+
*
109+
* @param \Illuminate\Database\Eloquent\Model $model
110+
*
111+
* @return mixed
112+
*/
113+
public static function exists(Model $model)
114+
{
115+
$poster = Poster::where('posterable_id', $model->id)->where('posterable_type', get_class($model))->first();
116+
if ($poster) {
117+
return $poster;
118+
}
119+
120+
return false;
121+
}
122+
123+
/**
124+
* 生成海报.
125+
*
126+
* @param \Illuminate\Database\Eloquent\Model $model
127+
* @param $url
128+
* @param bool $rebuild
129+
*
130+
* @return array|bool
131+
*/
132+
public static function run(Model $model, $url, $rebuild = false)
133+
{
134+
$poster = self::exists($model);
135+
136+
$path = [];
137+
138+
if (!$poster || $rebuild) {
139+
$path = self::generateShareImage($url);
140+
self::attach($model, $path);
141+
}
142+
143+
if ($poster && $rebuild) {
144+
$old = $poster->content;
145+
$storage = config('ibrand.miniprogram-poster.default.storage');
146+
if (config('ibrand.miniprogram-poster.delete', true) && !empty($old) && isset($old['path']) && Storage::disk($storage)->exists($old['path'])) {
147+
Storage::disk($storage)->delete($old['path']);
148+
}
149+
$poster->content = $path;
150+
$poster->save();
151+
}
152+
153+
return $path;
154+
}
149155
}

src/PhantoMmagickServiceProvider.php

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,30 @@
1515

1616
class PhantoMmagickServiceProvider extends ServiceProvider
1717
{
18-
public function boot()
19-
{
20-
if ($this->app->runningInConsole()) {
21-
$this->publishes([__DIR__.'/../config/config.php' => config_path('ibrand/miniprogram-poster.php')], 'config');
22-
23-
if (!class_exists('CreatePosterTables')) {
24-
$timestamp = date('Y_m_d_His', time());
25-
$this->publishes([
26-
__DIR__.'/../migrations/create_poster_tables.php.stub' => database_path('migrations/'.$timestamp.'_create_poster_tables.php'),
27-
], 'migrations');
28-
}
29-
}
30-
}
31-
32-
public function register()
33-
{
34-
$this->mergeConfigFrom(
35-
__DIR__.'/../config/config.php', 'ibrand.miniprogram-poster'
36-
);
37-
38-
$filesystems = $this->app['config']->get('filesystems.disks', []);
39-
40-
$this->app['config']->set('filesystems.disks', array_merge(config('ibrand.miniprogram-poster.disks'), $filesystems));
41-
}
18+
public function boot()
19+
{
20+
if ($this->app->runningInConsole()) {
21+
$this->publishes([__DIR__ . '/../config/config.php' => config_path('ibrand/miniprogram-poster.php')], 'config');
22+
23+
if (!class_exists('CreatePosterTables')) {
24+
$timestamp = date('Y_m_d_His', time());
25+
$this->publishes([
26+
__DIR__ . '/../migrations/create_poster_tables.php.stub' => database_path('migrations/' . $timestamp . '_create_poster_tables.php'),
27+
], 'migrations');
28+
}
29+
}
30+
}
31+
32+
public function register()
33+
{
34+
$this->mergeConfigFrom(
35+
__DIR__ . '/../config/config.php', 'ibrand.miniprogram-poster'
36+
);
37+
38+
$filesystems = $this->app['config']->get('filesystems.disks', []);
39+
40+
$this->app['config']->set('filesystems.disks', array_merge(config('ibrand.miniprogram-poster.disks'), $filesystems));
41+
42+
$this->app->register(\Overtrue\LaravelFilesystem\Qiniu\QiniuStorageServiceProvider::class);
43+
}
4244
}

0 commit comments

Comments
 (0)