php gd 生成日历图

  1 <?php
  2 
  3 //如果您提交了时间则显示您提交年月的日历,否则显示当前月份日历
  4 if (isset($_GET['month']) && isset($_GET['year']))
  5 {
  6     $month = $_GET['month'];
  7     $year = $_GET['year'];
  8 }
  9 else 
 10 {
 11     $month = date ('m');
 12     $year = date ('Y');
 13 }
 14 $weekid = date ('w',mktime(0,0,0,$month,1,$year));//某年某月第一天是星期几。0-7分别代表星期日-星期六
 15 $countdays = date('t',mktime(0,0,0,$month,1,$year));//某年某个月的天数
 16 //获取上个月的天数
 17 $prevMonth = $month - 1;
 18 $prevYear = $year;
 19 if($prevMonth == 0){
 20     $prevMonth = 12;
 21     $prevYear = $year - 1;
 22 }
 23 $prevMonthDays = date('t',mktime(0,0,0,$prevMonth,1,$prevYear));//某年某个月的上个月的天数
 24 //echo $prevMonthDays."<br/>";
 25 //echo $prevMonth."<br/>";
 26 $countdays = date('t',mktime(0,0,0,$month,1,$year));//某年某个月的天数
 27 $arr_days = array ();//数组$arr_days代表某个月的每一天
 28 //echo $weekid."<br/>";
 29 //echo $countdays."<br/>";
 30 //print_r($arr_days);
 31 $tempweekid = $weekid-1; //用于计算上个月前面的天数
 32 //初始化数组$arr_days
 33 for ($i = 0; $i <= 35; $i++)
 34 {
 35     if($tempweekid >=0){
 36         $arr_days[$i] = $prevMonthDays - $tempweekid;
 37         $tempweekid--;
 38     }else{
 39         $arr_days[$i] = "";
 40     }
 41     
 42 }
 43 
 44 //给$arr_days数组赋值
 45 for ($i = $weekid, $j = 1; $j <= $countdays; $i++, $j++)
 46 {
 47     $arr_days[$i] = $j;
 48 }
 49 
 50 header("Content-type: image/png");
 51 $im = @imagecreate(300, 185)
 52     or die("Cannot Initialize new GD image stream");
 53 $background_color = imagecolorallocate($im, 255, 255, 255);
 54 
 55 
 56 // Create some colors
 57 $white = imagecolorallocate($im, 255, 255, 255);
 58 $grey = imagecolorallocate($im, 128, 128, 128);
 59 $black = imagecolorallocate($im, 0, 0, 0);
 60 $red = imagecolorallocate($im, 255, 37, 37);
 61 $color2 = imagecolorallocatealpha($im,0,0,0,0);
 62 
 63 $alphagray = imagecolorallocatealpha($im,205,206,206,0);
 64 $alphared = imagecolorallocatealpha($im,255,206,206,0);
 65 
 66 $font = './gillsans.ttf';
 67 $text = "SU";
 68 
 69 $titleArray = array("SUN","MON","TUE","WED","THU","FRI","SAT");
 70 $font_x = 20;
 71 $font_y = 20;
 72 foreach($titleArray as $key=>$value){
 73 // Add some shadow to the text
 74     imagettftext($im, 12, 0, $font_x, $font_y, $color2, $font, $value);    
 75     $font_x +=40;
 76 }
 77 
 78 
 79 $font_x = 20;
 80 $font_y = 45;
 81 
 82     //表格输出
 83 for ($i = 0; $i <= 35; $i++)
 84 {
 85     $row = $arr_days[$i];
 86     $tempFontx = $font_x;
 87     if($row < 10){
 88         $tempFontx +=5;
 89     }
 90     
 91     
 92     
 93     if ($i % 7 == 0)
 94     {
 95         
 96         if($i < 7 && $row > 7){
 97             imagettftext($im, 12, 0, $tempFontx, $font_y, $alphared, $font, $row);    
 98         }else{
 99             imagettftext($im, 12, 0, $tempFontx, $font_y, $red, $font, $row);    
100         }
101         
102         
103         $font_x +=40;
104     }else{
105         if($i < 7 && $row > 7){
106             imagettftext($im, 12, 0, $tempFontx, $font_y, $alphagray, $font, $row);    
107         }else{
108             imagettftext($im, 12, 0, $tempFontx, $font_y, $color2, $font, $row);
109         }    
110             
111         $font_x +=40;
112     }
113     
114     if (($i + 1) % 7 == 0)
115     {
116         $font_x = 20;
117         $font_y += 25;
118     }
119 }
120 
121 // Add the text
122 imagepng($im,"./".$year.$month.".png");
123 //imagepng($im);
124 imagedestroy($im);
View Code

使用的字体Font: http://pan.baidu.com/s/1jGBzrM2

最终效果:

 

参考文章:用php实现的一个简单万年历

posted @ 2015-07-24 11:03  五毛钱的饼  阅读(526)  评论(0编辑  收藏  举报