PHP
在 PHP 中建立圖片和 PDF
用 PHP SDK 從範本生成圖片、PDF 或託管的圖片網址。
每個方法都接受範本 ID 和一個選項陣列。所有選項都是選填。
建立圖片
createImage 會渲染範本,並回傳圖片的位元組。
範本 ID,例如 tpl_xxx。
要放進範本圖層的值。每個項目都是一個陣列,用 name 指定一個圖層。你沒有列出的圖層會保留範本中的值。每種圖層接受哪些欄位,請見 Modifications。
圖片格式:png、jpeg 或 webp。
$result = $client->createImage('tpl_xxx', [
'modifications' => [
['name' => 'headline', 'text' => 'Launch promo'],
['name' => 'price', 'text' => '$29'],
],
'format' => 'webp',
]);
if (isset($result['error'])) {
throw new RuntimeException($result['error']['message']);
}
file_put_contents('launch-promo.webp', $result['result']);
在網頁請求中,請連同對應的 Content-Type 標頭一起傳送位元組,例如 image/webp。
建立 PDF
createPdf 接受相同的 modifications 選項,沒有 format 選項。
$result = $client->createPdf('tpl_invoice', [
'modifications' => [
['name' => 'customer', 'text' => 'Acme Studio'],
[
'name' => 'line-items',
'columns' => ['Item', 'Qty', 'Total'],
'rows' => [['Item' => 'Automate plan', 'Qty' => 1, 'Total' => '$29.00']],
],
],
]);
if (isset($result['error'])) {
throw new RuntimeException($result['error']['message']);
}
file_put_contents('invoice.pdf', $result['result']);
建立已儲存的圖片
createStoredImage 會渲染圖片並儲存在 Bannerify 的 CDN,回傳網址而不是位元組。網址以 https://images.bannerify.co/files/ 開頭。當你想把連結放進電子郵件、資料庫的資料列或無程式碼工具時,就可以使用它。
它接受與 createImage 相同的選項。
$result = $client->createStoredImage('tpl_xxx', [
'modifications' => [
['name' => 'headline', 'text' => 'Welcome back'],
],
]);
if (isset($result['error'])) {
throw new RuntimeException($result['error']['message']);
}
echo $result['result'];
PHP SDK 無法傳送 s3Config。若要把檔案儲存到你自己的儲存貯體,請透過 REST 呼叫建立已儲存的圖片。
處理結果
三個方法都會回傳 ['result' => ...] 或 ['error' => ...]。請見處理錯誤。