スナックelve 本店

バツイチ40代女の日記です

iPhoneのホームに年と月を表示したい

ここSafariでブクマ(?)で解決(爆)
cal.hokkyokudesign.com

<追記>
なんか最新は180pxらしいので直しました~(;´Д`)

<?php
// コンテントタイプを設定します
header('Content-Type: image/png');

// 画像を生成します
$im = imagecreatetruecolor(180, 180);

// いくつかの色を生成します
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);

//ましろ
imagefill($im, 0, 0, $white);


// 描画する文字列
$text1 = date('Y');
$text2 = date('m');

// フォント
$font = "./rounded-x-mgenplus-2cp-heavy.ttf";

//文字幅とか(年)
$str1 = imagettfbbox(45, 0, $font,  $text1);

$textWidth1 = $str1[4] - $str1[6];
$texthigh1 = 45;
$x1 = (180 - $textWidth1) / 2 - 5; //5は適当

//文字幅とか(月)
$str2 = imagettfbbox(50, 0, $font,  $text2);

$textWidth2 = $str2[4] - $str2[6];
$texthigh2 = 55; //5は適当
$x2 = (180 - $textWidth2) / 2;

$y1 = (180 - ($texthigh1 + $texthigh2)) / 2 + $texthigh2 - 5;
$y2 = $y1 + $texthigh2 + 5; //5は適当

// テキストを追加します

imagettftext($im, 45, 0, $x1, $y1, $black, $font, $text1);
imagettftext($im, 50, 0, $x2, $y2, $black, $font, $text2);

// imagepng() を使用して imagejpeg() よりもクリアなテキストにします
imagepng($im);
imagedestroy($im);

?>

参照:GDライブラリで文字を右寄せ出力する | SRIA BLOG – 宮城県仙台市のWEBシステム開発・スマホアプリ開発

比較
f:id:elve:20211209073228j:plain
f:id:elve:20211209073234j:plain
</追記>
まぁまぁ、自分でやってみようや、と。
まずアイコンは普通にサファリでホームに置くとスクリーンショットみたいなのがアイコンになっちゃう。
www.symmetric.co.jp


ふむふむ。ヘッダに設定すればいいぞ、と。サイズは57*57px。
じゃーphpで今の年と月をpngで吐けばいいかな、と。

日本語でひっかかるやーつ。
PHP: imagestring - Manual

date('Y\nm')だとnが出力。ダブルコーテーションか・・・。結局年と月で分けて描く。
PHP: date - Manual

ふぉ、ふぉんと?
PHP: imagettftext - Manual

rounded-x-mgenplus-2cp-heavy.ttf のみ使用。
jikasei.me

phpで吐け、と。(これ、$imと$img混在してるよなぁ・・・)
PHPで画像を動的に生成する【GD編】 | バシャログ。

角度指定があるの忘れて文字出力されなくてハマった・・・w
PHP: imagettftext - Manualのコピー

<?php
// コンテントタイプを設定します
header('Content-Type: image/png');

// 画像を生成します
$im = imagecreatetruecolor(57, 57);

// いくつかの色を生成します
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);

imagefilledrectangle($im, 0, 0, 57, 57, $white);

// 描画する文字列
$text1 = date('Y');
$text2 = date('m');
// フォント自身のパスでパスを置き換えます
$font = "./rounded-x-mgenplus-2cp-heavy.ttf";

// テキストを追加します
imagettftext($im,15,0,1.5,25,$black ,$font,$text1);
imagettftext($im,15,0,15,50,$black ,$font,$text2);

// imagepng() を使用して imagejpeg() よりもクリアなテキストにします
imagepng($im);
imagedestroy($im);
?>

ヌッキ(HTMLで文字コード指定してなかった)
f:id:elve:20211207235508p:plain

いいじゃんいいじゃんヽ(=´▽`=)ノ
f:id:elve:20211207235603p:plain

出来上がり
年月