<?php

require_once "operations/file/find_file.php";

class Labeled_Segmented_Image extends Segmented_Image
{
   private $_labels;
   private $_font_name;
   private $_font_weight;
   private $_text_color;
   private $_font_size;
   private $_text_background;
   private $_text_horizontal_padding;
   private $_text_vertical_padding;
   private $_text_stroke_width;
   private $_text_stroke_color;
   private $_label_alignment;
   private $_labeled_segments_directory_path;

   public function __construct(
      $file_path, $labels, $width=null, $height=null, $cut_orientation=null,
      $text_color=null, $font_size=null, $font_name=null, $label_alignment=null,
      $segment_count=null)
   {
      $this->_labels = $labels;
      $this->_label_alignment = $label_alignment;
      $this->construct_parent(
         $file_path, $segment_count, $cut_orientation, $width, $height);
      $this->build_labeled_segments_directory();
      $this->draw_labels();
   }

   private function construct_parent(
      $file_path, $segment_count, $cut_orientation, $width, $height)
   {
      $segment_count = $this->determine_segment_count($segment_count);
      parent::__construct(
         $file_path, $segment_count, $cut_orientation, $width, $height);
   }


   private function determine_segment_count($segment_count)
   {
      if ($segment_count === null)
      {
         $segment_count = sizeof($this->_labels);
      }
      return $segment_count;
   }

   private function build_labeled_segments_directory()
   {
      $path = $this->_segments_directory_path . "/";
      $path .= $GLOBALS["LABELED_SEGMENTS_DIRECTORY_NAME"];
      if (!is_dir($path))
      {
         mkdir($path);
      }
      $this->_labeled_segments_directory_path = $path;
   }

   private function draw_labels()
   {
      $ii = 0;
      foreach ($this->_labels as $label)
      {
         $this->add_label_to_segment($label, $ii++);
      }
   }

   private function add_label_to_segment($label, $segment_index)
   {
      if ($label instanceof Entity)
      {
         $this->add_entity_to_segment($label, $segment_index);
      }
      else
      {
         $this->add_annotation_to_segment($label, $segment_index);
      }
   }

   private function add_entity_to_segment($entity, $segment_index)
   {
      $segment = $this->segments[$segment_index];
      $entity = $this->build_replacement_entity($entity, $segment);
      $this->segments[$segment_index] = $entity;
   }

   private function build_replacement_entity($label, $segment)
   {
      $file_path = $segment->path;
      $entity = new Div($segment->width, $segment->height);
      $entity->background = $file_path;
      $entity->content = $label;
      $entity->overflow = "hidden";
      if ($this->_cut_orientation == "vertical")
      {
         $entity->float = "left";
      }
      $label->line_height = $segment->height . "px";
      return $entity;
   }

   private function add_annotation_to_segment($text, $index)
   {
      $segment = $this->segments[$index];
      $annotation = $this->build_label($text);
      $labeled_segment_image = new Imagick($segment->path);
      $this->draw_annotation($labeled_segment_image, $annotation);
      $this->save_labeled_segment_image($labeled_segment_image, $index);
   }

   private function build_label($text)
   {
      $label = new Label(
         $text, null, $this->_font_size, $this->_font_weight,
         $this->_text_color, $this->_font_name, $this->_text_background,
         $this->_text_horizontal_padding, $this->_text_vertical_padding,
         $this->_stroke_width, $this->_stroke_color);
      return $label->_image;
   }

   private function draw_annotation($image, $annotation)
   {
      $x = $this->determine_annotation_horizontal_offset($image, $annotation);
      $y = $this->determine_annotation_vertical_offset($image, $annotation);
      $image->compositeImage($annotation, Imagick::COMPOSITE_DEFAULT, $x, $y);
   }

   private function determine_annotation_horizontal_offset($image, $annotation)
   {
      $offset = 0;
      $image_width = $image->getImageWidth();
      $annotation_width = $annotation->getImageWidth();
      if ($this->_label_alignment == "center")
      {
         $offset = $image_width/2 - $annotation_width/2;
      }
      elseif ($this->_label_alignment == "right")
      {
         $offset = $image_width - $annotation_width;
      }
      return $offset;
   }

   private function determine_annotation_vertical_offset($image, $annotation)
   {
      $offset = $image->getImageHeight()/2 - $annotation->getImageHeight()/2;
      return $offset;
   }

   private function save_labeled_segment_image($image, $index)
   {
      $path = $this->build_labeled_segment_path($index);
      $image->writeImage($path);
      $this->segments[$index]->path = $path;
   }

   private function build_labeled_segment_path($index)
   {
      return $this->_labeled_segments_directory_path . "/" . $index;
   }
}
<?php

define("LABEL_CONSTANT_PREFIX", "LABEL_");
define("LABEL_FONT_CONSTANT_SUFFIX", "_FONT");
define("LABEL_SIZE_CONSTANT_SUFFIX", "_SIZE");

class Navigation_Map extends Div
{
   private $labels = array();

   public function __construct()
   {
      parent::__construct();
      $this->import_labels();
      $this->segmented_image = new Labeled_Segmented_Image(
         "src/img/jimjilbbang.jpg", $this->labels, 400, 500, "horizontal");
      $this->width = 400;
      $this->text_align = "left";
      $this->margin = "20 auto";
   }

   private function import_labels()
   {
      for ($ii = 1; isset($GLOBALS[LABEL_CONSTANT_PREFIX . $ii]); $ii++)
      {
         $this->include_label($ii);
      }
   }

   private function include_label($ii)
   {
      $label_constant_name = LABEL_CONSTANT_PREFIX . $ii;
      $font_constant_name = $label_constant_name . LABEL_FONT_CONSTANT_SUFFIX;
      $size_constant_name = $label_constant_name . LABEL_SIZE_CONSTANT_SUFFIX;
      $text = $GLOBALS[$label_constant_name];
      $font = $this->get_constant_value($font_constant_name);
      $size = $this->get_constant_value($size_constant_name);
      $statement = new Statement($text, $size, "bold", null, "black", "white", $font);
      $statement->padding = 3;
/*       $statement = $text; */
      $this->labels[] = $statement;
   }

   private function get_constant_value($constant_name)
   {
      if (isset($GLOBALS[$constant_name]))
      {
         return $GLOBALS[$constant_name];
      }
      return null;
   }
}
3.239.129.52
3.239.129.52
3.239.129.52
 
June 6, 2016