ILIAS  release_7 Revision v7.30-3-g800a261c036
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
4include_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" => htmlspecialchars($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 = ilUtil::escapeShellCmd($convert_cmd);
198 $convert_cmd = preg_replace('/\\\\(#([a-fA-F0-9]{3}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8}))/', '${1}', $convert_cmd);
199 $convert_cmd = $source . "[0] " . $convert_cmd . " " . $target;
200 ilUtil::execQuoted(PATH_TO_CONVERT, $convert_cmd);
201 }
202
203 public function getPreviewFilename($imagePath, $baseFileName)
204 {
205 $filename = $baseFileName;
206 if (count($this->areas) + count($this->points) > 0) {
208 if (is_file($pfile)) {
209 $ident = $this->getAreaIdent();
210 $previewfile = $imagePath . $ident . $baseFileName;
211 if (@md5_file($previewfile) != @md5_file($pfile)) {
212 if (strlen($ident) > 0) {
213 @copy($pfile, $previewfile);
214 }
215 }
216 @unlink($pfile);
217 if (strlen($pfile) == 0) {
218 ilUtil::sendInfo($this->lng->txt("qpl_imagemap_preview_missing"));
219 } else {
220 $filename = basename($previewfile);
221 }
222 } else {
223 ilUtil::sendInfo($this->lng->txt("qpl_imagemap_preview_missing"));
224 }
225 }
226 return $filename;
227 }
228
233 public function getImagemap($title)
234 {
235 $map = "<map name=\"$title\"> ";
236 foreach ($this->areas as $area) {
237 $map .= "<area alt=\"" . $area["title"] . "\" title=\"" . $area["title"] . "\" ";
238 $map .= "shape=\"" . $area["shape"] . "\" ";
239 $map .= "coords=\"" . $area["coords"] . "\" ";
240 if ($area["href"]) {
241 $map .= "href=\"" . $area["href"] . "\" ";
242 if ($area["target"]) {
243 $map .= "target=\"" . $area["target"] . "\" ";
244 }
245 $map .= "/>\n";
246 } else {
247 $map .= "nohref />\n";
248 }
249 }
250 $map .= "</map>";
251 return $map;
252 }
253}
$filename
Definition: buildRTE.php:89
An exception for terminatinating execution or to throw for unit testing.
Image map image preview creator.
getPreviewFilename($imagePath, $baseFileName)
addPoint( $index, $coords, $visible=true, $linecolor="red", $bordercolor="white", $fillcolor="#FFFFFFA0")
getImagemap($title)
get imagemap html code note: html code should be placed in template files
__construct($imagemap_filename="")
ilImagemapPreview constructor
addArea( $index, $shape, $coords, $title="", $href="", $target="", $visible=true, $linecolor="red", $bordercolor="white", $fillcolor="#FFFFFFA0")
static execQuoted($cmd, $args=null)
exec command and fix spaces on windows
static escapeShellCmd($a_arg)
escape shell cmd
static ilTempnam($a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
global $DIC
Definition: goto.php:24
$index
Definition: metadata.php:128
$i
Definition: metadata.php:24
$source
Definition: metadata.php:76