1
1
<?php
2
2
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
+
3
12
namespace iBrand \Miniprogram \Poster ;
4
13
5
14
use Anam \PhantomMagick \Converter ;
6
15
use Storage ;
7
16
8
17
class MiniProgramShareImg
9
18
{
10
- public static $ conv = null ;
11
-
12
- public static function init ()
13
- {
14
- if (is_null (self ::$ conv ) || !self ::$ conv instanceof Converter) {
15
- self ::$ conv = new Converter ();
16
- }
17
-
18
- return self ::$ conv ;
19
- }
20
-
21
- public static function generateShareImage ($ route )
22
- {
23
- if (!$ route ) {
24
- return false ;
25
- }
26
-
27
- $ options = [
28
- 'dimension ' => config ('ibrand.miniprogram-poster.width ' , '575px ' ),
29
- 'zoomfactor ' => config ('ibrand.miniprogram-poster.zoomfactor ' , 1.5 ),
30
- 'quality ' => config ('ibrand.miniprogram-poster.quality ' , 100 ),
31
- ];
32
-
33
- $ saveName = config ('ibrand.miniprogram-poster.directory ' ) . '/ ' . md5 (uniqid ()) . '.png ' ;
34
- $ root = config ('ibrand.miniprogram-poster.disks.MiniProgramShare.root ' );
35
- $ file = $ root . '/ ' . $ saveName ;
36
-
37
- try {
38
- $ converter = self ::init ();
39
-
40
- $ converter ->source ($ route )->toPng ($ options )->save ($ file );
41
-
42
- if (config ('ibrand.miniprogram-poster.compress ' , true )) {
43
- self ::imagePngSizeAdd ($ file );
44
- }
45
-
46
- return Storage::disk ('MiniProgramShare ' )->url ($ saveName );
47
-
48
- } catch (\Exception $ exception ) {
49
-
50
- return false ;
51
- }
52
- }
53
-
54
- public static function imagePngSizeAdd ($ file )
55
- {
56
- list ($ width , $ height , $ type ) = getimagesize ($ file );
57
- $ new_width = $ width * 1 ;
58
- $ new_height = $ height * 1 ;
59
-
60
- //header('Content-Type:image/png');
61
- $ resource = imagecreatetruecolor ($ new_width , $ new_height );
62
- $ image = imagecreatefrompng ($ file );
63
- imagecopyresampled ($ resource , $ image , 0 , 0 , 0 , 0 , $ new_width , $ new_height , $ width , $ height );
64
- imagejpeg ($ resource , $ file , config ('ibrand.miniprogram-poster.quality ' ));
65
- imagedestroy ($ resource );
66
- }
67
- }
19
+ public static $ conv = null ;
20
+
21
+ public static function init ()
22
+ {
23
+ if (is_null (self ::$ conv ) || !self ::$ conv instanceof Converter) {
24
+ self ::$ conv = new Converter ();
25
+ }
26
+
27
+ return self ::$ conv ;
28
+ }
29
+
30
+ public static function generateShareImage ($ route )
31
+ {
32
+ if (!$ route ) {
33
+ return false ;
34
+ }
35
+
36
+ $ options = [
37
+ 'dimension ' => config ('ibrand.miniprogram-poster.width ' , '575px ' ),
38
+ 'zoomfactor ' => config ('ibrand.miniprogram-poster.zoomfactor ' , 1.5 ),
39
+ 'quality ' => config ('ibrand.miniprogram-poster.quality ' , 100 ),
40
+ ];
41
+
42
+ $ saveName = config ('ibrand.miniprogram-poster.directory ' ).'/ ' .md5 (uniqid ()).'.png ' ;
43
+ $ root = config ('ibrand.miniprogram-poster.disks.MiniProgramShare.root ' );
44
+ $ file = $ root .'/ ' .$ saveName ;
45
+
46
+ try {
47
+ $ converter = self ::init ();
48
+
49
+ $ converter ->source ($ route )->toPng ($ options )->save ($ file );
50
+
51
+ if (config ('ibrand.miniprogram-poster.compress ' , true )) {
52
+ self ::imagePngSizeAdd ($ file );
53
+ }
54
+
55
+ return Storage::disk ('MiniProgramShare ' )->url ($ saveName );
56
+ } catch (\Exception $ exception ) {
57
+ return false ;
58
+ }
59
+ }
60
+
61
+ public static function imagePngSizeAdd ($ file )
62
+ {
63
+ list ($ width , $ height , $ type ) = getimagesize ($ file );
64
+ $ new_width = $ width * 1 ;
65
+ $ new_height = $ height * 1 ;
66
+
67
+ //header('Content-Type:image/png');
68
+ $ resource = imagecreatetruecolor ($ new_width , $ new_height );
69
+ $ image = imagecreatefrompng ($ file );
70
+ imagecopyresampled ($ resource , $ image , 0 , 0 , 0 , 0 , $ new_width , $ new_height , $ width , $ height );
71
+ imagejpeg ($ resource , $ file , config ('ibrand.miniprogram-poster.quality ' ));
72
+ imagedestroy ($ resource );
73
+ }
74
+ }
0 commit comments