ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 $lng;
37 $this->lng =&$lng;
38 $this->imagemap_filename = $imagemap_filename;
39 $this->preview_filename = $preview_filename;
40 if (!@is_file($this->preview_filename)) {
41 $extension = ".jpg";
42 if (preg_match("/.*\.(png|jpg|gif|jpeg)$/", $this->imagemap_filename, $matches)) {
43 $extension = "." . $matches[1];
44 }
45 include_once "./Services/Utilities/classes/class.ilUtil.php";
46 $this->preview_filename = ilUtil::ilTempnam() . $extension;
47 }
48 $this->areas = array();
49 $this->points = array();
50 $this->linewidth_outer = 4;
51 $this->linewidth_inner = 2;
52 }
53
54 public function getAreaCount()
55 {
56 return count($this->areas);
57 }
58
59 public function getPointCount()
60 {
61 return count($this->points);
62 }
63
64 public function addArea(
65 $index,
66 $shape,
67 $coords,
68 $title = "",
69 $href = "",
70 $target = "",
71 $visible = true,
72 $linecolor = "red",
73 $bordercolor = "white",
74 $fillcolor = "#FFFFFFA0"
75 ) {
76 if (ini_get("safe_mode")) {
77 if ((strpos($fillcolor, "#") !== false) || (strpos($fillcolor, "rgb") !== false)) {
78 $fillcolor = str_replace("\"", "", $fillcolor);
79 }
80 }
81 $this->areas[$index] = array(
82 "shape" => "$shape",
83 "coords" => "$coords",
84 "title" => "$title",
85 "href" => "$href",
86 "target" => "$target",
87 "linecolor" => '"' . $linecolor . '"',
88 "fillcolor" => '"' . $fillcolor . '"',
89 "bordercolor" => '"' . $bordercolor . '"',
90 "visible" => (int) $visible
91 );
92 }
93
94 public function addPoint(
95 $index,
96 $coords,
97 $visible = true,
98 $linecolor = "red",
99 $bordercolor = "white",
100 $fillcolor = "#FFFFFFA0"
101 ) {
102 $this->points[$index] = array(
103 "coords" => "$coords",
104 "linecolor" => '"' . $linecolor . '"',
105 "fillcolor" => '"' . $fillcolor . '"',
106 "bordercolor" => '"' . $bordercolor . '"',
107 "visible" => (int) $visible
108 );
109 }
110
111 public function getAreaIdent()
112 {
113 if (count($this->areas)+count($this->points) > 0) {
114 $arr = array_merge(array_keys($this->areas), array_keys($this->points));
115 sort($arr, SORT_NUMERIC);
116
117 $inner = join("_", $arr);
118 if (strlen($inner) > 32) {
119 $inner = md5($inner);
120 }
121 return "preview_" . $inner . "_";
122 } else {
123 return "";
124 }
125 }
126
127 public function createPreview()
128 {
129 if (count($this->areas)+count($this->points)==0) {
130 return;
131 }
132 include_once "./Services/Utilities/classes/class.ilUtil.php";
133 $convert_cmd = "-quality 100 ";
134 foreach ($this->points as $point) {
135 if ($point["visible"]) {
136 preg_match("/(\d+)\s*,\s*(\d+)/", $point["coords"], $matches);
137 $x = $matches[1];
138 $y = $matches[2];
139 $r = 6;
140 // draw a circle at the point
141 $convert_cmd .= "-stroke " . $point["bordercolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"line " .
142 ($x-$r) . "," . ($y-$r) . " " . ($x+$r) . "," . ($y+$r) . "\" " .
143 "-stroke " . $point["bordercolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"line " .
144 ($x+$r) . "," . ($y-$r) . " " . ($x-$r) . "," . ($y+$r) . "\" " .
145 "-stroke " . $point["linecolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"line " .
146 ($x-$r) . "," . ($y-$r) . " " . ($x+$r) . "," . ($y+$r) . "\" " .
147 "-stroke " . $point["linecolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"line " .
148 ($x+$r) . "," . ($y-$r) . " " . ($x-$r) . "," . ($y+$r) . "\" ";
149 }
150 }
151 foreach ($this->areas as $area) {
152 if ($area["visible"] and strcmp(strtolower($area["shape"]), "rect") == 0) {
153 preg_match("/(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/", $area["coords"], $matches);
154 $x0 = $matches[1];
155 $y0 = $matches[2];
156 $x1 = $matches[3];
157 $y1 = $matches[4];
158 // draw a rect around the selection
159 $convert_cmd .= "-stroke " . $area["bordercolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"rectangle " .
160 $x0 . "," . $y0 . " " . ($x1) . "," . $y1 . "\" " .
161 "-stroke " . $area["linecolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"rectangle " .
162 $x0 . "," . $y0 . " " . ($x1) . "," . $y1 . "\" ";
163 } elseif ($area["visible"] and strcmp(strtolower($area["shape"]), "circle") == 0) {
164 preg_match("/(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/", $area["coords"], $matches);
165 $x = $matches[1];
166 $y = $matches[2];
167 $r = $matches[3];
168 // draw a circle around the selection
169 $convert_cmd .= "-stroke " . $area["bordercolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"circle " .
170 $x . "," . $y . " " . ($x+$r) . "," . $y . "\" " .
171 "-stroke " . $area["linecolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"circle " .
172 $x . "," . $y . " " . ($x+$r) . "," . $y . "\" ";
173 } elseif ($area["visible"] and strcmp(strtolower($area["shape"]), "poly") == 0) {
174 $obj = "polygon";
175 // draw a polygon around the selection
176 preg_match_all("/(\d+)\s*,\s*(\d+)/", $area["coords"], $matches, PREG_PATTERN_ORDER);
177 if (count($matches[0]) == 2) {
178 $obj = "line";
179 }
180 $convert_cmd .= "-stroke " . $area["bordercolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"$obj ";
181 for ($i = 0; $i < count($matches[0]); $i++) {
182 $convert_cmd .= $matches[1][$i] . "," . $matches[2][$i] . " ";
183 }
184 $convert_cmd .= "\" ";
185 $convert_cmd .= "-stroke " . $area["linecolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"$obj ";
186 preg_match_all("/(\d+)\s*,\s*(\d+)/", $area["coords"], $matches, PREG_PATTERN_ORDER);
187 for ($i = 0; $i < count($matches[0]); $i++) {
188 $convert_cmd .= $matches[1][$i] . "," . $matches[2][$i] . " ";
189 }
190 $convert_cmd .= "\" ";
191 }
192 }
193
194 $source = ilUtil::escapeShellCmd($this->imagemap_filename);
195 $target = ilUtil::escapeShellCmd($this->preview_filename);
196 $convert_cmd = $source . "[0] " . $convert_cmd . " " . $target;
197 ilUtil::execConvert($convert_cmd);
198 }
199
200 public function getPreviewFilename($imagePath, $baseFileName)
201 {
202 $filename = $baseFileName;
203 if (count($this->areas)+count($this->points)>0) {
205 if (is_file($pfile)) {
206 $ident = $this->getAreaIdent();
207 $previewfile = $imagePath . $ident . $baseFileName;
208 if (@md5_file($previewfile) != @md5_file($pfile)) {
209 if (strlen($ident) > 0) {
210 @copy($pfile, $previewfile);
211 }
212 }
213 @unlink($pfile);
214 if (strlen($pfile) == 0) {
215 ilUtil::sendInfo($this->lng->txt("qpl_imagemap_preview_missing"));
216 } else {
217 $filename = basename($previewfile);
218 }
219 } else {
220 ilUtil::sendInfo($this->lng->txt("qpl_imagemap_preview_missing"));
221 }
222 }
223 return $filename;
224 }
225
230 public function getImagemap($title)
231 {
232 $map = "<map name=\"$title\"> ";
233 foreach ($this->areas as $area) {
234 $map .= "<area alt=\"" . $area["title"] . "\" title=\"" . $area["title"] . "\" ";
235 $map .= "shape=\"" . $area["shape"] . "\" ";
236 $map .= "coords=\"" . $area["coords"] . "\" ";
237 if ($area["href"]) {
238 $map .= "href=\"" . $area["href"] . "\" ";
239 if ($area["target"]) {
240 $map .= "target=\"" . $area["target"] . "\" ";
241 }
242 $map .= "/>\n";
243 } else {
244 $map .= "nohref />\n";
245 }
246 }
247 $map .= "</map>";
248 return $map;
249 }
250}
$source
Definition: linkback.php:22
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 execConvert($args)
execute convert command
static escapeShellCmd($a_arg)
escape shell cmd
static ilTempnam($a_temp_path=null)
Create a temporary file in an ILIAS writable directory.
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
$i
Definition: disco.tpl.php:19
$y
Definition: example_007.php:83
$x
Definition: example_009.php:98
$coords
Definition: example_030.php:88
$r
Definition: example_031.php:79
$index
Definition: metadata.php:60