LoginSignup
0

More than 5 years have passed since last update.

【PHP】ImageMagickによる画像処理_スターウォーズOP風画像を作る

Last updated at Posted at 2017-12-15

はじめに

(この記事はオールアバウトアドベントカレンダー15日目記事です)
本日は映画「スターウォーズEP8 / 最後のジェダイ」の公開日になります。めでたい!!!!👏👏👏

楽しみですね。興奮しますね!!!
時事ネタに絡めて、今回はPHPとImageMagickを用いてスターウォーズOP風画像を作成しました。

環境

  • 【言語】PHP
  • 【ライブラリ】ImageMagick
  • 【フォント】源暎ゴシック H-KL:リンク

やったこと

  • 画像の生成
  • 文字の合成
  • 画像の変形(台形)

コード

<?php

$image = new Imagick();
// 画像生成
$image->newImage(480, 270, new ImagickPixel('rgba(8%, 8%, 8%, 1.0)'));
// 拡張子設定
$image->setImageFormat('png');

$draw = new ImagickDraw();
// 文字列(改行コード:\n)
$string = "スターウォーズEP8公開記念 \nIt is a period of civil war. \nRebel spaceships, striking \nfrom a hidden base, have \nwon their first victory";
// フォントの指定
$draw->setFont('../fonts/GenEiGothicKL-1.3/GenEiGothicP-H-KL.otf');
// 文字色を設定
$draw->setFillColor(new ImagickPixel('rgba(255, 198, 60, 1.0)'));
// 文字サイズの指定
$draw->setFontSize(20);
// 描画
$draw->annotation(110, 148, $string);
// 文字列を合成
$image->drawImage($draw);

// 画像サイズ取得
$iw = $image->getImageWidth();
$ih = $image->getImageHeight();
// 傾き係数
$angle_num = 160;

// 歪め用の制御点
$points = array(
    10, 10,
    10 + $angle_num, 10,

    10, $ih - 20,
    10 - $angle_num, $ih - 20,

    $iw - 10, 10,
    $iw - 10 - $angle_num, 10,

    $iw - 10, $ih - 20,
    $iw - 10 + $angle_num, $ih - 20
    );
// 画像を変形
$image->distortImage(Imagick::DISTORTION_PERSPECTIVE, $points, false);

// 画像の出力
header('Content-type: image/png');
echo $image;

?>

出力結果

sw_op.png

今後の改修点

現状のプログラムでは、文字位置が固定です(画像の変形に重きをおいたため)。

今後の改修点としては、フォームから文字を自由に入力できる想定で
サーバー側で文字位置、サイズを自動で調整させた方がより楽しくなるでしょう。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0