Example #1
0
 public static function asImage(WPlan $week, $withEmptyDays, $fileName, array $options = array())
 {
     # merge the request parameters with the defaults
     $opt = array_merge(WPlan_ImageRenderer::$defaults, $options);
     # set the values we'll use
     $ww = $opt['w'];
     $hh = @$opt['h'] ? $opt['h'] : $ww;
     #
     $table = imagecreatetruecolor($ww, $hh);
     $dark = hexdec($opt['dark']);
     $light = hexdec($opt['light']);
     $grid = hexdec($opt['grid']);
     $border = hexdec($opt['border']);
     $textColor = hexdec($opt['text']);
     #
     $tcol = 40;
     $hdr = 30;
     $ftr = 10;
     $numrows = (count(WPlan_ImageRenderer::$hrs1) - 1) * 2;
     $w = ($ww - 2 * $tcol) / 7;
     $h = ($hh - $hdr - $ftr) / $numrows;
     imagefilledrectangle($table, 0, 0, $ww, $hh, $light);
     imageline($table, $tcol, $hdr - 7, $ww, $hdr - 7, $dark);
     # draw the grid
     for ($i = 0; $i < 6; $i += 1) {
         $x = $tcol + $i * $w;
         imageline($table, $x, 0, $x, $hh, $grid);
     }
     $x += $tcol;
     imageline($table, $x, 0, $x, $hh, $grid);
     $x += $w;
     imageline($table, $x, 0, $x, $hh, $grid);
     for ($j = 0; $j < $numrows + 1; $j += 2) {
         $y = $hdr + $j * $h;
         imagefilledrectangle($table, $tcol, $y, $tcol + 5 * $w, $y + $h, $grid);
         imagefilledrectangle($table, 2 * $tcol + 5 * $w, $y, $ww, $y + $h, $grid);
     }
     # fill the times
     $t1 = WPlan::getTime(WPlan_ImageRenderer::$hrs1[0]);
     foreach (WPlan_ImageRenderer::$hrs1 as $hr) {
         $t = WPlan::getTime($hr);
         WPlan_ImageRenderer::center($table, $hr, 0, $tcol, $hdr - 7 + ($t->serial - $t1->serial) * $h, 8, $textColor);
     }
     $t2 = WPlan::getTime(WPlan_ImageRenderer::$hrs2[0]);
     foreach (WPlan_ImageRenderer::$hrs2 as $hr) {
         $t = WPlan::getTime($hr);
         WPlan_ImageRenderer::center($table, $hr, $tcol + 5 * $w, $tcol, $hdr - 7 + ($t->serial - $t2->serial) * $h, 8, $textColor);
     }
     #assign colors
     $crtcol = 0;
     $colorMap = array();
     foreach ($week->days as $day) {
         if (isset($day->events)) {
             foreach ($day->events as $event) {
                 if (!array_key_exists($event->name, $colorMap)) {
                     $colorMap[$event->name] = $crtcol;
                     $crtcol++;
                 }
             }
         }
     }
     #echo var_export($map);
     # print the schedule
     $nday = 0;
     foreach ($week->days as $day) {
         $dx = $w;
         $x0 = 0;
         if ($nday < 5) {
             $x0 = $tcol + $nday * $w;
         } else {
             $x0 = 2 * $tcol + ($nday - 5) * $w + 5 * $w;
         }
         $dayname = $day->name;
         WPlan_ImageRenderer::center($table, $dayname, $x0 + 5, $dx - 10, 0, 12, $textColor, 1);
         if (isset($day->events)) {
             foreach ($day->events as $event) {
                 $dy = ($event->to->serial - $event->from->serial) * $h;
                 $y0 = 0;
                 if ($nday < 5) {
                     $y0 = $hdr + ($event->from->serial - $t1->serial) * $h;
                 } else {
                     $y0 = $hdr + ($event->from->serial - $t2->serial) * $h;
                 }
                 WPlan_ImageRenderer::drawBox($table, $x0 + 1, $y0 + 1, $dx - 2, $dy - 2, hexdec(WPlan_ImageRenderer::$colors[$colorMap[$event->name]]), $event, $textColor);
             }
         }
         $nday++;
     }
     # return the completed image
     imagepng($table, $fileName);
     imagedestroy($table);
 }