ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilImagemapPreview.php
Go to the documentation of this file.
1 <?php
2  /*
3  +----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +----------------------------------------------------------------------------+
22 */
23 
24 include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
25 
37 {
40  var $areas;
41  var $points;
44  var $lng;
45 
55  {
56  global $lng;
57  $this->lng =& $lng;
58  $this->imagemap_filename = $imagemap_filename;
59  $this->preview_filename = $preview_filename;
60  if (!@is_file($this->preview_filename))
61  {
62  $extension = ".jpg";
63  if (preg_match("/.*\.(png|jpg|gif|jpeg)$/", $this->imagemap_filename, $matches))
64  {
65  $extension = "." . $matches[1];
66  }
67  include_once "./Services/Utilities/classes/class.ilUtil.php";
68  $this->preview_filename = ilUtil::ilTempnam() . $extension;
69  }
70  $this->areas = array();
71  $this->points = array();
72  $this->linewidth_outer = 4;
73  $this->linewidth_inner = 2;
74  }
75 
76  function getAreaCount()
77  {
78  return count($this->areas);
79  }
80 
81  function getPointCount()
82  {
83  return count($this->points);
84  }
85 
86  function addArea(
87  $index,
88  $shape,
89  $coords,
90  $title = "",
91  $href = "",
92  $target = "",
93  $visible = true,
94  $linecolor = "red",
95  $bordercolor = "white",
96  $fillcolor = "#FFFFFFA0"
97  )
98  {
99  if (ini_get("safe_mode"))
100  {
101  if ((strpos($fillcolor, "#") !== false) || (strpos($fillcolor, "rgb") !== false))
102  {
103  $fillcolor = str_replace("\"", "", $fillcolor);
104  }
105  }
106  $this->areas[$index] = array(
107  "shape" => "$shape",
108  "coords" => "$coords",
109  "title" => "$title",
110  "href" => "$href",
111  "target" => "$target",
112  "linecolor" => '"' . $linecolor . '"',
113  "fillcolor" => '"' . $fillcolor . '"',
114  "bordercolor" => '"' . $bordercolor . '"',
115  "visible" => (int)$visible
116  );
117  }
118 
119  function addPoint(
120  $index,
121  $coords,
122  $visible = true,
123  $linecolor = "red",
124  $bordercolor = "white",
125  $fillcolor = "#FFFFFFA0"
126  )
127  {
128  $this->points[$index] = array(
129  "coords" => "$coords",
130  "linecolor" => '"' . $linecolor . '"',
131  "fillcolor" => '"' . $fillcolor . '"',
132  "bordercolor" => '"' . $bordercolor . '"',
133  "visible" => (int)$visible
134  );
135  }
136 
137  function getAreaIdent()
138  {
139  if (count($this->areas)+count($this->points) > 0)
140  {
141  $arr = array_merge(array_keys($this->areas), array_keys($this->points));
142  sort($arr, SORT_NUMERIC);
143  return "preview_" . join("_", $arr) . "_";
144  }
145  else
146  {
147  return "";
148  }
149  }
150 
151  function createPreview()
152  {
153  if (count($this->areas)+count($this->points)==0) return;
154  include_once "./Services/Utilities/classes/class.ilUtil.php";
155  $convert_prefix = ilUtil::getConvertCmd() . " -quality 100 ";
156  foreach ($this->points as $point)
157  {
158  if ($point["visible"])
159  {
160  preg_match("/(\d+)\s*,\s*(\d+)/", $point["coords"], $matches);
161  $x = $matches[1];
162  $y = $matches[2];
163  $r = 6;
164  // draw a circle at the point
165  $convert_cmd .= "-stroke " . $point["bordercolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"line " .
166  ($x-$r) . "," . ($y-$r) . " " . ($x+$r) . "," . ($y+$r) . "\" " .
167  "-stroke " . $point["bordercolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"line " .
168  ($x+$r) . "," . ($y-$r) . " " . ($x-$r) . "," . ($y+$r) . "\" " .
169  "-stroke " . $point["linecolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"line " .
170  ($x-$r) . "," . ($y-$r) . " " . ($x+$r) . "," . ($y+$r) . "\" " .
171  "-stroke " . $point["linecolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"line " .
172  ($x+$r) . "," . ($y-$r) . " " . ($x-$r) . "," . ($y+$r) . "\" ";
173  }
174  }
175  foreach ($this->areas as $area)
176  {
177  if ($area["visible"] and strcmp(strtolower($area["shape"]), "rect") == 0)
178  {
179  preg_match("/(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/", $area["coords"], $matches);
180  $x0 = $matches[1];
181  $y0 = $matches[2];
182  $x1 = $matches[3];
183  $y1 = $matches[4];
184  // draw a rect around the selection
185  $convert_cmd .= "-stroke " . $area["bordercolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"rectangle " .
186  $x0 . "," . $y0 . " " . ($x1) . "," . $y1 . "\" " .
187  "-stroke " . $area["linecolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"rectangle " .
188  $x0 . "," . $y0 . " " . ($x1) . "," . $y1 . "\" ";
189  }
190  else if ($area["visible"] and strcmp(strtolower($area["shape"]), "circle") == 0)
191  {
192  preg_match("/(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/", $area["coords"], $matches);
193  $x = $matches[1];
194  $y = $matches[2];
195  $r = $matches[3];
196  // draw a circle around the selection
197  $convert_cmd .= "-stroke " . $area["bordercolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"circle " .
198  $x . "," . $y . " " . ($x+$r) . "," . $y . "\" " .
199  "-stroke " . $area["linecolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"circle " .
200  $x . "," . $y . " " . ($x+$r) . "," . $y . "\" ";
201  }
202  else if ($area["visible"] and strcmp(strtolower($area["shape"]), "poly") == 0)
203  {
204  $obj = "polygon";
205  // draw a polygon around the selection
206  preg_match_all("/(\d+)\s*,\s*(\d+)/", $area["coords"], $matches, PREG_PATTERN_ORDER);
207  if (count($matches[0]) == 2) $obj = "line";
208  $convert_cmd .= "-stroke " . $area["bordercolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"$obj ";
209  for ($i = 0; $i < count($matches[0]); $i++)
210  {
211  $convert_cmd .= $matches[1][$i] . "," . $matches[2][$i] . " ";
212  }
213  $convert_cmd .= "\" ";
214  $convert_cmd .= "-stroke " . $area["linecolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"$obj ";
215  preg_match_all("/(\d+)\s*,\s*(\d+)/", $area["coords"], $matches, PREG_PATTERN_ORDER);
216  for ($i = 0; $i < count($matches[0]); $i++)
217  {
218  $convert_cmd .= $matches[1][$i] . "," . $matches[2][$i] . " ";
219  }
220  $convert_cmd .= "\" ";
221  }
222  }
223 
224  $source = ilUtil::escapeShellCmd($this->imagemap_filename);
225  $target = ilUtil::escapeShellCmd($this->preview_filename);
226  $convert_cmd = $convert_prefix . $convert_cmd . $source ." " . $target;
227  system($convert_cmd);
228  }
229 
230  function getPreviewFilename($imagePath, $baseFileName)
231  {
232  $filename = $baseFileName;
233  if (count($this->areas)+count($this->points)>0)
234  {
235  $pfile = $this->preview_filename;
236  if (is_file($pfile))
237  {
238  $ident = $this->getAreaIdent();
239  $previewfile = $imagePath . $ident . $baseFileName;
240  if (@md5_file($previewfile) != @md5_file($pfile))
241  {
242  if (strlen($ident) > 0)
243  {
244  @copy($pfile, $previewfile);
245  }
246  }
247  @unlink($pfile);
248  if (strlen($pfile) == 0)
249  {
250  ilUtil::sendInfo($this->lng->txt("qpl_imagemap_preview_missing"));
251  }
252  else
253  {
254  $filename = basename($previewfile);
255  }
256  }
257  else
258  {
259  ilUtil::sendInfo($this->lng->txt("qpl_imagemap_preview_missing"));
260  }
261  }
262  return $filename;
263  }
264 
269  function getImagemap($title)
270  {
271  $map = "<map name=\"$title\"> ";
272  foreach ($this->areas as $area)
273  {
274  $map .= "<area alt=\"" . $area["title"] . "\" title=\"" . $area["title"] . "\" ";
275  $map .= "shape=\"" . $area["shape"] . "\" ";
276  $map .= "coords=\"" . $area["coords"] . "\" ";
277  if ($area["href"])
278  {
279  $map .= "href=\"" . $area["href"] . "\" ";
280  if ($area["target"])
281  {
282  $map .= "target=\"" . $area["target"] . "\" ";
283  }
284  $map .= "/>\n";
285  }
286  else
287  {
288  $map .= "nohref />\n";
289  }
290  }
291  $map .= "</map>";
292  return $map;
293  }
294 
295 }
296 ?>