Author Topic: Image Upload Preview  (Read 11276 times)

0 Members and 1 Guest are viewing this topic.

Offline himu

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
    • Point Of View
Image Upload Preview
« on: February 23, 2006, 05:43:52 AM »
Here is a little java trick i found at V@no's gallery .. which i did not find in the forum..
later managed to incorporate it with my site...  :wink:

When you choose a file to upload you can see a preview of the file before submit.

-----------------------------------------------
Files to edit: only two
template/{your_template}/header.html
template/{your_template}/member_uploadform.html

-----------------------------------------------

Step 1
Open template/{your_template}/header.html

Insert above</head> :
Code: [Select]
<script type="text/javascript">
<!-- Begin
/***** CUSTOMIZE THESE VARIABLES *****/
  // width to resize large images to
var maxWidth=150;
  // height to resize large images to
var maxHeight=150;
  // valid file types
var fileTypes=["bmp","gif","png","jpg","jpeg"];
  // the id of the preview image tag
var outImage="previewField";
  // what to display when the image is not valid
var defaultPic="spacer.gif";

/***** DO NOT EDIT BELOW *****/

function preview(what){
  var source=what.value;
  var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
  for (var i=0; i<fileTypes.length; i++) if (fileTypes[i]==ext) break;
  globalPic=new Image();
  if (i<fileTypes.length) globalPic.src=source;
  else {
    globalPic.src=defaultPic;
    alert("THAT IS NOT A VALID IMAGE\nPlease load an image with an extention of one of the following:\n\n"+fileTypes.join(", "));
  }
  setTimeout("applyChanges()",200);
}
var globalPic;
function applyChanges(){
  var field=document.getElementById(outImage);
  var x=parseInt(globalPic.width);
  var y=parseInt(globalPic.height);
  if (x>maxWidth) {
    y*=maxWidth/x;
    x=maxWidth;
  }
  if (y>maxHeight) {
    x*=maxHeight/y;
    y=maxHeight;
  }
  field.style.display=(x<1 || y<1)?"none":"";
  field.src=globalPic.src;
  field.width=x;
  field.height=y;
}
// End -->
</script>

Step 2
Open template/{your_template}/member_uploadform.html

Find:
Code: [Select]
  <span class="smalltext">
  {lang_max_filesize}<b>{max_media_filsize}</b><br />
  {lang_max_imagewidth}<b>{max_media_imagewidth}</b><br />
  {lang_max_imageheight}<b>{max_media_imageheight}</b><br />

Insert below:
Code: [Select]
  <img alt="Image preview here" id="previewField" src="{template_url}/images/spacer.gif"><br />
Step 2.1
Find:
Code: [Select]
  <input type="file" name="media_file" class="input" /><br />
Replace with:
Code: [Select]
  <input type="file" name="media_file" onchange="preview(this)" class="input" /><br />

You can customize some of the variables at Step 1
between /***** CUSTOMIZE THESE VARIABLES *****/ and /***** DO NOT EDIT BELOW *****/

That's it. Enjoy...  :D

This is my first topic post.. hope i did not make any mistakes..

Lastly Thanks V@no.

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: Image Upload Preview
« Reply #1 on: February 23, 2006, 06:53:12 AM »
Note: this only works in IE browser.
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline himu

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
    • Point Of View
Re: Image Upload Preview
« Reply #2 on: February 23, 2006, 09:22:51 AM »
Note: this only works in IE browser.

Ooopss!..  sorry forgot to mention that.. :oops:
Thans!

Offline Lucifix

  • Hero Member
  • *****
  • Posts: 710
    • View Profile
    • http://www.slo-foto.net
Re: Image Upload Preview
« Reply #3 on: September 01, 2008, 01:50:15 PM »
Too bad that this mod doesn't work in Firefox. Looks like FF doesn't allow to show local images. You can enable this in about:config but it will work only on your computer.

Anyone has any other idea how to allow users to preview images before they publish them?