ILIAS  release_8 Revision v8.24
class.ilImagemapPreview.php
Go to the documentation of this file.
1<?php
2
19include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
20
32{
35 public $areas;
36 public $points;
39 public $lng;
40 private \ilGlobalTemplateInterface $main_tpl;
41
50 public function __construct($imagemap_filename = "")
51 {
52 global $DIC;
53 $this->main_tpl = $DIC->ui()->mainTemplate();
54 $lng = $DIC['lng'];
55 $this->lng = &$lng;
56 $this->imagemap_filename = $imagemap_filename;
57
58 if (!@is_file($this->preview_filename)) {
59 $extension = ".jpg";
60 if (preg_match("/.*\.(png|jpg|gif|jpeg)$/", $this->imagemap_filename, $matches)) {
61 $extension = "." . $matches[1];
62 }
63 include_once "./Services/Utilities/classes/class.ilUtil.php";
64 $this->preview_filename = ilFileUtils::ilTempnam() . $extension;
65 }
66 $this->areas = array();
67 $this->points = array();
68 $this->linewidth_outer = 4;
69 $this->linewidth_inner = 2;
70 }
71
72 public function getAreaCount(): int
73 {
74 return count($this->areas);
75 }
76
77 public function getPointCount(): int
78 {
79 return count($this->points);
80 }
81
82 public function addArea(
83 $index,
84 $shape,
85 $coords,
86 $title = "",
87 $href = "",
88 $target = "",
89 $visible = true,
90 $linecolor = "red",
91 $bordercolor = "white",
92 $fillcolor = "#FFFFFFA0"
93 ): void {
94 if (ini_get("safe_mode")) {
95 if ((strpos($fillcolor, "#") !== false) || (strpos($fillcolor, "rgb") !== false)) {
96 $fillcolor = str_replace("\"", "", $fillcolor);
97 }
98 }
99 $this->areas[$index] = array(
100 "shape" => "$shape",
101 "coords" => "$coords",
102 "title" => htmlspecialchars($title),
103 "href" => "$href",
104 "target" => "$target",
105 "linecolor" => '"' . $linecolor . '"',
106 "fillcolor" => '"' . $fillcolor . '"',
107 "bordercolor" => '"' . $bordercolor . '"',
108 "visible" => (int) $visible
109 );
110 }
111
112 public function addPoint(
113 $index,
114 $coords,
115 $visible = true,
116 $linecolor = "red",
117 $bordercolor = "white",
118 $fillcolor = "#FFFFFFA0"
119 ): void {
120 $this->points[$index] = array(
121 "coords" => "$coords",
122 "linecolor" => '"' . $linecolor . '"',
123 "fillcolor" => '"' . $fillcolor . '"',
124 "bordercolor" => '"' . $bordercolor . '"',
125 "visible" => (int) $visible
126 );
127 }
128
129 public function getAreaIdent(): string
130 {
131 if (count($this->areas) + count($this->points) > 0) {
132 $arr = array_merge(array_keys($this->areas), array_keys($this->points));
133 sort($arr, SORT_NUMERIC);
134
135 $inner = join("_", $arr);
136 if (strlen($inner) > 32) {
137 $inner = md5($inner);
138 }
139 return "preview_" . $inner . "_";
140 } else {
141 return "";
142 }
143 }
144
145 public function createPreview(): void
146 {
147 if (count($this->areas) + count($this->points) == 0) {
148 return;
149 }
150 include_once "./Services/Utilities/classes/class.ilUtil.php";
151 $convert_cmd = "-quality 100 ";
152 foreach ($this->points as $point) {
153 if ($point["visible"]) {
154 preg_match("/(\d+)\s*,\s*(\d+)/", $point["coords"], $matches);
155 $x = $matches[1];
156 $y = $matches[2];
157 $r = 6;
158 // draw a circle at the point
159 $convert_cmd .= "-stroke " . $point["bordercolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"line " .
160 ($x - $r) . "," . ($y - $r) . " " . ($x + $r) . "," . ($y + $r) . "\" " .
161 "-stroke " . $point["bordercolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"line " .
162 ($x + $r) . "," . ($y - $r) . " " . ($x - $r) . "," . ($y + $r) . "\" " .
163 "-stroke " . $point["linecolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"line " .
164 ($x - $r) . "," . ($y - $r) . " " . ($x + $r) . "," . ($y + $r) . "\" " .
165 "-stroke " . $point["linecolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"line " .
166 ($x + $r) . "," . ($y - $r) . " " . ($x - $r) . "," . ($y + $r) . "\" ";
167 }
168 }
169 foreach ($this->areas as $area) {
170 if ($area["visible"] and strcmp(strtolower($area["shape"]), "rect") == 0) {
171 preg_match("/(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/", $area["coords"], $matches);
172 $x0 = $matches[1];
173 $y0 = $matches[2];
174 $x1 = $matches[3];
175 $y1 = $matches[4];
176 // draw a rect around the selection
177 $convert_cmd .= "-stroke " . $area["bordercolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"rectangle " .
178 $x0 . "," . $y0 . " " . ($x1) . "," . $y1 . "\" " .
179 "-stroke " . $area["linecolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"rectangle " .
180 $x0 . "," . $y0 . " " . ($x1) . "," . $y1 . "\" ";
181 } elseif ($area["visible"] and strcmp(strtolower($area["shape"]), "circle") == 0) {
182 preg_match("/(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/", $area["coords"], $matches);
183 $x = $matches[1];
184 $y = $matches[2];
185 $r = $matches[3];
186 // draw a circle around the selection
187 $convert_cmd .= "-stroke " . $area["bordercolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"circle " .
188 $x . "," . $y . " " . ($x + $r) . "," . $y . "\" " .
189 "-stroke " . $area["linecolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"circle " .
190 $x . "," . $y . " " . ($x + $r) . "," . $y . "\" ";
191 } elseif ($area["visible"] and strcmp(strtolower($area["shape"]), "poly") == 0) {
192 $obj = "polygon";
193 // draw a polygon around the selection
194 preg_match_all("/(\d+)\s*,\s*(\d+)/", $area["coords"], $matches, PREG_PATTERN_ORDER);
195 if (count($matches[0]) == 2) {
196 $obj = "line";
197 }
198 $convert_cmd .= "-stroke " . $area["bordercolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"$obj ";
199 for ($i = 0; $i < count($matches[0]); $i++) {
200 $convert_cmd .= $matches[1][$i] . "," . $matches[2][$i] . " ";
201 }
202 $convert_cmd .= "\" ";
203 $convert_cmd .= "-stroke " . $area["linecolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"$obj ";
204 preg_match_all("/(\d+)\s*,\s*(\d+)/", $area["coords"], $matches, PREG_PATTERN_ORDER);
205 for ($i = 0; $i < count($matches[0]); $i++) {
206 $convert_cmd .= $matches[1][$i] . "," . $matches[2][$i] . " ";
207 }
208 $convert_cmd .= "\" ";
209 }
210 }
211
212 $source = $this->escapeShellCmd($this->imagemap_filename);
213 $target = $this->escapeShellCmd($this->preview_filename);
214 $convert_cmd = $this->escapeShellCmd($convert_cmd);
215 $convert_cmd = preg_replace('/\\\\(#([a-fA-F0-9]{3}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8}))/', '${1}', $convert_cmd);
216 $convert_cmd = $source . "[0] " . $convert_cmd . " " . $target;
217 $this->execQuoted(PATH_TO_CONVERT, $convert_cmd);
218 }
219
220 public static function escapeShellCmd($a_arg)
221 {
222 if (ini_get('safe_mode') == 1) {
223 return $a_arg;
224 }
225 setlocale(LC_CTYPE, "UTF8", "en_US.UTF-8"); // fix for PHP escapeshellcmd bug. See: http://bugs.php.net/bug.php?id=45132
226 return escapeshellcmd($a_arg);
227 }
228
229 public static function execQuoted($cmd, $args = null)
230 {
231 global $DIC;
232
233 if (ilUtil::isWindows() && strpos($cmd, " ") !== false && substr($cmd, 0, 1) !== '"') {
234 $cmd = '"' . $cmd . '"';
235 if ($args) {
236 $cmd .= " " . $args;
237 }
238 } elseif ($args) {
239 $cmd .= " " . $args;
240 }
241 exec($cmd, $arr);
242
243 $DIC->logger()->root()->debug("ilUtil::execQuoted: " . $cmd . ".");
244
245 return $arr;
246 }
247
248 public function getPreviewFilename($imagePath, $baseFileName)
249 {
250 $filename = $baseFileName;
251 if (count($this->areas) + count($this->points) > 0) {
252 $pfile = $this->preview_filename;
253 if (is_file($pfile)) {
254 $ident = $this->getAreaIdent();
255 $previewfile = $imagePath . $ident . $baseFileName;
256 if (@md5_file($previewfile) != @md5_file($pfile)) {
257 if (strlen($ident) > 0) {
258 @copy($pfile, $previewfile);
259 }
260 }
261 @unlink($pfile);
262 if (strlen($pfile) == 0) {
263 $this->main_tpl->setOnScreenMessage('info', $this->lng->txt("qpl_imagemap_preview_missing"));
264 } else {
265 $filename = basename($previewfile);
266 }
267 } else {
268 $this->main_tpl->setOnScreenMessage('info', $this->lng->txt("qpl_imagemap_preview_missing"));
269 }
270 }
271 return $filename;
272 }
273
278 public function getImagemap($title): string
279 {
280 $map = "<map name=\"$title\"> ";
281 foreach ($this->areas as $area) {
282 $map .= "<area alt=\"" . $area["title"] . "\" title=\"" . $area["title"] . "\" ";
283 $map .= "shape=\"" . $area["shape"] . "\" ";
284 $map .= "coords=\"" . $area["coords"] . "\" ";
285 if ($area["href"]) {
286 $map .= "href=\"" . $area["href"] . "\" ";
287 if ($area["target"]) {
288 $map .= "target=\"" . $area["target"] . "\" ";
289 }
290 $map .= "/>\n";
291 } else {
292 $map .= "nohref />\n";
293 }
294 }
295 $map .= "</map>";
296 return $map;
297 }
298}
$filename
Definition: buildRTE.php:78
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static execQuoted($cmd, $args=null)
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
ilGlobalTemplateInterface $main_tpl
__construct($imagemap_filename="")
ilImagemapPreview constructor
addArea( $index, $shape, $coords, $title="", $href="", $target="", $visible=true, $linecolor="red", $bordercolor="white", $fillcolor="#FFFFFFA0")
static isWindows()
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
global $DIC
Definition: feed.php:28
$index
Definition: metadata.php:145
$i
Definition: metadata.php:41
$source
Definition: metadata.php:93