画像
フルエントなimage()およびimageSvg()メソッドを使用して、ラスターおよびベクター画像をPDFに埋め込みます。位置とサイズを完全に制御できます。
完全なサンプル
php
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use TcpdfNext\Document;
Document::create()
->setTitle('Image Examples')
->addPage()
// -- タイトル -----------------------------------------------------------
->setFont('helvetica', style: 'B', size: 18)
->cell(0, 12, 'Embedding Images', newLine: true)
// -- 1. JPEG(フローモード) --------------------------------------------
->setFont('helvetica', style: 'B', size: 13)
->cell(0, 10, '1. JPEG -- auto height', newLine: true)
->image(
file: __DIR__ . '/assets/photo.jpg',
x: null, // 現在のX
y: null, // 現在のY
width: 60, // 幅60mm
height: null, // 自動(アスペクト比維持)
)
// -- 2. 透過付きPNG ---------------------------------------
->setFont('helvetica', style: 'B', size: 13)
->cell(0, 10, '2. PNG with alpha channel', newLine: true)
->image(
file: __DIR__ . '/assets/logo.png',
x: null,
y: null,
width: 40,
height: null,
)
// -- 3. SVGベクターグラフィック ------------------------------------------
->setFont('helvetica', style: 'B', size: 13)
->cell(0, 10, '3. SVG vector image', newLine: true)
->imageSvg(
file: __DIR__ . '/assets/diagram.svg',
x: null,
y: null,
width: 80,
height: 50,
)
// -- 4. 絶対位置指定 ----------------------------------------
->image(
file: __DIR__ . '/assets/badge.png',
x: 150, // 左端から150mm
y: 10, // 上端から10mm
width: 30,
height: 30,
)
->save(__DIR__ . '/images.pdf');
echo 'PDF created.' . PHP_EOL;配置モード
フローモード
xとyにnullを渡します。画像は現在のカーソル位置に配置され、カーソルは下方に進みます:
php
->image(file: 'photo.jpg', x: null, y: null, width: 60, height: null)絶対モード
明示的な座標を渡します。カーソルは移動しません -- ロゴ、バッジ、透かしオーバーレイに最適です:
php
->image(file: 'badge.png', x: 150, y: 10, width: 30, height: 30)スケーリング動作
width | height | 結果 |
|---|---|---|
60 | null | 固定幅、自動高さ(アスペクト比維持) |
null | 40 | 自動幅、固定高さ(アスペクト比維持) |
60 | 40 | 正確な寸法(引き伸ばしの可能性あり) |
null | null | 元のピクセルサイズを96 DPIでmmに変換 |
サポートされるフォーマット
| フォーマット | 備考 |
|---|---|
| JPEG | ベースラインおよびプログレッシブ、CMYKカラースペースサポート |
| PNG | 8ビット、24ビット、完全なアルファ透過付き32ビット |
| SVG | imageSvg()経由 -- パス、テキスト、基本CSSをレンダリング |
画像の周りにテキストを回り込み
画像には絶対位置指定を使用し、multiCell()の幅を制限して重なりを避けます:
php
Document::create()
->setTitle('Text Wrap')
->addPage()
->image(file: 'photo.jpg', x: 140, y: 30, width: 50, height: null)
->setFont('helvetica', size: 11)
->setXY(10, 30)
->multiCell(width: 125, height: 6, text: 'Your paragraph text here...')
->save('text-wrap.pdf');出力
完全なサンプルは、JPEG写真、透過PNG ロゴ、SVGダイアグラム、右上に絶対位置指定されたバッジを含む1ページを生成します。