Skip to content

Commit 468a256

Browse files
committed
生成小程序分享图片
1 parent f9c2d13 commit 468a256

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

README.md

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,87 @@
1-
# laravel-miniprogram-poster
2-
Use Laravel to building a miniprogram poster.
1+
# Laravel 小程序图文海报生成
2+
3+
* 项目基于[**PhantomMagick**](https://github.com/anam-hossain/phantommagick)
4+
5+
6+
### 安装
7+
```
8+
composer require ibrand/phantommagick
9+
10+
```
11+
12+
* 依赖phantomjs。
13+
14+
* 非linux环境下安装:[phantomjs](http://phantomjs.org/download.html)
15+
16+
* mac环境下推荐使用 brew 安装phantomjs
17+
```
18+
brew install phantomjs
19+
```
20+
* 如果laravel版本小于5.5,需要在config/app.php providers数组添加如下代码
21+
```
22+
iBrand\Poster\PhantoMmagickServiceProvider::class
23+
```
24+
* 发布配置文件
25+
```
26+
php artisan vendor:publish --provider="iBrand\Poster\PhantoMmagickServiceProvider" --tag="config"
27+
```
28+
29+
### 配置项
30+
31+
```
32+
config/phantommagick.php
33+
[
34+
//定义图片存储位置
35+
'disks' => [
36+
'MiniProgramShare' => [
37+
'driver' => 'local',
38+
'root' => storage_path('app/public/share'),
39+
'url' => env('APP_URL') . '/storage/share',
40+
'visibility' => 'public',
41+
],
42+
],
43+
//图片宽度
44+
'width' => '575',
45+
//放大倍数
46+
'zoomfactor' => 1.5,
47+
//0-100,100质量最高
48+
'quality' => 100,
49+
//是否压缩图片
50+
'compress' => true,
51+
]
52+
```
53+
54+
###示例
55+
```
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+
$savePath = '/baidu/';
76+
$saveName = md5(time() . mt_rand(10000, 99999)) . '_share' . '.png';
77+
$route = 'https://m.baidu.com/';
78+
79+
MiniProgramShareImgTest::generateShareImage($savePath, $saveName, $route);
80+
$this->assertTrue(file_exists(__DIR__ . $savePath . $saveName));
81+
}
82+
}
83+
84+
```
85+
86+
* 生成图片效果如下:
87+
![效果图](http://admin.dev.tnf.ibrand.cc/storage/uploads/images/2018_09_13/3A3x1TlJgfFZWfbr58lo5exeBQ3rBCbE9VdTEf6H.jpeg)

src/PhantoMmagickServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class PhantoMmagickServiceProvider extends ServiceProvider
99
public function boot()
1010
{
1111
if ($this->app->runningInConsole()) {
12-
$this->publishes([__DIR__ . '/../config/config.php' => config_path('phantommagick.php')]);
12+
$this->publishes([__DIR__ . '/../config/config.php' => config_path('phantommagick.php')], 'config');
1313
}
1414
}
1515

0 commit comments

Comments
 (0)