00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
00025
00036 class ilImagemapPreview
00037 {
00038 var $imagemap_filename;
00039 var $preview_filename;
00040 var $areas;
00041 var $points;
00042 var $linewidth_outer;
00043 var $linewidth_inner;
00044 var $lng;
00045
00054 function ilImagemapPreview($imagemap_filename = "")
00055 {
00056 global $lng;
00057 $this->lng =& $lng;
00058 $this->imagemap_filename = $imagemap_filename;
00059 $this->preview_filename = $preview_filename;
00060 if (!@is_file($this->preview_filename))
00061 {
00062 $extension = ".jpg";
00063 if (preg_match("/.*\.(png|jpg|gif|jpeg)$/", $this->imagemap_filename, $matches))
00064 {
00065 $extension = "." . $matches[1];
00066 }
00067 include_once "./Services/Utilities/classes/class.ilUtil.php";
00068 $this->preview_filename = ilUtil::ilTempnam() . $extension;
00069 }
00070 $this->areas = array();
00071 $this->points = array();
00072 $this->linewidth_outer = 4;
00073 $this->linewidth_inner = 2;
00074 }
00075
00076 function getAreaCount()
00077 {
00078 return count($this->areas);
00079 }
00080
00081 function getPointCount()
00082 {
00083 return count($this->points);
00084 }
00085
00086 function addArea(
00087 $index,
00088 $shape,
00089 $coords,
00090 $title = "",
00091 $href = "",
00092 $target = "",
00093 $visible = true,
00094 $linecolor = "red",
00095 $bordercolor = "white",
00096 $fillcolor = "\"#FFFFFFA0\""
00097 )
00098 {
00099 if (ini_get("safe_mode"))
00100 {
00101 if ((strpos($fillcolor, "#") !== false) || (strpos($fillcolor, "rgb") !== false))
00102 {
00103 $fillcolor = str_replace("\"", "", $fillcolor);
00104 }
00105 }
00106 $this->areas[$index] = array(
00107 "shape" => "$shape",
00108 "coords" => "$coords",
00109 "title" => "$title",
00110 "href" => "$href",
00111 "target" => "$target",
00112 "linecolor" => "$linecolor",
00113 "fillcolor" => "$fillcolor",
00114 "bordercolor" => "$bordercolor",
00115 "visible" => (int)$visible
00116 );
00117 }
00118
00119 function addPoint(
00120 $index,
00121 $coords,
00122 $visible = true,
00123 $linecolor = "red",
00124 $bordercolor = "white",
00125 $fillcolor = "\"#FFFFFFA0\""
00126 )
00127 {
00128 $this->points[$index] = array(
00129 "coords" => "$coords",
00130 "linecolor" => "$linecolor",
00131 "bordercolor" => "$bordercolor",
00132 "fillcolor" => "$fillcolor",
00133 "visible" => (int)$visible
00134 );
00135 }
00136
00137 function getAreaIdent()
00138 {
00139 if (count($this->areas)+count($this->points) > 0)
00140 {
00141 $arr = array_merge(array_keys($this->areas), array_keys($this->points));
00142 sort($arr, SORT_NUMERIC);
00143 return "preview_" . join("_", $arr) . "_";
00144 }
00145 else
00146 {
00147 return "";
00148 }
00149 }
00150
00151 function createPreview()
00152 {
00153 if (count($this->areas)+count($this->points)==0) return;
00154 include_once "./Services/Utilities/classes/class.ilUtil.php";
00155 $convert_prefix = ilUtil::getConvertCmd() . " -quality 100 ";
00156 foreach ($this->points as $point)
00157 {
00158 if ($point["visible"])
00159 {
00160 preg_match("/(\d+)\s*,\s*(\d+)/", $point["coords"], $matches);
00161 $x = $matches[1];
00162 $y = $matches[2];
00163 $r = 6;
00164
00165 $convert_cmd .= "-stroke " . $point["bordercolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"line " .
00166 ($x-$r) . "," . ($y-$r) . " " . ($x+$r) . "," . ($y+$r) . "\" " .
00167 "-stroke " . $point["bordercolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"line " .
00168 ($x+$r) . "," . ($y-$r) . " " . ($x-$r) . "," . ($y+$r) . "\" " .
00169 "-stroke " . $point["linecolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"line " .
00170 ($x-$r) . "," . ($y-$r) . " " . ($x+$r) . "," . ($y+$r) . "\" " .
00171 "-stroke " . $point["linecolor"] . " -fill " . $point["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"line " .
00172 ($x+$r) . "," . ($y-$r) . " " . ($x-$r) . "," . ($y+$r) . "\" ";
00173 }
00174 }
00175 foreach ($this->areas as $area)
00176 {
00177 if ($area["visible"] and strcmp(strtolower($area["shape"]), "rect") == 0)
00178 {
00179 preg_match("/(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/", $area["coords"], $matches);
00180 $x0 = $matches[1];
00181 $y0 = $matches[2];
00182 $x1 = $matches[3];
00183 $y1 = $matches[4];
00184
00185 $convert_cmd .= "-stroke " . $area["bordercolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"rectangle " .
00186 $x0 . "," . $y0 . " " . ($x1) . "," . $y1 . "\" " .
00187 "-stroke " . $area["linecolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"rectangle " .
00188 $x0 . "," . $y0 . " " . ($x1) . "," . $y1 . "\" ";
00189 }
00190 else if ($area["visible"] and strcmp(strtolower($area["shape"]), "circle") == 0)
00191 {
00192 preg_match("/(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/", $area["coords"], $matches);
00193 $x = $matches[1];
00194 $y = $matches[2];
00195 $r = $matches[3];
00196
00197 $convert_cmd .= "-stroke " . $area["bordercolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"circle " .
00198 $x . "," . $y . " " . ($x+$r) . "," . $y . "\" " .
00199 "-stroke " . $area["linecolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"circle " .
00200 $x . "," . $y . " " . ($x+$r) . "," . $y . "\" ";
00201 }
00202 else if ($area["visible"] and strcmp(strtolower($area["shape"]), "poly") == 0)
00203 {
00204 $obj = "polygon";
00205
00206 preg_match_all("/(\d+)\s*,\s*(\d+)/", $area["coords"], $matches, PREG_PATTERN_ORDER);
00207 if (count($matches[0]) == 2) $obj = "line";
00208 $convert_cmd .= "-stroke " . $area["bordercolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_outer -draw \"$obj ";
00209 for ($i = 0; $i < count($matches[0]); $i++)
00210 {
00211 $convert_cmd .= $matches[1][$i] . "," . $matches[2][$i] . " ";
00212 }
00213 $convert_cmd .= "\" ";
00214 $convert_cmd .= "-stroke " . $area["linecolor"] . " -fill " . $area["fillcolor"] . " -strokewidth $this->linewidth_inner -draw \"$obj ";
00215 preg_match_all("/(\d+)\s*,\s*(\d+)/", $area["coords"], $matches, PREG_PATTERN_ORDER);
00216 for ($i = 0; $i < count($matches[0]); $i++)
00217 {
00218 $convert_cmd .= $matches[1][$i] . "," . $matches[2][$i] . " ";
00219 }
00220 $convert_cmd .= "\" ";
00221 }
00222 }
00223
00224 $source = ilUtil::escapeShellCmd($this->imagemap_filename);
00225 $target = ilUtil::escapeShellCmd($this->preview_filename);
00226 $convert_cmd = $convert_prefix . $convert_cmd . $source ." " . $target;
00227 system($convert_cmd);
00228 }
00229
00230 function getPreviewFilename($imagePath, $baseFileName)
00231 {
00232 $filename = $baseFileName;
00233 if (count($this->areas)+count($this->points)>0)
00234 {
00235 $pfile = $this->preview_filename;
00236 if (is_file($pfile))
00237 {
00238 $ident = $this->getAreaIdent();
00239 $previewfile = $imagePath . $ident . $baseFileName;
00240 if (@md5_file($previewfile) != @md5_file($pfile))
00241 {
00242 if (strlen($ident) > 0)
00243 {
00244 @copy($pfile, $previewfile);
00245 }
00246 }
00247 @unlink($pfile);
00248 if (strlen($pfile) == 0)
00249 {
00250 ilUtil::sendInfo($this->lng->txt("qpl_imagemap_preview_missing"));
00251 }
00252 else
00253 {
00254 $filename = basename($previewfile);
00255 }
00256 }
00257 else
00258 {
00259 ilUtil::sendInfo($this->lng->txt("qpl_imagemap_preview_missing"));
00260 }
00261 }
00262 return $filename;
00263 }
00264
00269 function getImagemap($title)
00270 {
00271 $map = "<map name=\"$title\"> ";
00272 foreach ($this->areas as $area)
00273 {
00274 $map .= "<area alt=\"" . $area["title"] . "\" title=\"" . $area["title"] . "\" ";
00275 $map .= "shape=\"" . $area["shape"] . "\" ";
00276 $map .= "coords=\"" . $area["coords"] . "\" ";
00277 if ($area["href"])
00278 {
00279 $map .= "href=\"" . $area["href"] . "\" ";
00280 if ($area["target"])
00281 {
00282 $map .= "target=\"" . $area["target"] . "\" ";
00283 }
00284 $map .= "/>\n";
00285 }
00286 else
00287 {
00288 $map .= "nohref />\n";
00289 }
00290 }
00291 $map .= "</map>";
00292 return $map;
00293 }
00294
00295 }
00296 ?>