ナビゲーション

PHP

PHP で画像と PDF を作成する

PHP SDK で、テンプレートから画像、PDF、ホスト済みの画像 URL を生成します。

各メソッドは、テンプレート ID とオプションの配列を受け取ります。オプションはすべて省略できます。

画像を作成する

createImage はテンプレートをレンダリングし、画像のバイト列を返します。

#templateIdstringrequired

テンプレート ID。たとえば tpl_xxx です。

#modificationsarray

テンプレートのレイヤーに設定する値。各項目は、name でレイヤーを指定する配列です。指定しなかったレイヤーは、テンプレートの値のままです。各レイヤーが受け付けるフィールドは、Modifications にあります。

#formatstringdefault: png

画像の形式。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']);

Web リクエストでは、バイト列を、対応する 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 に保存し、バイト列の代わりに URL を返します。URL は 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 で呼び出してください。

結果を処理する

3 つのメソッドは、いずれも ['result' => ...] または ['error' => ...] を返します。エラーを処理するを参照してください。