At present, version 2.0 has been updated, which supports the function of associating the generated posters with Model and whether to regenerate posters or not. For details, please go to Github: laravel-miniprogram-poster

Wechat small program official did not provide a way to share to the circle of friends, so the current basic entire industry is to use the generation of graphic posters to send to the circle of friends, and then identify the sun code into the small program.

Many students have provided some solutions through Google or Baidu, but the results are not very ideal after we use them, mainly reflected in the following aspects:

  1. Fonts written through PHP do not look good.
  2. The background picture and wechat avatar are not clear enough after being combined.
  3. Unable to achieve some complex effects.
  4. The implementation process is also complicated.

In the end, we found a reasonable implementation based on PhantomJS, using PhantomJS invisible browser screenshots to generate posters.

PhantomJS is a WebKit-based JavaScript API. It uses QtWebKit as its core browser functionality, using WebKit to compile, interpret and execute JavaScript code. Anything you can do in a WebKit-based browser, it does. Not only is it an invisible browser, it provides CSS selectors, support for Web standards, DOM manipulation, JSON, HTML5, Canvas, SVG, etc., but it also provides file I/O operations so that you can read and write files to the operating system, etc. PhantomJS is used for a wide range of things, including network monitoring, Web screenshots, browser-free Web testing, page access automation, and more.

It has the following advantages:

  1. Based on HTML can achieve complex text, pictures, shadow effects.
  2. Reasonable clarity and file size
  3. Simple to use, plug and play

Package address: laravel-miniprogram-poster

experience

Scan the code to enter the product details page to share the generated image and text experience

The installation

composer require ibrand/laravel-miniprogram-poster 
Copy the code
  • Below Laravel5.5 version, the config/app. PHP file ‘will add iBrand \ Poster \ PhantoMmagickServiceProvider: : class

  • The image is saved in storage/app/public, so PHP artisan Storage :link is required

  • For custom configuration please execute PHP artisan vendor: publish – provider = “iBrand \ Poster \ PhantoMmagickServiceProvider” tag = “config”

Configuration items

    return[// Image storage location'disks'= > ['MiniProgramShare'= > ['driver'= >'local'.'root'       => storage_path('app/public/share'),
    			'url'        => env('APP_URL').'/storage/share'.'visibility'= >'public',],], // Image width'width'= >'575px', // Magnification'zoomfactor'=> 1.5, //0-100,100 quality highest'quality'=> 100, // Whether to compress the image'compress'= >true,];Copy the code

use

use iBrand\Miniprogram\Poster\MiniProgramShareImg;
    
$url = 'https://www.ibrand.cc/';
$result = MiniProgramShareImg::generateShareImage($url);

Copy the code

Return result:

    [
        'url'= >'http://xxx.png', image access URL'path'= >'path/to/image', image file path]Copy the code

Font installation

If you need to implement complex font effects, you need to install fonts. For example, there is no Microsoft Yahei font on centos, so if the generated images have special fonts, you need to install fonts on the server.

  • Windows Copy the downloaded font file to the C:Windows\Fonts directory or double-click the font file to install it
  • MAC download font file Double-click font file to install
  • centos
Install Microsoft Yahei
wget -P /tmp/ https://iyoyo.oss-cn-hangzhou.aliyuncs.com/mirror/fonts/msyh.ttf
wget -P /tmp/ https://iyoyo.oss-cn-hangzhou.aliyuncs.com/mirror/fonts/msyhbd.ttf
wget -P /tmp/ https://iyoyo.oss-cn-hangzhou.aliyuncs.com/mirror/fonts/msyhl.ttf
cd /usr/share/fonts/lyx/
mkdir chinese
cd chinese
mv /tmp/msyhbd.ttf ./
chmod 755 *.ttf
yum -y install mkfontscale
mkfontscale
mkfontdir
fc-cache -fv
Copy the code

Resource

The project is based on PhantomMagick

The discussion