|
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 | + |
0 commit comments