我有一个占位符图像说:
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中,你将在图像标记中使用如下