PHP實現(xiàn)繪制3D扇形統(tǒng)計圖及圖片縮放實例
更新時間:2014年10月01日 15:09:55 投稿:junjie
這篇文章主要介紹了PHP實現(xiàn)繪制3D扇形統(tǒng)計圖及圖片縮放實例,本文給出代碼示例,代碼中包含一些說明注釋,需要的朋友可以參考下
1、利用php gd庫的函數(shù)繪制3D扇形統(tǒng)計圖
<?php
header("content-type","text/html;charset=utf-8");
/*扇形統(tǒng)計圖*/
$image = imagecreatetruecolor(100, 100); /*創(chuàng)建畫布*/
/*設置畫布需要的顏色*/
$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);
/*3D制作*/
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" style="border:1px solid red;">
</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);
/*根據(jù)參數(shù)$width和$height,換算出等比例的高度和寬度*/
if($width && ($width_orig < $height_orig))
{
$width = ($height / $height_orig) * $width_orig;
}
else
{
$height = ($width / $width_orig) * $height_orig;
}
/*以新的大小創(chuàng)建畫布*/
$image_p = imagecreatetruecolor($width, $height);
/*獲取圖像資源*/
$image = imagecreatefrompng($filename);
/*使用imagecopyresampled縮放*/
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
/*保存縮放后的圖片和命名*/
imagepng($image_p,'test.png');
/*釋放資源*/
imagedestroy($image_p);
imagedestroy($image);
}
/*調用函數(shù)*/
thumb('1.png');
?>
<div>
<h4>縮放后的大小</h4>
<img src="test.png" style="border:1px solid red;">
</div>
效果:

相關文章
php批量把數(shù)組中的日期時間轉為時間戳的實現(xiàn)
本文介紹了php批量把數(shù)組中的日期時間轉為時間戳的實現(xiàn),主要使用strtotime()函數(shù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-03-03
codeigniter集成ucenter1.6雙向通信的解決辦法
用codeigniter開發(fā)一個子網(wǎng)站,之后想和原來的論壇進行同步,包括同步登陸和雙向通信。這篇文章主要介紹了codeigniter集成ucenter1.6雙向通信的解決辦法,需要的朋友可以參考下2014-06-06

