PHP制作3D扇形统计图以及对图片进行缩放操作实例

时间:2021-05-26

1、利用php gd库的函数绘制3D扇形统计图

<?php
header("content-type","text/html;charset=utf-8");

$image = imagecreatetruecolor(100, 100);

$white = imagecolorallocate($image,0xff,0xff,0xff);
$gray = imagecolorallocate($image, 0xc0, 0xc0, 0xc0);
$darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
$darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50);
$red = imagecolorallocate($image, 0xff, 0x00, 0x00);
$darkred = imagecolorallocate($image, 0x90, 0x00, 0x00);

imagefill($image, 0, 0, $white);

for($i = 60; $i > 50; $i--)
{
imagefilledarc($image, 50, $i, 100, 50, -160, 40, $darknavy, IMG_ARC_PIE);
imagefilledarc($image, 50, $i, 100, 50, 40, 75, $darkgray, IMG_ARC_PIE);
imagefilledarc($image, 50, $i, 100, 50, 75, 200, $darkred, IMG_ARC_PIE);
}

imagefilledarc($image, 50, 50, 100, 50, -160, 40, $darknavy, IMG_ARC_PIE);
imagefilledarc($image, 50, 50, 100, 50, 40, 75, $darkgray, IMG_ARC_PIE);
imagefilledarc($image, 50, 50, 100, 50, 75, 200, $darkred, IMG_ARC_PIE);

imagestring($image, 3, 15, 55, "30%", $white);
imagestring($image, 3, 45, 35, "60%", $white);
imagestring($image, 3, 60, 60, "10%", $white);

header("content-type:image/png");
imagepng($image);

imagedestroy($image);
?>

效果:

2、对图片进行缩放

<div>
<h4>原图大小</h4>
<img src="1.png">
</div>
<?php
header("content-type","text/html;charset=utf-8");
/*
*图片缩放
*@param string $filename 图片的url
*@param int $width 设置图片缩放的最大宽度
*@param int $height 设置图片缩放的最大高度
*/
function thumb($filename,$width=130,$height=130)
{

list($width_orig,$height_orig) = getimagesize($filename);

if($width && ($width_orig < $height_orig))
{
$width = ($height / $height_orig) * $width_orig;
}
else
{
$height = ($width / $width_orig) * $height_orig;
}

$image_p = imagecreatetruecolor($width, $height);

$image = imagecreatefrompng($filename);

imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

imagepng($image_p,'test.png');

imagedestroy($image_p);
imagedestroy($image);
}

thumb('1.png');
?>
<div>
<h4>缩放后的大小</h4>
<img src="test.png">
</div>

效果:

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章