Blogtimes

使用方法:将代码复制保存为:blogtimes.php放入plugins文件夹,active。在适当位置插入

  1. <p align="center"><img src="http://antonioz.com/wp-images/blogtimes.png" alt="Blogtimes image" /></p>

图片可以从我这copy。:-)

  1. < ?php
  2.  
  3. /*
  4. Plugin Name: Blogtimes
  5. Plugin URI:
  6. Description: To generate a bar graph image showing when posts are made during a period of time. For this to work <code>wp-images/blogtimes.png must be writable by the web server. Original code by Sanjay Sheth of sastools.com.
  7. Author: Matt Mullenweg
  8. Author URI: http://photomatt.net
  9. Version: 0.1
  10. */
  11.  
  12.  
  13. // Main function to invoke to generate the bar graph pass it the width/height/padding/title that you want
  14. function updateBlogTimePNG($dummy = 'placeholder', $saveFile = '', $last_x_days = 30,
  15.                            $width = 480,$height = 65,$horzpadding = 5,$vertpadding = 5,
  16.                            $show_ticks = 1,
  17.                            $title = "Blog Post Times")
  18. {
  19.     if (!$saveFile) $saveFile = ABSPATH . 'wp-images/blogtimes.png';
  20.     // constants defining image
  21.     $fontheight = ImageFontHeight(2);
  22.     $fontwidth  = ImageFontWidth(2);
  23.     $monthtext = "Last $last_x_days days";
  24.     $unitname = "hour of day";
  25.     $show_units = 1;
  26.  
  27.     // create the basic image
  28.     $im = @ImageCreate ($width, $height)
  29.        or die ('Cannot create a new GD image.');
  30.  
  31.     // generate some colors, format: RED, GREEN, BLUE
  32.     $white      = ImageColorAllocate ($im, 255,255,255);
  33.     $black      = ImageColorAllocate ($im, 0,0,0);
  34.     $beige      = ImageColorAllocate ($im, 238,238,238);
  35.     $blue       = ImageColorAllocate ($im, 102,102,102);
  36.     $silver     = ImageColorAllocate ($im, 0xE0,0xE0,0xE0);
  37.  
  38.     // define what color to use where
  39.     $back_color = $white;    # this is background of entire image (text & all)
  40.    $box_color  = $beige;    # this is background of just the posts box
  41.    $text_color = $black;
  42.     $line_color = $blue;     # this is color of lines for each post
  43.    $border_color = $black;
  44.  
  45.     # query the db and build the list
  46.    $posttimes = getPostTimes(30);
  47.  
  48.     # calculate how many intervals to show
  49.    $intervals = floor( ($width / 40) );
  50.     if ($intervals >= 24) $i_mod = 1;
  51.     else if ($intervals >= 12) $i_mod = 2;
  52.     else if ($intervals >= 8) $i_mod = 3;
  53.     else if ($intervals >= 6) $i_mod = 4;
  54.     else if ($intervals >= 4) $i_mod = 6;
  55.     else if ($intervals >= 3) $i_mod = 8;
  56.     else if ($intervals >= 2) $i_mod = 16;
  57.     else $i_mod = 24;
  58.  
  59.     # fill the image with the background color
  60.    ImageFill($im, 0, 0, $back_color);
  61.  
  62.     # create a filled  rectangle with a solid border
  63.    $left = $horzpadding; $right = $width - $horzpadding;
  64.     $top = $fontheight + $vertpadding;
  65.     $bottom = $height - $vertpadding - $fontheight;
  66.  
  67.     if ($show_units)
  68.         $bottom -= $fontheight;
  69.  
  70.     ImageFilledRectangle($im, $left,$top,$right,$bottom, $box_color);
  71.     ImageRectangle($im, $left,$top,$right,$bottom, $border_color);
  72.  
  73.     # write title and monthtext
  74.    ImageString($im, 2, $left, 0, $title,$text_color);
  75.     $txtwidth = strlen($monthtext) * $fontwidth;
  76.     ImageString($im, 2, $right - $txtwidth, 0,$monthtext,$text_color);
  77.  
  78.     # add the legend on the bottom
  79.    for ($i = 0; $i < = 23; $i=$i+1)
  80.     {
  81.         if ($i % $i_mod == 0) {
  82.             $curX = $left + ($right - $left)/24 * $i;
  83.  
  84.             if ($i > 9) {$strX = $curX - 5;}
  85.             else        {$strX = $curX - 2;}
  86.  
  87.             ImageString($im, 2, $strX , $bottom, $i, $text_color);
  88.             if ($show_ticks)
  89.                 ImageLine($im, $curX, $bottom, $curX, $bottom - 5, $tick_color);
  90.         }
  91.     }
  92.     ImageString($im, 2, $right - 5, $bottom,  0, $text_color);
  93.     if ($show_units) {
  94.         $curX = ($right + $left) / 2 - ($fontwidth * strlen($unitname)/2);
  95.         $curY = $bottom + $fontheight + 2;
  96.         ImageString($im, 2, $curX, $curY, $unitname, $text_color);
  97.     }
  98.  
  99.     # now we draw the lines for each post
  100.    # the post times should be in terms of # of minutes since midnight
  101.    $arrcount = count($posttimes);
  102.     for ($i = 0; $i < $arrcount; $i++)
  103.     {
  104.         # make sure postTime is between 0 and 1439
  105.        $curPostTime = abs($posttimes&#91;$i]) % 1440;
  106.        
  107.         # calculate the horz pos inside box              
  108.        $curX = $left + ($right - $left)/1440 * $curPostTime;    # 1440 minutes per day
  109.  
  110.         # draw the post line
  111.        ImageLine($im, $curX, $bottom, $curX, $top, $line_color);
  112.     }
  113.  
  114.     # save the file to disk in PNG format
  115.    ImagePNG ($im,$saveFile);
  116. }
  117.  
  118. # This function will query the db for all the posts in last x days
  119. # and build an array of # of minutes since midnight for each post
  120. function getPostTimes($last_x_days=30)
  121. {
  122. global $wpdb, $tableposts;
  123.  
  124.     $result = $wpdb->get_results("
  125.             SELECT HOUR(post_date)*60+MINUTE(post_date) AS totmins
  126.             FROM $tableposts
  127.             WHERE (TO_DAYS(CURRENT_DATE) - TO_DAYS(post_date)) < = $last_x_days
  128.             AND post_status = 'publish'
  129.             ORDER BY totmins ASC
  130.             ");
  131.  
  132.     foreach ($result as $row) {
  133.       $postTimes&#91;] = $row->totmins;
  134.    }
  135.    
  136.     return $postTimes;
  137. }
  138.  
  139.  
  140. add_action('publish_post', 'updateBlogTimePNG');
  141. ?>

发表评论

电子邮件地址不会被公开。 必填项已用*标注