|
17 | 17 |
|
18 | 18 | class MiniProgramShareImg
|
19 | 19 | {
|
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 | + } |
149 | 155 | }
|
0 commit comments