2
私はHTMLページを持っています。このページでは、動的に作成されたPNG画像を最初にファイルに保存せずに表示します。ダイナミックに作成された画像をHTMLページ内で直接出力するにはどうすればよいですか?
ページ内にイメージを作成しようとすると、もちろん、すでに送信されているヘッダーを示すエラーが表示されます。
サンプルコード:
<!doctype html>
<html>
<head>
<title>A Test Page</title>
</head>
<body>
<h1>An Image</h1>
<?php
$im = imagecreatetruecolor(100, 100);
$white = ImageColorAllocate ($im, 255, 255, 255);
$black = ImageColorAllocate ($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 50, 50, $white);
imagefilledrectangle($im, 0, 50, 50, 100, $black);
imagefilledrectangle($im, 50, 0, 100, 50, $black);
imagefilledrectangle($im, 50, 50, 100, 100, $white);
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
</body>
</html>