ILIAS  Release_4_2_x_branch Revision 61807
 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 
144  $inner = join("_", $arr);
145  if (strlen($inner) > 32) {
146  $inner = md5($inner);
147  }
148  return "preview_" . $inner . "_";
149  }
150  else
151  {
152  return "";
153  }
154  }
155 
156  function createPreview()
157  {
158  if (count($this->areas)+count($this->points)==0) return;
159  include_once "./Services/Utilities/classes/class.ilUtil.php";
160  $convert_cmd = "-quality 100 ";
161  foreach ($this->points as $point)
162  {
163  if ($point["visible"])
164  {
165  preg_match("/(\d+)\s*,\s*(\d+)/", $point["coords"], $matches);
166  $x = $matches[1];
167  $y = $matches[2];
168  $r = 6;
169  // draw a circle at the point
170  $convert_cmd .= "-stroke " . $point["bordercolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"line " .
171  ($x-$r) . "," . ($y-$r) . " " . ($x+$r) . "," . ($y+$r) . "\" " .
172  "-stroke " . $point["bordercolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"line " .
173  ($x+$r) . "," . ($y-$r) . " " . ($x-$r) . "," . ($y+$r) . "\" " .
174  "-stroke " . $point["linecolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"line " .
175  ($x-$r) . "," . ($y-$r) . " " . ($x+$r) . "," . ($y+$r) . "\" " .
176  "-stroke " . $point["linecolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"line " .
177  ($x+$r) . "," . ($y-$r) . " " . ($x-$r) . "," . ($y+$r) . "\" ";
178  }
179  }
180  foreach ($this->areas as $area)
181  {
182  if ($area["visible"] and strcmp(strtolower($area["shape"]), "rect") == 0)
183  {
184  preg_match("/(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/", $area["coords"], $matches);
185  $x0 = $matches[1];
186  $y0 = $matches[2];
187  $x1 = $matches[3];
188  $y1 = $matches[4];
189  // draw a rect around the selection
190  $convert_cmd .= "-stroke " . $area["bordercolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"rectangle " .
191  $x0 . "," . $y0 . " " . ($x1) . "," . $y1 . "\" " .
192  "-stroke " . $area["linecolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"rectangle " .
193  $x0 . "," . $y0 . " " . ($x1) . "," . $y1 . "\" ";
194  }
195  else if ($area["visible"] and strcmp(strtolower($area["shape"]), "circle") == 0)
196  {
197  preg_match("/(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/", $area["coords"], $matches);
198  $x = $matches[1];
199  $y = $matches[2];
200  $r = $matches[3];
201  // draw a circle around the selection
202  $convert_cmd .= "-stroke " . $area["bordercolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"circle " .
203  $x . "," . $y . " " . ($x+$r) . "," . $y . "\" " .
204  "-stroke " . $area["linecolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"circle " .
205  $x . "," . $y . " " . ($x+$r) . "," . $y . "\" ";
206  }
207  else if ($area["visible"] and strcmp(strtolower($area["shape"]), "poly") == 0)
208  {
209  $obj = "polygon";
210  // draw a polygon around the selection
211  preg_match_all("/(\d+)\s*,\s*(\d+)/", $area["coords"], $matches, PREG_PATTERN_ORDER);
212  if (count($matches[0]) == 2) $obj = "line";
213  $convert_cmd .= "-stroke " . $area["bordercolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"$obj ";
214  for ($i = 0; $i < count($matches[0]); $i++)
215  {
216  $convert_cmd .= $matches[1][$i] . "," . $matches[2][$i] . " ";
217  }
218  $convert_cmd .= "\" ";
219  $convert_cmd .= "-stroke " . $area["linecolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"$obj ";
220  preg_match_all("/(\d+)\s*,\s*(\d+)/", $area["coords"], $matches, PREG_PATTERN_ORDER);
221  for ($i = 0; $i < count($matches[0]); $i++)
222  {
223  $convert_cmd .= $matches[1][$i] . "," . $matches[2][$i] . " ";
224  }
225  $convert_cmd .= "\" ";
226  }
227  }
228 
229  $source = ilUtil::escapeShellCmd($this->imagemap_filename);
230  $target = ilUtil::escapeShellCmd($this->preview_filename);
231  $convert_cmd = $source . "[0] " . $convert_cmd." ".$target;
232  ilUtil::execConvert($convert_cmd);
233  }
234 
235  function getPreviewFilename($imagePath, $baseFileName)
236  {
237  $filename = $baseFileName;
238  if (count($this->areas)+count($this->points)>0)
239  {
240  $pfile = $this->preview_filename;
241  if (is_file($pfile))
242  {
243  $ident = $this->getAreaIdent();
244  $previewfile = $imagePath . $ident . $baseFileName;
245  if (@md5_file($previewfile) != @md5_file($pfile))
246  {
247  if (strlen($ident) > 0)
248  {
249  @copy($pfile, $previewfile);
250  }
251  }
252  @unlink($pfile);
253  if (strlen($pfile) == 0)
254  {
255  ilUtil::sendInfo($this->lng->txt("qpl_imagemap_preview_missing"));
256  }
257  else
258  {
259  $filename = basename($previewfile);
260  }
261  }
262  else
263  {
264  ilUtil::sendInfo($this->lng->txt("qpl_imagemap_preview_missing"));
265  }
266  }
267  return $filename;
268  }
269 
274  function getImagemap($title)
275  {
276  $map = "<map name=\"$title\"> ";
277  foreach ($this->areas as $area)
278  {
279  $map .= "<area alt=\"" . $area["title"] . "\" title=\"" . $area["title"] . "\" ";
280  $map .= "shape=\"" . $area["shape"] . "\" ";
281  $map .= "coords=\"" . $area["coords"] . "\" ";
282  if ($area["href"])
283  {
284  $map .= "href=\"" . $area["href"] . "\" ";
285  if ($area["target"])
286  {
287  $map .= "target=\"" . $area["target"] . "\" ";
288  }
289  $map .= "/>\n";
290  }
291  else
292  {
293  $map .= "nohref />\n";
294  }
295  }
296  $map .= "</map>";
297  return $map;
298  }
299 
300 }
301 ?>