Skip to content

Commit 49bf22b

Browse files
committed
1. 添加是否删除废弃图片的配置。2.取消删除字段。3.简化HasPoster代码。4.FIX CS. 5.update test case.
1 parent 1dfcbb4 commit 49bf22b

File tree

7 files changed

+218
-199
lines changed

7 files changed

+218
-199
lines changed

config/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@
2727
'quality' => 9,
2828
//是否压缩图片
2929
'compress' => true,
30+
//是否删除废弃图片文件
31+
'delete'=>true,
3032
];

migrations/create_poster_tables.php.stub

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ class CreatePosterTables extends Migration
1515
$table->integer('posterable_id')->unsigned();
1616
$table->string('posterable_type');
1717
$table->timestamps();
18-
$table->softDeletes();
19-
2018
$table->index(['posterable_id', 'posterable_type']);
2119
});
2220
}

src/HasPoster.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
<?php
22

3+
/*
4+
* This file is part of ibrand/laravel-miniprogram-poster.
5+
*
6+
* (c) iBrand <https://www.ibrand.cc>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace iBrand\Miniprogram\Poster;
413

514
trait HasPoster
615
{
7-
public static function getPosterClassName()
8-
{
9-
return Poster::class;
10-
}
11-
12-
public function posters()
13-
{
14-
return $this->morphMany(self::getPosterClassName(), 'posterable');
15-
}
16-
}
16+
public function posters()
17+
{
18+
return $this->morphMany(Poster::class, 'posterable');
19+
}
20+
}

src/MiniProgramShareImg.php

Lines changed: 129 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -17,131 +17,133 @@
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 \Illuminate\Database\Eloquent\Model $model
89-
* @param array $path
90-
*/
91-
public static function attach(Model $model, array $path)
92-
{
93-
$poster = new Poster(['content' => $path]);
94-
$model->posters()->save($poster);
95-
}
96-
97-
/**
98-
* 关系是否存在
99-
*
100-
* @param \Illuminate\Database\Eloquent\Model $model
101-
*
102-
* @return mixed
103-
*/
104-
public static function exists(Model $model)
105-
{
106-
$poster = Poster::where('posterable_id', $model->id)->where('posterable_type', get_class($model))->first();
107-
if ($poster) {
108-
return $poster;
109-
}
110-
111-
return false;
112-
}
113-
114-
/**
115-
* 生成海报
116-
*
117-
* @param \Illuminate\Database\Eloquent\Model $model
118-
* @param $url
119-
* @param bool $rebuild
120-
*
121-
* @return array|bool
122-
*/
123-
public static function run(Model $model, $url, $rebuild = false)
124-
{
125-
$poster = self::exists($model);
126-
if (!$poster) {
127-
$path = self::generateShareImage($url);
128-
self::attach($model, $path);
129-
} else {
130-
if ($rebuild === false) {
131-
$path = $poster->content;
132-
} else {
133-
$path = self::generateShareImage($url);
134-
$old = $poster->content;
135-
if (!empty($old) && isset($old['path']) && Storage::disk('MiniProgramShare')->exists($old['path'])) {
136-
Storage::disk('MiniProgramShare')->delete($old['path']);
137-
}
138-
139-
$poster->content = $path;
140-
141-
$poster->save();
142-
}
143-
}
144-
145-
return $path;
146-
}
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+
}
147149
}

src/PhantoMmagickServiceProvider.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,28 @@
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+
}
4242
}

0 commit comments

Comments
 (0)