2016-02-26 73 views
0

我有一個PHP腳本cakeChart.php這是生成簡單的蛋糕。PHP gd縮小生成圖像

$image = imagecreatetruecolor(100, 100); 

$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF); 
$gray  = imagecolorallocate($image, 0xC0, 0xC0, 0xC0); 
$navy  = imagecolorallocate($image, 0x00, 0x00, 0x80); 
$red  = imagecolorallocate($image, 0xFF, 0x00, 0x00); 

imagefilledarc($image, 50, 50, 100, 50, 0, 45, $navy, IMG_ARC_PIE); 
imagefilledarc($image, 50, 50, 100, 50, 45, 75 , $gray, IMG_ARC_PIE); 
imagefilledarc($image, 50, 50, 100, 50, 75, 360 , $red, IMG_ARC_PIE); 


header('Content-type: image/png'); 
imagepng($image); 
imagedestroy($image); 

在文件createThumb.php我想從cakeChart.php加載產生的圖像。 喜歡的東西(我知道這是壞的):

$pngImage = imagecreatefrompng("pieChart.php"); 

我想使這個圖像的縮略圖。眼下這個php文件的唯一引用是這個

<a href="pieChart.php" target="blank">PHP pie chart</a><br> 

但我想,以取代這段文字與tumb,whitch將在createThumb.php產生。是否可以用cakeChart.php製作圖像,然後用createThumb.php將它轉換爲縮略圖?

回答

0

您需要其他腳本調用cakeChart.php和調整它,就像這樣:

<?php 
$src = imagecreatefrompng('http://example.com/cakeChart.php'); 

$width = imagesx($src); 
$height = imagesy($src); 
// resize to 50% of original: 
$new_width = $width * .5; 
$new_height = $height * .5; 

$dest = imagecreatetruecolor($new_width, $new_height); 
imagecopyresampled($dest, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

header('Content-type: image/png'); 
imagepng($dest); 
imagedestroy($dest); 
imagedestroy($src); 

那麼你的HTML代碼將引用文件作爲圖像來源:

<a href="pieChart.php" target="blank"> 
    <img src="http://example.com/cakeChartThumb.php" alt="PHP pie chart"> 
</a> 

雖然這會工作,它不是服務器資源的有效使用。即使少量的頁面瀏覽量也可能導致服務器CPU使用率激增並影響性能。你應該真的創建一次這兩個文件並將它們保存到磁盤,像在任何其他圖像文件中那樣在HTML中引用它們。