Skip to content

Commit 70f712d

Browse files
committed
代码优化
1 parent 8ea9874 commit 70f712d

File tree

7 files changed

+88
-193
lines changed

7 files changed

+88
-193
lines changed

README.md

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
### 安装
77
```
8-
composer require ibrand/phantommagick
8+
composer require ibrand/laravel-miniprogram-poster
99
1010
```
1111

@@ -21,17 +21,15 @@ composer require ibrand/phantommagick
2121
```
2222
iBrand\Poster\PhantoMmagickServiceProvider::class
2323
```
24-
* 发布配置文件
2524
```
25+
php artisan storage:link
2626
php artisan vendor:publish --provider="iBrand\Poster\PhantoMmagickServiceProvider" --tag="config"
2727
```
28-
2928
### 配置项
3029

3130
```
32-
config/phantommagick.php
33-
[
34-
//定义图片存储位置
31+
return [
32+
//图片存储位置
3533
'disks' => [
3634
'MiniProgramShare' => [
3735
'driver' => 'local',
@@ -48,37 +46,22 @@ composer require ibrand/phantommagick
4846
'quality' => 100,
4947
//是否压缩图片
5048
'compress' => true,
51-
]
49+
];
5250
```
5351

5452
###示例
5553
```
56-
tests/ShareImgTest.php
57-
58-
<?php
59-
60-
namespace iBrand\Poster\Test;
61-
62-
class ShareImgTest extends BaseTest
63-
{
64-
/** @test */
65-
public function TestConfig()
66-
{
67-
$config = config('filesystems.disks');
68-
69-
$this->assertArrayHasKey('MiniProgramShare', $config);
70-
}
71-
72-
/** @test */
73-
public function TestGenerateShareImage()
74-
{
75-
$route = 'https://m.baidu.com/';
76-
77-
$file = MiniProgramShareImgTest::generateShareImage($route);
78-
$this->assertTrue(file_exists(__DIR__ . '/' . $file));
79-
}
80-
}
81-
54+
use iBrand\Miniprogram\Poster\MiniProgramShareImg;
55+
56+
$url = 'https://m.baidu.com/';
57+
$result = MiniProgramShareImg::generateShareImage($url);
58+
59+
/*返回值:$result
60+
[
61+
'url' => 'http://xxx.png',图片访问路径
62+
'path' => 'path/to/image', 图片相对路径
63+
]
64+
/*
8265
```
8366

8467
* 生成图片效果如下:<br/>

config/config.php

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,21 @@
1010
*/
1111

1212
return [
13-
//图片存储位置
14-
'disks' => [
15-
'MiniProgramShare' => [
16-
'driver' => 'local',
17-
'root' => storage_path('app/public/share'),
18-
'url' => env('APP_URL').'/storage/share',
19-
'visibility' => 'public',
20-
],
21-
],
22-
'directory' => 'travel',
23-
//图片宽度
24-
'width' => '575',
25-
//放大倍数
26-
'zoomfactor' => 1.5,
27-
//0-100,100质量最高
28-
'quality' => 100,
29-
//是否压缩图片
30-
'compress' => true,
13+
//图片存储位置
14+
'disks' => [
15+
'MiniProgramShare' => [
16+
'driver' => 'local',
17+
'root' => storage_path('app/public/share'),
18+
'url' => env('APP_URL') . '/storage/share',
19+
'visibility' => 'public',
20+
],
21+
],
22+
//图片宽度
23+
'width' => '575px',
24+
//放大倍数
25+
'zoomfactor' => 1.5,
26+
//0-100,100质量最高
27+
'quality' => 100,
28+
//是否压缩图片
29+
'compress' => true,
3130
];

src/MiniProgramShareImg.php

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,59 +16,61 @@
1616

1717
class MiniProgramShareImg
1818
{
19-
public static $conv = null;
19+
public static $conv = null;
2020

21-
public static function init()
22-
{
23-
if (is_null(self::$conv) || !self::$conv instanceof Converter) {
24-
self::$conv = new Converter();
25-
}
21+
public static function init()
22+
{
23+
if (is_null(self::$conv) || !self::$conv instanceof Converter) {
24+
self::$conv = new Converter();
25+
}
2626

27-
return self::$conv;
28-
}
27+
return self::$conv;
28+
}
2929

30-
public static function generateShareImage($route)
31-
{
32-
if (!$route) {
33-
return false;
34-
}
30+
public static function generateShareImage($url, $type = 'default')
31+
{
32+
if (!$url) {
33+
return false;
34+
}
3535

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-
];
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+
];
4141

42-
$saveName = config('ibrand.miniprogram-poster.directory').'/'.md5(uniqid()).'.png';
43-
$root = config('ibrand.miniprogram-poster.disks.MiniProgramShare.root');
44-
$file = $root.'/'.$saveName;
42+
$saveName = date('Ymd') . '/' . $type . '_' . md5(uniqid()) . '.png';
43+
$file = config('ibrand.miniprogram-poster.disks.MiniProgramShare.root') . '/' . $saveName;
4544

46-
try {
47-
$converter = self::init();
45+
try {
46+
$converter = self::init();
4847

49-
$converter->source($route)->toPng($options)->save($file);
48+
$converter->source($url)->toPng($options)->save($file);
5049

51-
if (config('ibrand.miniprogram-poster.compress', true)) {
52-
self::imagePngSizeAdd($file);
53-
}
50+
if (config('ibrand.miniprogram-poster.compress', true)) {
51+
self::imagePngSizeAdd($file);
52+
}
5453

55-
return Storage::disk('MiniProgramShare')->url($saveName);
56-
} catch (\Exception $exception) {
57-
return false;
58-
}
59-
}
54+
return [
55+
'url' => Storage::disk('MiniProgramShare')->url($saveName),
56+
'path' => $saveName,
57+
];
58+
} catch (\Exception $exception) {
59+
return false;
60+
}
61+
}
6062

61-
public static function imagePngSizeAdd($file)
62-
{
63-
list($width, $height, $type) = getimagesize($file);
64-
$new_width = $width * 1;
65-
$new_height = $height * 1;
63+
public static function imagePngSizeAdd($file)
64+
{
65+
list($width, $height, $type) = getimagesize($file);
66+
$new_width = $width * 1;
67+
$new_height = $height * 1;
6668

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-
}
69+
//header('Content-Type:image/png');
70+
$resource = imagecreatetruecolor($new_width, $new_height);
71+
$image = imagecreatefrompng($file);
72+
imagecopyresampled($resource, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
73+
imagejpeg($resource, $file, config('ibrand.miniprogram-poster.quality'));
74+
imagedestroy($resource);
75+
}
7476
}

tests/BaseTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace iBrand\Poster\Test;
44

5-
use Illuminate\Foundation\Testing\DatabaseMigrations;
65
use Orchestra\Testbench\TestCase;
76

87
abstract class BaseTest extends TestCase

tests/ShareImgTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ public function TestConfig()
1818
/** @test */
1919
public function TestGenerateShareImage()
2020
{
21-
$route = 'https://m.baidu.com/';
21+
$url = 'https://m.baidu.com/';
22+
$result = MiniProgramShareImg::generateShareImage($url, 'travel');
23+
$this->assertTrue(Storage::disk('MiniProgramShare')->exists($result['path']));
2224

23-
$url = MiniProgramShareImg::generateShareImage($route);
24-
$this->assertTrue(Storage::disk('MiniProgramShare')->exists($url));
25+
$result = MiniProgramShareImg::generateShareImage('');
26+
$this->assertFalse($result);
27+
28+
$url = 'www.xxxx.com/';
29+
$result = MiniProgramShareImg::generateShareImage($url);
30+
$this->assertFalse($result);
2531
}
2632
}

tests/config/filesystems.php

Lines changed: 0 additions & 72 deletions
This file was deleted.

tests/config/phantommagick.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)