-
-
-
-
- 萬年曆
- 驗證碼
- 圖檔縮放
- 水印
-
-
-
萬年曆
<?php
//修改頁面編碼
header("content-type:text/html;charset=utf-8");
//擷取目前年
$year=$_GET['year']?$_GET['year']:date('Y');
//擷取目前月
$month=$_GET['month']?$_GET['month']:date('m');
//擷取目前月有多少天
$days=date("t",strtotime("{$year}-{$month}-1"));
//目前月的一号是周幾
$week=date("w",strtotime("{$year}-{$month}-1"));
//所有内容居中
echo "<center>";
//輸出表頭
echo "<h2>{$year}年{$month}月</h2>";
//輸出日期表格
echo "<table width=500 ;
echo "<tr>";
echo "<th>周日</th>";
echo "<th>周一</th>";
echo "<th>周二</th>";
echo "<th>周三</th>";
echo "<th>周四</th>";
echo "<th>周五</th>";
echo "<th>周六</th>";
echo "</tr>";
//鋪表格
for($i=1-$week;$i<=$days;){
echo "<tr>";
for($j=1;$j<=7;$j++){
if($i<=$days && $i>=1){
echo "<td>$i</td>";
$i++;
}
else{
echo "<td> </td>";
$i++;
}
}
echo "</tr>";
}
echo "<table width=500>";
//實作上一年和上一月
if($month==1){
$lasty=$year-1;
$lastm=12;
}else{
$lasty=$year;
$lastm=$month-1;
}
//實作下一年和下一月
if($month==12){
$nexty=$year+1;
$nextm=1;
}else{
$nexty=$year;
$nextm=$month+1;
}
//輸出上一月和下一月的按鈕
echo "<a href='calendar.php?year=$lasty&month=$lastm'>上一月</a>";
echo "|";
echo "<a href='calendar.php?year=$nexty&month=$nextm'>下一月</a>";
echo "</center>";
?>
驗證碼
<?php
//vcode(int $width,int $height,int $fontsize,int $vcode_number,$int $interferon_number)
//可以根據需要在傳入其他參數,例如字型、幹擾素個數等
function vcode($w,$h,$size,$fontn,$internum){
//開啟session
session_start();
//準備畫布資源
$im=imagecreatetruecolor($w,$h);
//準備塗料
$white=imagecolorallocate($im,255,255,255);
//背景填充
imagefill($im,0,0,$white);
//加幹擾素
for($i=0;$i<$internum;$i++){
$color = imagecolorallocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
imagesetpixel($im,mt_rand(0,$w),mt_rand(0,$h),$color);
}
for($i=0;$i<10;$i++){
$color = imagecolorallocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
imageline($im,mt_rand(0,$w),mt_rand(0,$h),mt_rand(0,$w),mt_rand(0,$h),$color);
}
//繪制邊框
$rect_color = imagecolorallocate($im, 150, 150, 150);
imagerectangle($im, 0, 0, $w-1, $h-1, $rect_color);
//文字坐标
$x=($w-$size*4)/2+$size/2;
$y=($h-$size)/2+$size;
//準備文字
$arr=array_merge(range(0,9),range('a','z'),range('A','Z'));
shuffle($arr);
$str=join("",array_slice($arr,0,$fontn));
//把$str放入session中,友善在所有頁面使用
$_SESSION['vstr']=$str;
$font='C:\phpstudy_pro\WWW\Messages\font\CENSCBK.TTF';
imagettftext($im,$size,0,$x,$y,$black,$font,$str);
//輸出最終圖像或儲存最終圖像
header("content-type:image/png");
imagepng($im);
//釋放畫布資源
imagedestroy($im);
}
vcode(85,35,18,4,200);
?>
圖檔縮放
<?php
function scale($maximagefile,$small_per){
$maxfile=$maximagefile;
//大圖寬高
$arr=getimagesize("{$maxfile}");
$maxw=$arr[0];
$maxh=$arr[1];
$maxtype=$arr[2];
$maxmime=$arr["mime"];
//大圖資源
$maxim=imagecreatefromjpeg("{$maxfile}");
//等比例縮放
$percent=$small_per;
//小圖資源
$minw=floor($maxw*$percent);
$minh=floor($maxh*$percent);
$minim=imagecreatetruecolor($minw,$minh);
//把大圖縮放成小圖
imagecopyresampled($minim, $maxim, 0,0,0,0,$minw, $minh, $maxw, $maxh);
//小圖輸出
header("content-type:{$maxm}");
//判斷類型
switch ($maxtype) {
case '1':
$imageout="imagegif";
break;
case '2':
$imageout="imagejpeg";
break;
case '3':
$imageout="imagepng";
break;
}
// $imgout($minim);
$minfile="s_".$maxfile;
$imageout($minim,$minfile);
//釋放大小圖資源
imagedestroy($maxim);
imagedestroy($minim);
}
scale("music.jpg",0.5); //調用函數
?>
水印
<?php
$maxim=imagecreatefromjpeg("music.jpg");
$minim=imagecreatefrompng("logo.png");
$maxw=imagesx($maxim);
$maxh=imagesy($maxim);
$minw=imagesx($minim);
$minh=imagesy($minim);
$maxx=$maxw-$minw;
$maxy=$maxh-$minh;
imagecopy($maxim,$minim,$maxx,$maxy,0,0,$minw,$minh);
header("content-type:image/jpeg");
imagejpeg($maxim);
?>
進行圖檔縮放時,如果對縮略圖的品質要求不高可以使用imagecopyresized()函數,imagecopyresize()所生成的圖像比較粗糙,但是速度較快;
imagecopyresampled()函數是GD 2.x後新增加的函數,字面上的意思是會對圖檔進行重新采樣(resampling),GD是采用插值算法生成更平滑的圖像,但是速度相對imagecopyresize()函數來說要慢一些。