PHP 優雅的生成海報

生成海報

基於 GD 庫,生成海報!GitHub 地址,如果覺得好用,給個 star 支持下哈 ☺

安裝

composer require cshaptx4869/poster

示例

run.php

<?php

require_once './vendor/autoload.php';

use Fairy\Poster;

try {
    $poster = new Poster('./images/bg.png');
    $poster->image('./images/avatar.jpg', 94, 52, 82, 82, true)
        ->image('./images/service.png', 40, 310, 665, 566)
        ->image('./images/mini.jpg', 518, 1070, 180, 180)
        ->font('白開水', 26, 220, 86, '150, 151, 150')
        ->font('我們感情深,幫忙助力行不行', 35, 30, 210, '255, 255, 255')
        ->font('單次4小時,專業保潔師爲您解決全屋精細保潔', 24, 30, 270, '255, 255, 255')
        ->font('&#165; 0.00', 40, 50, 946, '255, 0, 0')
        ->font('原價 &#165;199', 20, 50, 990, '150, 151, 150')
        ->line(44, 980, 170, 980, '150, 151, 150', 2)
        ->output();
    exit();
} catch (\Exception $e) {
    file_put_contents('./error.log', $e->getMessage(), FILE_APPEND);
}

方法說明

  • new Poster()
/**
 * 生成一張背景圖
 * @param string $backgroundImage 背景圖片
 * @throws \Exception
 */
public function __construct($backgroundImage)
  • image()
/**
 * 設置圖像
 * @param string $image 本地、網絡、二進制圖片
 * @param int $x 起始x座標
 * @param int $y 起始y座標
 * @param int $width 所佔寬度
 * @param int $height 所佔高度
 * @param bool $circle 是否需要轉成圓形
 * @return $this
 * @throws \Exception
 */
public function image($image, $x, $y, $width, $height, $circle = false)
  • font()
/**
 * 設置文字
 * @param string $text 文本
 * @param float $size 字體大小
 * @param int $x 起始x座標(注意是字體左下角的座標)
 * @param int $y 起始y座標
 * @param string $rgb rgb顏色字符串 逗號隔開
 * @param string $fontFamily 字體
 * @return $this
 * @throws \Exception
 */
public function font($text, $size, $x, $y, $rgb = '255,255,255', $fontFamily = null)
  • line()
/**
 * 設置線條
 * @param int $x1 第1個點x座標
 * @param int $y1 第1個點y座標
 * @param int $x2 第2個點x座標
 * @param int $y2 第2個點y座標
 * @param string $rgb rgb顏色字符串 逗號隔開
 * @param int $weight 線條粗細
 * @return $this
 * @throws \Exception
 */
public function line($x1, $y1, $x2, $y2, $rgb = '255,255,255', $weight = 1)
  • output()
/**
 * 輸出
 * @param null $filename 保存圖片名稱 不寫則瀏覽器輸出
 */
public function output($filename = null)

其他

如果要生成二維碼,推薦使用 endroid/qr-code 包。生成二維碼後再調用本包的 image() 方法即可生成帶有二維碼的海報。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章