1、对图片进行缩放
01 | <div> |
02 | <h4>原图大小</h4> |
03 | <img src= "1.png" > |
04 | </div> |
05 | <?php |
06 | header( "content-type" , "text/html;charset=utf-8" ); |
07 | /* |
08 | *图片缩放 |
09 | *@param string $filename 图片的url |
10 | *@param int $width 设置图片缩放的最大宽度 |
11 | *@param int $height 设置图片缩放的最大高度 |
12 | */ |
13 | function thumb( $filename , $width =130, $height =130) |
14 | { |
15 | /*获取原图的大小*/ |
16 | list( $width_orig , $height_orig ) = getimagesize ( $filename ); |
17 | /*根据参数$width和$height,换算出等比例的高度和宽度*/ |
18 | if ( $width && ( $width_orig < $height_orig )) |
19 | { |
20 | $width = ( $height / $height_orig ) * $width_orig ; |
21 | } |
22 | else |
23 | { |
24 | $height = ( $width / $width_orig ) * $height_orig ; |
25 | } |
26 | /*以新的大小创建画布*/ |
27 | $image_p = imagecreatetruecolor( $width , $height ); |
28 | /*获取图像资源*/ |
29 | $image = imagecreatefrompng( $filename ); |
30 | /*使用imagecopyresampled缩放*/ |
31 | imagecopyresampled( $image_p , $image , 0, 0, 0, 0, $width , $height , $width_orig , $height_orig ); |
32 | /*保存缩放后的图片和命名*/ |
33 | imagepng( $image_p , 'test.png' ); |
34 | /*释放资源*/ |
35 | imagedestroy( $image_p ); |
36 | imagedestroy( $image ); |
37 | } |
38 | /*调用函数*/ |
39 | thumb( '1.png' ); |
40 | ?> |
41 | <div> |
42 | <h4>缩放后的大小</h4> |
43 | <img src= "test.png" > |
44 | </div> |
效果:
2、利用php gd库的函数绘制3D扇形统计图
01 | <?php |
02 | header( "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 | /*填充背景色*/ |
14 | imagefill( $image , 0, 0, $white ); |
15 | /*3D制作*/ |
16 | for ( $i = 60; $i > 50; $i --) |
17 | { |
18 | imagefilledarc( $image , 50, $i , 100, 50, -160, 40, $darknavy , IMG_ARC_PIE); |
19 | imagefilledarc( $image , 50, $i , 100, 50, 40, 75, $darkgray , IMG_ARC_PIE); |
20 | imagefilledarc( $image , 50, $i , 100, 50, 75, 200, $darkred , IMG_ARC_PIE); |
21 | } |
22 | /*画椭圆弧并填充*/ |
23 | imagefilledarc( $image , 50, 50, 100, 50, -160, 40, $darknavy , IMG_ARC_PIE); |
24 | imagefilledarc( $image , 50, 50, 100, 50, 40, 75, $darkgray , IMG_ARC_PIE); |
25 | imagefilledarc( $image , 50, 50, 100, 50, 75, 200, $darkred , IMG_ARC_PIE); |
26 | /*画字符串*/ |
27 | imagestring( $image , 3, 15, 55, "30%" , $white ); |
28 | imagestring( $image , 3, 45, 35, "60%" , $white ); |
29 | imagestring( $image , 3, 60, 60, "10%" , $white ); |
30 | /*输出图像*/ |
31 | header( "content-type:image/png" ); |
32 | imagepng( $image ); |
33 | /*释放资源*/ |
34 | imagedestroy( $image ); |
35 | ?> |
效果: