2009-01-16 20 views

回答

8

下面是一個示例,說明如何做到這一點 - 使用gd function來調用您的圖片,但播放效果不錯並緩存圖片。此示例扮演甚至更​​好通過確保如果瀏覽器已經擁有了所需的圖像,它返回一個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" /> 
1

我知道這個問題是「如何動態地創建一個具有指定數字的圖像?」但是我會解決底層問題。動態圖像處理在CPU上很重要。只是不要這樣做。當然,不要在網絡請求的情況下這樣做。考慮使用靜態圖像,然後根據評分顯示正確的圖像。即使您的評分系統一路走到100,最好擁有100張靜態圖片,而不是一遍又一遍地重繪同一張圖片。

+0

實際上它可以是0.1到9.99之間的任何地方,所以我真的不想手動做。 – 2009-01-16 09:10:52

2

使用靜態圖片,從0〜9,只是將它們組合在頁面上建立大量的:

Your Rating: [image1.jpg][image2.jpg][image3.jpg]

0

也看到 http://ca3.php.net/manual/en/function.imagecopymerge.php#73477

而不是使用和imagestring()的內置或TTF字體寫的, alpha混合例如,你可以用 阿爾法創建自己的角色0-9作爲24位PNG圖片然後將它們與imagecopymerge()合成。這是一個更多的工作,但會給你更多的控制字符集的外觀。

0

爲什麼不將數字設置爲div中的文本,然後使用字體和背景選擇進行設置?

相關問題