/**
  * Composites visible Feature/Event markers, regions, and labels onto
  * image.
  *
  * @param object $imagickImage An Imagick object
  *
  * @return void
  */
 private function _addEventLayer($imagickImage)
 {
     if ($this->width < 200 || $this->height < 200) {
         return;
     }
     $markerPinPixelOffsetX = 12;
     $markerPinPixelOffsetY = 38;
     require_once HV_ROOT_DIR . '/../src/Event/HEKAdapter.php';
     $hek = new Event_HEKAdapter();
     // Query the HEK
     $events = $hek->getEvents($this->date, array());
     if ($events === false) {
         return false;
     }
     // Lay down all relevant event REGIONS first
     $allowedFRMs = $this->events->toArray();
     foreach ($events as $index => $event) {
         $found = false;
         foreach ($allowedFRMs as $j => $frm) {
             if ($event['event_type'] == $frm['event_type'] && ($frm['frm_name'] == 'all' || $frm['frm_name'] == str_replace(' ', '_', $event['frm_name']) || strpos($frm['frm_name'], str_replace(' ', '_', $event['frm_name'])) !== false)) {
                 $found = true;
                 break;
             }
         }
         if ($found === false) {
             continue;
         }
         if (array_key_exists('hv_poly_width_max_zoom_pixels', $event)) {
             $width = round($event['hv_poly_width_max_zoom_pixels'] * ($this->maxPixelScale / $this->roi->imageScale()));
             $height = round($event['hv_poly_height_max_zoom_pixels'] * ($this->maxPixelScale / $this->roi->imageScale()));
             if ($width >= 1 && $height >= 1) {
                 $region_polygon = new IMagick(HV_ROOT_DIR . '/' . urldecode($event['hv_poly_url']));
                 $x = ($event['hv_poly_hpc_x_final'] - $this->roi->left()) / $this->roi->imageScale();
                 $y = ($event['hv_poly_hpc_y_final'] - $this->roi->top()) / $this->roi->imageScale();
                 $region_polygon->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 1);
                 $imagickImage->compositeImage($region_polygon, IMagick::COMPOSITE_DISSOLVE, $x, $y);
             }
         }
     }
     if (isset($region_polygon)) {
         $region_polygon->destroy();
     }
     // Now lay down the event MARKERS
     foreach ($events as $index => $event) {
         $found = false;
         $allowedFRMs = $this->events->toArray();
         foreach ($allowedFRMs as $j => $frm) {
             if ($event['event_type'] == $frm['event_type'] && ($frm['frm_name'] == 'all' || $frm['frm_name'] == str_replace(' ', '_', $event['frm_name']) || strpos($frm['frm_name'], str_replace(' ', '_', $event['frm_name'])) !== false)) {
                 $found = true;
                 continue;
             }
         }
         if ($found === false) {
             continue;
         }
         $marker = new IMagick(HV_ROOT_DIR . '/resources/images/eventMarkers/' . $event['event_type'] . '.png');
         $x = ($event['hv_hpc_x_final'] - $this->roi->left()) / $this->roi->imageScale() - $markerPinPixelOffsetX;
         $y = (-$event['hv_hpc_y_final'] - $this->roi->top()) / $this->roi->imageScale() - $markerPinPixelOffsetY;
         $imagickImage->compositeImage($marker, IMagick::COMPOSITE_DISSOLVE, $x, $y);
         if ($this->eventsLabels == true) {
             $x = ($event['hv_hpc_x_final'] - $this->roi->left()) / $this->roi->imageScale() + 11;
             $y = (-$event['hv_hpc_y_final'] - $this->roi->top()) / $this->roi->imageScale() - 24;
             $count = 0;
             if (!array_key_exists('hv_labels_formatted', $event) || count($event['hv_labels_formatted']) < 1) {
                 $event['hv_labels_formatted'] = array('Event Type' => $event['concept']);
             }
             foreach ($event['hv_labels_formatted'] as $key => $value) {
                 //Fix unicode
                 $value = str_replace(array('u03b1', 'u03b2', 'u03b3', 'u00b1', 'u00b2'), array('α', 'β', 'γ', '±', '²'), $value);
                 // Outline words in black
                 $text = new IMagickDraw();
                 $text->setTextEncoding('utf-8');
                 $text->setFont(HV_ROOT_DIR . '/../resources/fonts/DejaVuSans.ttf');
                 $text->setFontSize(10);
                 $text->setStrokeColor('#000C');
                 $text->setStrokeAntialias(true);
                 $text->setStrokeWidth(3);
                 $text->setStrokeOpacity(0.3);
                 $imagickImage->annotateImage($text, $x, $y + $count * 12, 0, $value);
                 // Write words in white over outline
                 $text = new IMagickDraw();
                 $text->setTextEncoding('utf-8');
                 $text->setFont(HV_ROOT_DIR . '/../resources/fonts/DejaVuSans.ttf');
                 $text->setFontSize(10);
                 $text->setFillColor('#ffff');
                 $text->setTextAntialias(true);
                 $text->setStrokeWidth(0);
                 $imagickImage->annotateImage($text, $x, $y + $count * 12, 0, $value);
                 $count++;
             }
             // Cleanup
             $text->destroy();
         }
     }
     if (isset($marker)) {
         $marker->destroy();
     }
 }
 /**
  * Import Features/Events from HEK database to the helioviewer for the requested time range
  *
  * @return void
  */
 public function importEvents()
 {
     include_once HV_ROOT_DIR . '/../src/Event/HEKAdapter.php';
     $hek = new Event_HEKAdapter();
     if (array_key_exists('period', $this->_options)) {
         $period = $this->_options['period'];
     } else {
         $period = null;
     }
     // Query the HEK
     $events = $hek->importEvents($period);
     header('Content-Type: application/json');
     echo json_encode('{"status":"success"}');
 }