source: trunk/plugins/InsertPicture/InsertPicture.php @ 438

Last change on this file since 438 was 438, checked in by gocher, 14 years ago

add width and hight to insertPicture plugin

File size: 10.1 KB
Line 
1<?PHP
2  //this plugin only use the relativ webpath to the picturefolder
3  //default ~  /htmlarea/plugins/InsertPicture/demo_pictures/
4  strstr( PHP_OS, "WIN") ? $strPathSeparator = "\\" : $strPathSeparator = "/";
5  if (isset($_REQUEST['picturepath'])) {
6    $getRequest = true;
7    $PicturePath = 'http://'.$_SERVER['HTTP_HOST'].$_REQUEST['picturepath'];
8    //$LocalPicturePath = $_REQUEST['localpicturepath'];
9
10    $AInsertPicturePath = explode ('/', 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/demo_pictures/');
11    $ALocalInsertPicturePath = explode($strPathSeparator, dirname(__FILE__).$strPathSeparator.'demo_pictures');
12    $APicturePath = explode('/', 'http://'.$_SERVER['HTTP_HOST'].$_REQUEST['picturepath']);
13
14    $AtheFilePath = array_values (array_diff ($APicturePath, $AInsertPicturePath));
15    $theFilePath = implode($strPathSeparator, $AtheFilePath).$strPathSeparator;
16
17    $AtheRootPath = array_values (array_diff ($ALocalInsertPicturePath, $AInsertPicturePath));
18    $theRootPath = implode($strPathSeparator, $AtheRootPath);
19
20    $LocalPicturePath = $theRootPath.$strPathSeparator.$theFilePath.$strPathSeparator;
21  } else {
22    $getRequest = false;
23    $PicturePath =  'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/demo_pictures/';
24    $LocalPicturePath = dirname(__FILE__).$strPathSeparator.'demo_pictures';
25  }
26  $limitedext = array(".gif",".jpg",".png",".jpeg"); //Extensions you want files uploaded limited to.
27  $limitedsize = "1000000"; //size limit in bytes
28  $message = "";
29  function formatSize($size)
30  {
31    if($size < 1024)
32      return $size.' bytes';   
33    else if($size >= 1024 && $size < 1024*1024)
34      return sprintf('%01.2f',$size/1024.0).' Kb';     
35    else
36      return sprintf('%01.2f',$size/(1024.0*1024)).' Mb';       
37  }
38
39  if (isset($_FILES['file'])) {
40    $file = $_FILES['file'];
41    $ext = strrchr($file['name'],'.');
42    if (!in_array($ext,$limitedext)) {
43      $message = "The file you are uploading doesn't have the correct extension.";
44    } else if (file_exists($LocalPicturePath.'\\'.$file['name'])) {
45      $message = "The file you are uploading already exists.";
46    } else if ($file['size'] > $limitedsize) {
47      $message = "The file you are uploading is to big. The max Filesize is</span><span> ".formatSize($limitedsize).".";
48    } else {
49      copy($file['tmp_name'], $LocalPicturePath.$strPathSeparator.$file['name']);
50    }
51  }
52
53?>
54<html>
55
56<head>
57  <title>Insert Image</title>
58       
59<link rel="stylesheet" type="text/css" href="../../popups/popup.css" />
60<script type="text/javascript" src="../../popups/popup.js"></script>
61
62<script type="text/javascript">
63
64var HTMLArea = window.opener.HTMLArea;
65function i18n(str) {
66  return (HTMLArea._lc(str, 'HTMLArea'));
67}
68
69function Init() {
70  __dlg_translate("InsertPicture");
71  __dlg_init();
72  window.resizeTo(500, 490);
73  // Make sure the translated string appears in the drop down. (for gecko)
74  document.getElementById("f_align").selectedIndex = 1;
75  document.getElementById("f_align").selectedIndex = 5;
76  var param = window.dialogArguments;
77  if (param) {
78      document.getElementById("f_url").value = param["f_url"];
79      document.getElementById("f_alt").value = param["f_alt"];
80      document.getElementById("f_border").value = param["f_border"];
81      document.getElementById("f_align").value = param["f_align"];
82      document.getElementById("f_vert").value = param["f_vert"];
83      document.getElementById("f_horiz").value = param["f_horiz"];
84      document.getElementById("f_height").value = param["f_height"];
85      document.getElementById("f_width").value = param["f_width"];                     
86      window.ipreview.location.replace(param.f_url);
87  }
88  document.getElementById("f_url").focus();
89}
90
91function onOK() {
92  var required = {
93    "f_url": i18n("You must enter the URL")
94  };
95  for (var i in required) {
96    var el = document.getElementById(i);
97    if (!el.value) {
98      alert(required[i]);
99      el.focus();
100      return false;
101    }
102  }
103  // pass data back to the calling window
104  var fields = ["f_url", "f_alt", "f_align", "f_border",
105                "f_horiz", "f_vert"];
106  var param = new Object();
107  for (var i in fields) {
108    var id = fields[i];
109    var el = document.getElementById(id);
110    param[id] = el.value;
111  }
112  __dlg_close(param);
113  return false;
114}
115
116function onUpload() {
117  var required = {
118    "file": i18n("Please select a file to upload.")
119  };
120  for (var i in required) {
121    var el = document.getElementById(i);
122    if (!el.value) {
123      alert(required[i]);
124      el.focus();
125      return false;
126    }
127  }
128  submit();
129  return false;
130}
131
132function onCancel() {
133  __dlg_close(null);
134  return false;
135}
136
137function onPreview() {
138  var f_url = document.getElementById("f_url");
139  var url = f_url.value;
140  if (!url) {
141    alert(i18n("You must enter the URL"));
142    f_url.focus();
143    return false;
144  }
145  if (document.all) {
146    window.ipreview.location.replace('viewpicture.html?'+url);
147  } else {
148    window.ipreview.location.replace(url);
149  }
150  return false;
151}
152
153var img = new Image();
154function imgWait() {
155  waiting = window.setInterval("imgIsLoaded()", 1000)
156}
157function imgIsLoaded() {
158  if(img.width > 0) {
159    window.clearInterval(waiting)
160    document.getElementById("f_width").value = img.width;
161    document.getElementById("f_height").value = img.height;
162  }
163}
164
165function CopyToURL(imgName) {
166  document.getElementById("f_url").value = imgName;
167  onPreview();
168  img.src = imgName;
169  img.onLoad = imgWait()
170}
171
172function openFile() {
173  window.open(document.getElementById("f_url").value,'','');
174}
175</script>
176
177</head>
178
179<body class="dialog" onload="Init()">
180
181<div class="title">Insert Image</div>
182<table border="0" width="100%" style="padding: 0px; margin: 0px">
183  <tbody>
184  <tr>
185    <td>Images on the Server:<br>
186    <select value="" style="width:200" size="10" onClick="CopyToURL(this[this.selectedIndex].value);">
187<?php
188  $d = @dir($LocalPicturePath);
189  while (false !== ($entry = $d->read())) { //not a dot file or directory
190    if(substr($entry,0,1) != '.') {
191      echo '<OPTION value="' . $PicturePath.$entry. '">' . $entry . '(' . formatSize(filesize($LocalPicturePath.'\\'.$entry)) .')</OPTION>';
192    }
193  }
194  $d->close();
195?>
196    </select>
197<?php
198  if ($getRequest == true) {
199    echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'?picturepath='.$_REQUEST['picturepath'].'" enctype="multipart/form-data">';
200  } else {
201    echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data">';
202  }
203?>
204        <input type="file" name="file" id="file" size="30"><br>
205        <button type="submit" name="ok" onclick="return onUpload();">Upload file</button><br>
206        <span><?php echo $message ?></span>
207      </form>
208
209    </td>
210    <td valign="center" width="200" height="230">
211    <span>Image Preview:</span>
212    <a href="#" onClick="javascript:openFile();"title=" Open file in new window"><img src="img/btn_open.gif"  width="18" height="18" border="0" title="Open file in new window" /></a><br />
213    <iframe name="ipreview" id="ipreview" frameborder="0" style="border : 1px solid gray;" height="200" width="200" src=""></iframe>
214    </td>
215  </tr>
216  </tbody>
217</table>
218
219<form action="" method="get">
220<table border="0" width="100%" style="padding: 0px; margin: 0px">
221  <tbody>
222
223  <tr>
224    <td style="width: 7em; text-align: right">Image URL:</td>
225    <td><input type="text" name="url" id="f_url" style="width:75%"
226      title="Enter the image URL here" />
227      <button name="preview" onclick="return onPreview();"
228      title="Preview the image in a new window">Preview</button>
229    </td>
230  </tr>
231  <tr>
232    <td style="width: 7em; text-align: right">Alternate text:</td>
233    <td><input type="text" name="alt" id="f_alt" style="width:100%"
234      title="For browsers that don't support images" /></td>
235  </tr>
236
237  </tbody>
238</table>
239
240<p />
241
242<fieldset style="float: left; margin-left: 5px;">
243<legend>Layout</legend>
244
245<div class="space"></div>
246
247<div class="fl" style="width: 6em;">Alignment:</div>
248<select size="1" name="align" id="f_align"
249  title="Positioning of this image">
250  <option value=""                             >Not set</option>
251  <option value="left"                         >Left</option>
252  <option value="right"                        >Right</option>
253  <option value="texttop"                      >Texttop</option>
254  <option value="absmiddle"                    >Absmiddle</option>
255  <option value="baseline" selected="1"        >Baseline</option>
256  <option value="absbottom"                    >Absbottom</option>
257  <option value="bottom"                       >Bottom</option>
258  <option value="middle"                       >Middle</option>
259  <option value="top"                          >Top</option>
260</select>
261
262<p />
263
264<div class="fl" style="width: 6em;">Border thickness:</div>
265<input type="text" name="border" id="f_border" size="5"
266title="Leave empty for no border" />
267
268<div class="space"></div>
269
270</fieldset>
271
272<fieldset style="float: left; margin-left: 5px;">
273<legend>Size</legend>
274
275<div class="space"></div>
276
277<div class="fl" style="width: 5em;">Width:</div>
278<input type="text" name="width" id="f_width" size="5" title="Leave empty for not defined" />
279<p />
280
281<div class="fl" style="width: 5em;">Height:</div>
282<input type="text" name="height" id="f_height" size="5" title="Leave empty for not defined" />
283<div class="space"></div>
284
285</fieldset>
286
287<fieldset style="float:right; margin-right: 5px;">
288<legend>Spacing</legend>
289
290<div class="space"></div>
291
292<div class="fr" style="width: 5em;">Horizontal:</div>
293<input type="text" name="horiz" id="f_horiz" size="5"
294title="Horizontal padding" />
295
296<p />
297
298<div class="fr" style="width: 5em;">Vertical:</div>
299<input type="text" name="vert" id="f_vert" size="5"
300title="Vertical padding" />
301
302<div class="space"></div>
303
304</fieldset>
305<br clear="all" />
306
307<div id="buttons">
308  <button type="submit" name="ok" onclick="return onOK();">OK</button>
309  <button type="button" name="cancel" onclick="return onCancel();">Cancel</button>
310</div>
311</form>
312</body>
313</html>
Note: See TracBrowser for help on using the repository browser.