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