次のようなプレースホルダー画像があります。
Your rating is:
[rating here]
私のPHPコードは、プレースホルダー画像の空白部分が省略されている評価番号を動的に挿入することになっています。どうやってやるの?
次のようなプレースホルダー画像があります。
Your rating is:
[rating here]
私のPHPコードは、プレースホルダー画像の空白部分が省略されている評価番号を動的に挿入することになっています。どうやってやるの?
これを行う方法の例を次に示します-gd関数呼び出しを使用して画像を作成しますが、うまく再生して画像をキャッシュします。このサンプルは、必要な画像が既にブラウザにある場合に 304 ...
#here's where we'll store the cached images
$cachedir=$_SERVER['DOCUMENT_ROOT'].'/imgcache/'
#get the score and sanitize it
$score=$_GET['score'];
if (preg_match('/^[0-9]\.[0-9]{1,2}$/', $score)
{
#figure out filename of cached file
$file=$cachedir.'score'.$score.'gif';
#regenerate cached image
if (!file_exists($file))
{
#generate image - this is lifted straight from the php
#manual, you'll need to work out how to make your
#image, but this will get you started
#load a background image
$im = imagecreatefrompng("images/button1.png");
#allocate color for the text
$orange = imagecolorallocate($im, 220, 210, 60);
#attempt to centralise the text
$px = (imagesx($im) - 7.5 * strlen($score)) / 2;
imagestring($im, 3, $px, 9, $score, $orange);
#save to cache
imagegif($im, $file);
imagedestroy($im);
}
#return image to browser, but return a 304 if they already have it
$mtime=filemtime($file);
$headers = apache_request_headers();
if (isset($headers['If-Modified-Since']) &&
(strtotime($headers['If-Modified-Since']) >= $mtime))
{
// Client's cache IS current, so we just respond '304 Not Modified'.
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $mtime).' GMT', true, 304);
exit;
}
header('Content-Type:image/gif');
header('Content-Length: '.filesize($file));
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $mtime).' GMT');
readfile($file);
}
else
{
header("HTTP/1.0 401 Invalid score requested");
}
これを image.php に入れる場合、イメージタグで次のように使用します
<img src="image.php?score=5.5" alt="5.5" />
0 から 9 までの範囲の静止画像を使用し、それらをページ上で組み合わせて多数を構築します。
Your Rating: [image1.jpg][image2.jpg][image3.jpg]
「指定した番号の画像を動的に作成するにはどうすればよいですか?」という質問があることは知っています。しかし、代わりに根本的な問題に対処します。動的な画像操作はCPUに負担がかかります。ただそれをしないでください。そして確かに、Webリクエストのコンテキストでそれを行わないでください。代わりに静止画像を使用することを検討してから、評価に応じて正しい画像を表示してください。評価システムが100まで上がったとしても、同じ画像を何度も再描画し続けるよりも、100枚の静止画像を使用する方がよいでしょう。
http://ca3.php.net/manual/en/function.imagecopymerge.php#73477のアルファ ブレンディングの例も参照して ください。
imagestring() を使用して組み込みまたは TTF フォントで書き込む代わりに、独自の 0 ~ 9 文字をアルファ ブレンディングを使用して 24 ビット PNG 画像として作成し、それらを imagecopymerge() で合成できます。少し手間がかかりますが、文字セットの外観をより細かく制御できます。
数値を div 内のテキストとして設定してから、フォントと背景を選択してスタイルを設定してみませんか?