ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilImagemapPreview.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
5 
17 {
20  public $areas;
21  public $points;
24  public $lng;
25 
34  public function __construct($imagemap_filename = "")
35  {
36  global $DIC;
37  $lng = $DIC['lng'];
38  $this->lng = &$lng;
39  $this->imagemap_filename = $imagemap_filename;
40  $this->preview_filename = $preview_filename;
41  if (!@is_file($this->preview_filename)) {
42  $extension = ".jpg";
43  if (preg_match("/.*\.(png|jpg|gif|jpeg)$/", $this->imagemap_filename, $matches)) {
44  $extension = "." . $matches[1];
45  }
46  include_once "./Services/Utilities/classes/class.ilUtil.php";
47  $this->preview_filename = ilUtil::ilTempnam() . $extension;
48  }
49  $this->areas = array();
50  $this->points = array();
51  $this->linewidth_outer = 4;
52  $this->linewidth_inner = 2;
53  }
54 
55  public function getAreaCount()
56  {
57  return count($this->areas);
58  }
59 
60  public function getPointCount()
61  {
62  return count($this->points);
63  }
64 
65  public function addArea(
66  $index,
67  $shape,
68  $coords,
69  $title = "",
70  $href = "",
71  $target = "",
72  $visible = true,
73  $linecolor = "red",
74  $bordercolor = "white",
75  $fillcolor = "#FFFFFFA0"
76  ) {
77  if (ini_get("safe_mode")) {
78  if ((strpos($fillcolor, "#") !== false) ||  (strpos($fillcolor, "rgb") !== false)) {
79  $fillcolor = str_replace("\"", "", $fillcolor);
80  }
81  }
82  $this->areas[$index] = array(
83  "shape" => "$shape",
84  "coords" => "$coords",
85  "title" => "$title",
86  "href" => "$href",
87  "target" => "$target",
88  "linecolor" => '"' . $linecolor . '"',
89  "fillcolor" => '"' . $fillcolor . '"',
90  "bordercolor" => '"' . $bordercolor . '"',
91  "visible" => (int) $visible
92  );
93  }
94 
95  public function addPoint(
96  $index,
97  $coords,
98  $visible = true,
99  $linecolor = "red",
100  $bordercolor = "white",
101  $fillcolor = "#FFFFFFA0"
102  ) {
103  $this->points[$index] = array(
104  "coords" => "$coords",
105  "linecolor" => '"' . $linecolor . '"',
106  "fillcolor" => '"' . $fillcolor . '"',
107  "bordercolor" => '"' . $bordercolor . '"',
108  "visible" => (int) $visible
109  );
110  }
111 
112  public function getAreaIdent()
113  {
114  if (count($this->areas) + count($this->points) > 0) {
115  $arr = array_merge(array_keys($this->areas), array_keys($this->points));
116  sort($arr, SORT_NUMERIC);
117 
118  $inner = join("_", $arr);
119  if (strlen($inner) > 32) {
120  $inner = md5($inner);
121  }
122  return "preview_" . $inner . "_";
123  } else {
124  return "";
125  }
126  }
127 
128  public function createPreview()
129  {
130  if (count($this->areas) + count($this->points) == 0) {
131  return;
132  }
133  include_once "./Services/Utilities/classes/class.ilUtil.php";
134  $convert_cmd = "-quality 100 ";
135  foreach ($this->points as $point) {
136  if ($point["visible"]) {
137  preg_match("/(\d+)\s*,\s*(\d+)/", $point["coords"], $matches);
138  $x = $matches[1];
139  $y = $matches[2];
140  $r = 6;
141  // draw a circle at the point
142  $convert_cmd .= "-stroke " . $point["bordercolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"line " .
143  ($x - $r) . "," . ($y - $r) . " " . ($x + $r) . "," . ($y + $r) . "\" " .
144  "-stroke " . $point["bordercolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"line " .
145  ($x + $r) . "," . ($y - $r) . " " . ($x - $r) . "," . ($y + $r) . "\" " .
146  "-stroke " . $point["linecolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"line " .
147  ($x - $r) . "," . ($y - $r) . " " . ($x + $r) . "," . ($y + $r) . "\" " .
148  "-stroke " . $point["linecolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"line " .
149  ($x + $r) . "," . ($y - $r) . " " . ($x - $r) . "," . ($y + $r) . "\" ";
150  }
151  }
152  foreach ($this->areas as $area) {
153  if ($area["visible"] and strcmp(strtolower($area["shape"]), "rect") == 0) {
154  preg_match("/(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/", $area["coords"], $matches);
155  $x0 = $matches[1];
156  $y0 = $matches[2];
157  $x1 = $matches[3];
158  $y1 = $matches[4];
159  // draw a rect around the selection
160  $convert_cmd .= "-stroke " . $area["bordercolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"rectangle " .
161  $x0 . "," . $y0 . " " . ($x1) . "," . $y1 . "\" " .
162  "-stroke " . $area["linecolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"rectangle " .
163  $x0 . "," . $y0 . " " . ($x1) . "," . $y1 . "\" ";
164  } elseif ($area["visible"] and strcmp(strtolower($area["shape"]), "circle") == 0) {
165  preg_match("/(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/", $area["coords"], $matches);
166  $x = $matches[1];
167  $y = $matches[2];
168  $r = $matches[3];
169  // draw a circle around the selection
170  $convert_cmd .= "-stroke " . $area["bordercolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"circle " .
171  $x . "," . $y . " " . ($x + $r) . "," . $y . "\" " .
172  "-stroke " . $area["linecolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"circle " .
173  $x . "," . $y . " " . ($x + $r) . "," . $y . "\" ";
174  } elseif ($area["visible"] and strcmp(strtolower($area["shape"]), "poly") == 0) {
175  $obj = "polygon";
176  // draw a polygon around the selection
177  preg_match_all("/(\d+)\s*,\s*(\d+)/", $area["coords"], $matches, PREG_PATTERN_ORDER);
178  if (count($matches[0]) == 2) {
179  $obj = "line";
180  }
181  $convert_cmd .= "-stroke " . $area["bordercolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"$obj ";
182  for ($i = 0; $i < count($matches[0]); $i++) {
183  $convert_cmd .= $matches[1][$i] . "," . $matches[2][$i] . " ";
184  }
185  $convert_cmd .= "\" ";
186  $convert_cmd .= "-stroke " . $area["linecolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"$obj ";
187  preg_match_all("/(\d+)\s*,\s*(\d+)/", $area["coords"], $matches, PREG_PATTERN_ORDER);
188  for ($i = 0; $i < count($matches[0]); $i++) {
189  $convert_cmd .= $matches[1][$i] . "," . $matches[2][$i] . " ";
190  }
191  $convert_cmd .= "\" ";
192  }
193  }
194 
195  $source = ilUtil::escapeShellCmd($this->imagemap_filename);
196  $target = ilUtil::escapeShellCmd($this->preview_filename);
197  $convert_cmd = $source . "[0] " . $convert_cmd . " " . $target;
198  ilUtil::execConvert($convert_cmd);
199  }
200 
201  public function getPreviewFilename($imagePath, $baseFileName)
202  {
203  $filename = $baseFileName;
204  if (count($this->areas) + count($this->points) > 0) {
205  $pfile = $this->preview_filename;
206  if (is_file($pfile)) {
207  $ident = $this->getAreaIdent();
208  $previewfile = $imagePath . $ident . $baseFileName;
209  if (@md5_file($previewfile) != @md5_file($pfile)) {
210  if (strlen($ident) > 0) {
211  @copy($pfile, $previewfile);
212  }
213  }
214  @unlink($pfile);
215  if (strlen($pfile) == 0) {
216  ilUtil::sendInfo($this->lng->txt("qpl_imagemap_preview_missing"));
217  } else {
218  $filename = basename($previewfile);
219  }
220  } else {
221  ilUtil::sendInfo($this->lng->txt("qpl_imagemap_preview_missing"));
222  }
223  }
224  return $filename;
225  }
226 
231  public function getImagemap($title)
232  {
233  $map = "<map name=\"$title\"> ";
234  foreach ($this->areas as $area) {
235  $map .= "<area alt=\"" . $area["title"] . "\" title=\"" . $area["title"] . "\" ";
236  $map .= "shape=\"" . $area["shape"] . "\" ";
237  $map .= "coords=\"" . $area["coords"] . "\" ";
238  if ($area["href"]) {
239  $map .= "href=\"" . $area["href"] . "\" ";
240  if ($area["target"]) {
241  $map .= "target=\"" . $area["target"] . "\" ";
242  }
243  $map .= "/>\n";
244  } else {
245  $map .= "nohref />\n";
246  }
247  }
248  $map .= "</map>";
249  return $map;
250  }
251 }
global $DIC
Definition: saml.php:7
getPreviewFilename($imagePath, $baseFileName)
$index
Definition: metadata.php:60
$coords
Definition: example_030.php:88
Image map image preview creator.
static escapeShellCmd($a_arg)
escape shell cmd
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
getImagemap($title)
get imagemap html code note: html code should be placed in template files
$r
Definition: example_031.php:79
__construct($imagemap_filename="")
ilImagemapPreview constructor
$y
Definition: example_007.php:83
static execConvert($args)
execute convert command
addPoint( $index, $coords, $visible=true, $linecolor="red", $bordercolor="white", $fillcolor="#FFFFFFA0")
$filename
Definition: buildRTE.php:89
static ilTempnam($a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
$i
Definition: disco.tpl.php:19
$source
Definition: linkback.php:22
addArea( $index, $shape, $coords, $title="", $href="", $target="", $visible=true, $linecolor="red", $bordercolor="white", $fillcolor="#FFFFFFA0")
$target
Definition: test.php:19
$x
Definition: complexTest.php:9