14/Sep/2009 - Darren Dignam

Wyzz Image Uploading

Using imageshack.us API to add image-upload feature to wyzz

I created this page as the wyzz forum didnt like the ammout of code and/or links in my post!!!

Wyzz is a WYSIWYG editor implemented as javascript, its very lightweight, and FREE, check it out! The rest of this page is how I wanted a post to appear in the forum....


Hi, I have just started using wyzz. I like it a lot, its lightweight, and you can tweak it to how you like! I have been trying for a few days to use imageshack API for image uploading, and now I have it working, and working in wyzz too, I thought I would share my findings here with the community!
The principle behind my technique is based on iFrame AJAX (for that seamless user experience ;), and I have explained in detail on my blog the imageshack side of things:
http://www.blocsoft.com/blog/imageshack.asp
For my approach, you need a form, and a hidden iFrame (called AXframe, style="visibility:hidden"). I create the form in JS in my modified insertImage function:

function insertImage(n) {
global_n = n;
document.getElementById("imageform").innerHTML = "<form onsubmit=\"this.style.visibility='hidden';\" action=\"http://imageshack.us/redirect_api.php\" method=\"post\" target=\"AXframe\" enctype=\"multipart/form-data\"><span>Insert Image:</span> <input type=\"file\" name=\"media\"/><input type=\"hidden\" name=\"key\" value=\"YOUR_API_KEY\"><input type=\"hidden\" name=\"error_url\" value=\"http://example.com/imgerror.asp\"><input type=\"hidden\" name=\"success_url\" value=\"http://example.com/success.asp?1=%y&2=%u&3=%s&4=%b&5=%i\"> <input type=\"submit\" value=\"Upload Image\"><a href=\"#\" onclick=\"document.getElementById('imageform').style.display = 'none';\">Cancel</a><br><span style=\"font-size:60%;text-align:center;display:block;margin-bottom:-5px\">Images hosted by: <a target='_blank' href='http://www.imageshack.us'>imageshack.us</a></span></form>";
document.getElementById("imageform").style.display = 'block';
}

The form created looks like this:

<form onsubmit="this.style.visibility='hidden';" action="http://imageshack.us/redirect_api.php" method="post" target="AXframe" enctype="multipart/form-data">
<span>Insert Image:</span> <input type="file" name="media"/>
<input type="hidden" name="key" value="YOUR_API_KEY">
<input type="hidden" name="error_url" value="http://example.com/imgerror.asp">
<input type="hidden" name="success_url" value="http://example.com/success.asp?1=%y&2=%u&3=%s&4=%b&5=%i">
 <input type="submit" value="Upload Image">
<a href="#" onclick="document.getElementById('imageform').style.display = 'none';">Cancel</a>
<br><span style="font-size:60%;text-align:center;display:block;margin-bottom:-5px">Images hosted by: <a target='_blank' href='http://www.imageshack.us'>imageshack.us</a></span>
</form>

It looks nasty I know - sorry - this is still WIP! I had to use a global var global_n to remember which wyzz we are dealing with (I have only 1 wyzz editor on screen at any given time anyway, so i dont see a problem with this, if anyone can suggest a better way, im all ears!)
The form will hide itself when you submit the image, and the hidden iFrame performs the upload, on completion either an image URL is inserted into the wyzz window, or an error alert is generated. read my blog above for the details of how this works ;)
The imageshack API redirects to a page of your choosing on successful upload, and this page has javascript in it, that using global_n inserts the image, this is the code in my sucess.asp file (this could be done with PHP just as easily ;)

Success.asp

<%
      str1 = Request("1")
      str2 = Request("2")
      str3 = Request("3")
      str4 = Request("4")
      str5 = Request("5")  
%>
<script>
if (parent.browserName == "Microsoft Internet Explorer"||browserName == "Opera") {
	parent.document.getElementById("wysiwyg" + parent.global_n).contentWindow.focus();
	parent.document.getElementById("wysiwyg" + parent.global_n).contentWindow.document.execCommand("insertImage", false, "http://img<%=str3 %>.imageshack.us/img<%=str3 %>/<%=str4 %>/<%=str5 %>")
} else {
	parent.insertHTML('<img src="http://img<%=str3 %>.imageshack.us/img<%=str3 %>/<%=str4 %>/<%=str5 %>">', parent.global_n);
}
parent.document.getElementById('imageform').style.display = 'none';
</script>

FF and IE do things differently, hence the check. This code is a stripped down verison of what I am working on, so please feel free to post any errors I may have made. I have got this work!! And in due course I will link to an example. Please make any suggestions too!!! Darren




Comments:

problem
Dragos - 9/15/2009 9:31:41 PM
i have like two hours to try the codes and i',m unable to get the point.
Can you make and archive with wizz included just to ready to use it with wizz . Thank you

P.S Success error :

Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.5.30729; OfficeLiveConnector.1.3; OfficeLivePatch.0.0; Media Center PC 5.0; .NET CLR 3.0.30729)
Timestamp: Wed, 16 Sep 2009 14:27:00 UTC


Message: 'browserName' is undefined
Line: 9
Char: 1
Code: 0
URI: http://www.kwrentals.co.uk/wyzz/success.asp

problem 2
Dragos - 9/15/2009 9:39:20 PM
I'm prety sure copy/paste into wyzz.js not working correct too so many thanks if u can make an archive for php also will be awsome.
Hi Dragos
Darren - 9/18/2009 9:34:36 PM
Sorry to hear you have not been able to get this technique to work.
I have just been looking at the link you supplied, and can offer some tips:
In your wyzz.js file within the insert image function you have copied my code to create a web-form: document.getElementById("imageform").innerHTML = ...... but in your actual page, there is no element called imageform. Inculed an empty div on the index page somewhere and then the form will appear:
<div id="imageform"></div>

Also, your success and error pages are not working correctly. The code snippets I provided were for ASP, and it looks like your on an Apache server, so you need to rewrite them in PHP for your project. I will get round to putting a working example on these pages but for now you might read: http://www.w3schools.com/PHP/php_get.asp as it will get you started (the asp is real simple: 1. Get some values from the querystring, 2. Build a URI string from those values.... )
w3schools.com is your friend ;)
PS
browserName is a WYZZ variable, in my code "parent.browserName" assumes that success.asp is inside an iFrame, inside my wyzz page...
Problem 2 is a little more difficult to solve, as wyzz editor is dependent on the browser, and how it treats "contentEditable". The wyzz forums go into more depth on the subject.

 (*required)
 (*private)
 (*required)
 
© 2009 BlocSoft.com All Rights Reserved. Contact