LoginSignup
1

More than 3 years have passed since last update.

PHPで文字を画像にするやつ

Posted at

ちょっと、そういうのが必要になったので。(WEBGLで日本語フォントが通らないとか・・・)

im.php?str=こんにちは,世界にアクセスすると、カンマ区切りで改行して、こんな画像を生成します。

スクリーンショット 2020-01-18 22.46.15.png

<?php
if( isset($_GET["str"]) ){
    $points = explode(",",$_GET["str"]);
}else{
    $str = ["Hello","World"];
}

$im = imagecreatetruecolor(400,600);
imagefilledrectangle($im, 0, 0, 599, 399, 0x101010);
$font = '/font/NotoSansCJKjp-Regular.otf';
$y = 30;
foreach($points as $point){
    imagettftext($im, 20, 0, 0, $y, 0xffffff, $font, $point);
    $y = $y + 30;
}
header('Content-Type: image/png;');
imagepng($im);
imagedestroy($im);

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
1