[PHP]Imagickサンプル

Imagickサンプル

<?php
$_data = array(
        'dir'       => './output/',
        'filename'  => date('YmdHis'),
        'filetype'  => 'png',
        'fontsize'  => 24,
        'width'     => 1000,
        'height'    => 640,
        'text'      => 'sample text',
        'textcolor' => '000000',
        'color'     => 'eeeeee',
        );
$filepath = realpath($_data['dir']).DIRECTORY_SEPARATOR.$_data['filename'];

$draw = new ImagickDraw();
$draw->setFont( realpath('./font/GenEiGothicP-Regular.otf') );
$draw->setFontSize($_data['fontsize']);
$draw->setFillColor(new ImagickPixel('#'.$_data['textcolor']));
$draw->setTextAlignment(Imagick::ALIGN_CENTER);
$draw->annotation($_data['width']*0.5,$_data['height']*0.5, $_data['text'] );

$canvas = new Imagick();
$canvas->newImage($_data['width'], $_data['height'], new ImagickPixel('#'.$_data['color']), $_data['filetype']);
$canvas->drawImage($draw);
$canvas->writeImage($filepath);
$canvas->clear();