PHP对图片进行缩放和利用gd库制作3D扇形统计图的操作实例代码

分类:PHP_Python| 发布:llmaomi| 查看:425 | 发表时间:2015/4/13

1、对图片进行缩放

01<div>
02<h4>原图大小</h4>
03<img src="1.png">
04</div>
05<?php
06header("content-type","text/html;charset=utf-8");
07/*
08*图片缩放
09*@param string $filename   图片的url
10*@param int    $width      设置图片缩放的最大宽度
11*@param int    $height     设置图片缩放的最大高度
12*/
13function thumb($filename,$width=130,$height=130)
14{
15/*获取原图的大小*/
16list($width_orig,$height_orig) = getimagesize($filename);
17/*根据参数$width和$height,换算出等比例的高度和宽度*/
18if($width && ($width_orig < $height_orig))
19{
20$width = ($height / $height_orig) * $width_orig;
21}
22else
23{
24$height = ($width / $width_orig) * $height_orig;
25}
26/*以新的大小创建画布*/
27$image_p = imagecreatetruecolor($width, $height);
28/*获取图像资源*/
29$image = imagecreatefrompng($filename);
30/*使用imagecopyresampled缩放*/
31imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
32/*保存缩放后的图片和命名*/
33imagepng($image_p,'test.png');
34/*释放资源*/
35imagedestroy($image_p);
36imagedestroy($image);
37}
38/*调用函数*/
39thumb('1.png');
40?>
41<div>
42<h4>缩放后的大小</h4>
43<img src="test.png">
44</div>

效果:

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

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

01<?php
02header("content-type","text/html;charset=utf-8");
03/*扇形统计图*/
04$image = imagecreatetruecolor(100, 100);    /*创建画布*/
05/*设置画布需要的颜色*/
06$white = imagecolorallocate($image,0xff,0xff,0xff);
07$gray = imagecolorallocate($image, 0xc0, 0xc0, 0xc0);
08$darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);
09$navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
10$darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50);
11$red = imagecolorallocate($image, 0xff, 0x00, 0x00);
12$darkred = imagecolorallocate($image, 0x90, 0x00, 0x00);
13/*填充背景色*/
14imagefill($image, 0, 0, $white);
15/*3D制作*/
16for($i = 60; $i > 50; $i--)
17{
18imagefilledarc($image, 50, $i, 100, 50, -160, 40, $darknavy, IMG_ARC_PIE);
19imagefilledarc($image, 50, $i, 100, 50, 40, 75, $darkgray, IMG_ARC_PIE);
20imagefilledarc($image, 50, $i, 100, 50, 75, 200, $darkred, IMG_ARC_PIE);
21}
22/*画椭圆弧并填充*/
23imagefilledarc($image, 50, 50, 100, 50, -160, 40, $darknavy, IMG_ARC_PIE);
24imagefilledarc($image, 50, 50, 100, 50, 40, 75, $darkgray, IMG_ARC_PIE);
25imagefilledarc($image, 50, 50, 100, 50, 75, 200, $darkred, IMG_ARC_PIE);
26/*画字符串*/
27imagestring($image, 3, 15, 55, "30%", $white);
28imagestring($image, 3, 45, 35, "60%", $white);
29imagestring($image, 3, 60, 60, "10%", $white);
30/*输出图像*/
31header("content-type:image/png");
32imagepng($image);
33/*释放资源*/
34imagedestroy($image);
35?>

效果:

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

365据说看到好文章不转的人,服务器容易宕机
原创文章如转载,请注明:转载自郑州网建-前端开发 http://camnpr.com/
本文链接:http://camnpr.com/php-python/1977.html