2016-02-26 80 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中引用它们。