ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f87
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  var $areas;
21  var $points;
24  var $lng;
25 
34  function ilImagemapPreview($imagemap_filename = "")
35  {
36  global $lng;
37  $this->lng =& $lng;
38  $this->imagemap_filename = $imagemap_filename;
39  $this->preview_filename = $preview_filename;
40  if (!@is_file($this->preview_filename))
41  {
42  $extension = ".jpg";
43  if (preg_match("/.*\.(png|jpg|gif|jpeg)$/", $this->imagemap_filename, $matches))
44  {
45  $extension = "." . $matches[1];
46  }
47  include_once "./Services/Utilities/classes/class.ilUtil.php";
48  $this->preview_filename = ilUtil::ilTempnam() . $extension;
49  }
50  $this->areas = array();
51  $this->points = array();
52  $this->linewidth_outer = 4;
53  $this->linewidth_inner = 2;
54  }
55 
56  function getAreaCount()
57  {
58  return count($this->areas);
59  }
60 
61  function getPointCount()
62  {
63  return count($this->points);
64  }
65 
66  function addArea(
67  $index,
68  $shape,
69  $coords,
70  $title = "",
71  $href = "",
72  $target = "",
73  $visible = true,
74  $linecolor = "red",
75  $bordercolor = "white",
76  $fillcolor = "#FFFFFFA0"
77  )
78  {
79  if (ini_get("safe_mode"))
80  {
81  if ((strpos($fillcolor, "#") !== false) || (strpos($fillcolor, "rgb") !== false))
82  {
83  $fillcolor = str_replace("\"", "", $fillcolor);
84  }
85  }
86  $this->areas[$index] = array(
87  "shape" => "$shape",
88  "coords" => "$coords",
89  "title" => "$title",
90  "href" => "$href",
91  "target" => "$target",
92  "linecolor" => '"' . $linecolor . '"',
93  "fillcolor" => '"' . $fillcolor . '"',
94  "bordercolor" => '"' . $bordercolor . '"',
95  "visible" => (int)$visible
96  );
97  }
98 
99  function addPoint(
100  $index,
101  $coords,
102  $visible = true,
103  $linecolor = "red",
104  $bordercolor = "white",
105  $fillcolor = "#FFFFFFA0"
106  )
107  {
108  $this->points[$index] = array(
109  "coords" => "$coords",
110  "linecolor" => '"' . $linecolor . '"',
111  "fillcolor" => '"' . $fillcolor . '"',
112  "bordercolor" => '"' . $bordercolor . '"',
113  "visible" => (int)$visible
114  );
115  }
116 
117  function getAreaIdent()
118  {
119  if (count($this->areas)+count($this->points) > 0)
120  {
121  $arr = array_merge(array_keys($this->areas), array_keys($this->points));
122  sort($arr, SORT_NUMERIC);
123 
124  $inner = join("_", $arr);
125  if (strlen($inner) > 32) {
126  $inner = md5($inner);
127  }
128  return "preview_" . $inner . "_";
129  }
130  else
131  {
132  return "";
133  }
134  }
135 
136  function createPreview()
137  {
138  if (count($this->areas)+count($this->points)==0) return;
139  include_once "./Services/Utilities/classes/class.ilUtil.php";
140  $convert_cmd = "-quality 100 ";
141  foreach ($this->points as $point)
142  {
143  if ($point["visible"])
144  {
145  preg_match("/(\d+)\s*,\s*(\d+)/", $point["coords"], $matches);
146  $x = $matches[1];
147  $y = $matches[2];
148  $r = 6;
149  // draw a circle at the point
150  $convert_cmd .= "-stroke " . $point["bordercolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"line " .
151  ($x-$r) . "," . ($y-$r) . " " . ($x+$r) . "," . ($y+$r) . "\" " .
152  "-stroke " . $point["bordercolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"line " .
153  ($x+$r) . "," . ($y-$r) . " " . ($x-$r) . "," . ($y+$r) . "\" " .
154  "-stroke " . $point["linecolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"line " .
155  ($x-$r) . "," . ($y-$r) . " " . ($x+$r) . "," . ($y+$r) . "\" " .
156  "-stroke " . $point["linecolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"line " .
157  ($x+$r) . "," . ($y-$r) . " " . ($x-$r) . "," . ($y+$r) . "\" ";
158  }
159  }
160  foreach ($this->areas as $area)
161  {
162  if ($area["visible"] and strcmp(strtolower($area["shape"]), "rect") == 0)
163  {
164  preg_match("/(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/", $area["coords"], $matches);
165  $x0 = $matches[1];
166  $y0 = $matches[2];
167  $x1 = $matches[3];
168  $y1 = $matches[4];
169  // draw a rect around the selection
170  $convert_cmd .= "-stroke " . $area["bordercolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"rectangle " .
171  $x0 . "," . $y0 . " " . ($x1) . "," . $y1 . "\" " .
172  "-stroke " . $area["linecolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"rectangle " .
173  $x0 . "," . $y0 . " " . ($x1) . "," . $y1 . "\" ";
174  }
175  else if ($area["visible"] and strcmp(strtolower($area["shape"]), "circle") == 0)
176  {
177  preg_match("/(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/", $area["coords"], $matches);
178  $x = $matches[1];
179  $y = $matches[2];
180  $r = $matches[3];
181  // draw a circle around the selection
182  $convert_cmd .= "-stroke " . $area["bordercolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"circle " .
183  $x . "," . $y . " " . ($x+$r) . "," . $y . "\" " .
184  "-stroke " . $area["linecolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"circle " .
185  $x . "," . $y . " " . ($x+$r) . "," . $y . "\" ";
186  }
187  else if ($area["visible"] and strcmp(strtolower($area["shape"]), "poly") == 0)
188  {
189  $obj = "polygon";
190  // draw a polygon around the selection
191  preg_match_all("/(\d+)\s*,\s*(\d+)/", $area["coords"], $matches, PREG_PATTERN_ORDER);
192  if (count($matches[0]) == 2) $obj = "line";
193  $convert_cmd .= "-stroke " . $area["bordercolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"$obj ";
194  for ($i = 0; $i < count($matches[0]); $i++)
195  {
196  $convert_cmd .= $matches[1][$i] . "," . $matches[2][$i] . " ";
197  }
198  $convert_cmd .= "\" ";
199  $convert_cmd .= "-stroke " . $area["linecolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"$obj ";
200  preg_match_all("/(\d+)\s*,\s*(\d+)/", $area["coords"], $matches, PREG_PATTERN_ORDER);
201  for ($i = 0; $i < count($matches[0]); $i++)
202  {
203  $convert_cmd .= $matches[1][$i] . "," . $matches[2][$i] . " ";
204  }
205  $convert_cmd .= "\" ";
206  }
207  }
208 
209  $source = ilUtil::escapeShellCmd($this->imagemap_filename);
210  $target = ilUtil::escapeShellCmd($this->preview_filename);
211  $convert_cmd = $source . "[0] " . $convert_cmd." ".$target;
212  ilUtil::execConvert($convert_cmd);
213  }
214 
215  function getPreviewFilename($imagePath, $baseFileName)
216  {
217  $filename = $baseFileName;
218  if (count($this->areas)+count($this->points)>0)
219  {
220  $pfile = $this->preview_filename;
221  if (is_file($pfile))
222  {
223  $ident = $this->getAreaIdent();
224  $previewfile = $imagePath . $ident . $baseFileName;
225  if (@md5_file($previewfile) != @md5_file($pfile))
226  {
227  if (strlen($ident) > 0)
228  {
229  @copy($pfile, $previewfile);
230  }
231  }
232  @unlink($pfile);
233  if (strlen($pfile) == 0)
234  {
235  ilUtil::sendInfo($this->lng->txt("qpl_imagemap_preview_missing"));
236  }
237  else
238  {
239  $filename = basename($previewfile);
240  }
241  }
242  else
243  {
244  ilUtil::sendInfo($this->lng->txt("qpl_imagemap_preview_missing"));
245  }
246  }
247  return $filename;
248  }
249 
254  function getImagemap($title)
255  {
256  $map = "<map name=\"$title\"> ";
257  foreach ($this->areas as $area)
258  {
259  $map .= "<area alt=\"" . $area["title"] . "\" title=\"" . $area["title"] . "\" ";
260  $map .= "shape=\"" . $area["shape"] . "\" ";
261  $map .= "coords=\"" . $area["coords"] . "\" ";
262  if ($area["href"])
263  {
264  $map .= "href=\"" . $area["href"] . "\" ";
265  if ($area["target"])
266  {
267  $map .= "target=\"" . $area["target"] . "\" ";
268  }
269  $map .= "/>\n";
270  }
271  else
272  {
273  $map .= "nohref />\n";
274  }
275  }
276  $map .= "</map>";
277  return $map;
278  }
279 
280 }
281 ?>
getPreviewFilename($imagePath, $baseFileName)
ilImagemapPreview($imagemap_filename="")
ilImagemapPreview constructor
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
static execConvert($args)
execute convert command
addPoint( $index, $coords, $visible=true, $linecolor="red", $bordercolor="white", $fillcolor="#FFFFFFA0")
$filename
Definition: buildRTE.php:89
static ilTempnam()
Create a temporary file in an ILIAS writable directory.
addArea( $index, $shape, $coords, $title="", $href="", $target="", $visible=true, $linecolor="red", $bordercolor="white", $fillcolor="#FFFFFFA0")
$r