API

2i API allows for an easy integration of 2i - image hosting image hosting functionality into existing web sites, blogs, discussion forums etc. Using the API you may add a virtual button to your form, which makes it easy to invoke file selection dialog and upload the selected image to 2i - image hosting. Upon successful upload the API inserts link with a thumbnail to the form element of choice, thus inserting an image from the user's point of view is a two-click process, as simple as opening a file selection dialog and clicking "OK" as soon as the desired image file is selected.

Deployment example

XHTML example (2i - image hosting api integration code set in bold)

<form method="post" action="">
  <input type="text" name="title" value="" />
  <textarea name="body" id="target_2i_textarea"></textarea>
  <div class="button2i" id="button_2i_div">insert image</div>
  <input type="submit" value="submit" />
</form>

<script type="text/javascript" src="http://2i.sk/2i/j/api2i.js"></script>
<script type="text/javascript">
/* <![CDATA[ */
  var o2i = new upload2i('button_2i_div', 'target_2i_textarea', 'cs', 'BBCode+thumb');
  o2i.cfgBackURLPath = '2i.html';
/* ]]> */
</script>

How to allow the use of 2i - image hosting in your web form

Custom settings

Object reference (upload2i prototype)

object upload2i(string button, string target, string language in (en|cz|cs), string codeType in (id, bbcode, html, xhtml, custom) [, bool crop = false [, bool large = false]])
button
ID of DIV element to present the upload button
target
ID of TEXTAREA element to receive the link+thumbnail code (appended to the end of this element's value)
language
language used for error messages; default charset is UTF-8. As of this version of API, only en, cs and cz values are recognized. If your site uses a different language or charset, you may override default message texts with custom ones, see configuration
codeType
return code format
  • id - ID only
  • BBCode - BBCode style link
  • BBCode+thumb - BBCode style link with inline thumbnail image
  • HTML - HTML link, opening in a new window
  • HTML+thumb - HTML link, opening in a new window, with inline thumbnail
  • XHTML - valid XHTML link, opening in a new window via JavaScript (graceful degradation)
  • XHTML+thumb - valid XHTML link with thumbnail
  • custom -use custom template string cfgCustomCode (see configuration reference below) to format the code
crop
crop thumbnail to square
large
larger thumbnail (100px vs. 75px)

Configuration

cfgBackURLPath
(string) relative path to an empty file (a proxy for returned ID), if not set, current URL will be used. Recommended: set to an empty but existing filename somewhere in your document root.
cfgCodeType
(string) target code format, see codeType above
cfgCrop
(bool) true = square cropped thumbnail, false = keep original ratio
cfgCustomCode
(string) custom template string for the target link and thumbnail; following macros may be used:
  • #thumb# - URL of thumbnail image
  • #show# - URL of (link to) full image
  • #text# - link text (by selected language)
cfgLarge
(bool) larger thumbnail, true = 100px, false = 75px
cfgResetFormAfterUpload
(bool) reset upload form after each upload; prevents several issues so defaults to true. Set to false only if you plan to fetch return code via JavaScript yourself.
tBadType
(string) error message: unknown/unsupported file type uploaded
tLinkToImage
(string) text for linking to image if no thumbnail is used, maps to #text# macro in cfgCustomCode
tUploadError
(string) error message: image upload failed

Upload in progress indicator, use of CSS

You may style the DIV used as file upload "button" via CSS but you cannot put any form controls inside it (e.g. INPUT type=BUTTON) as these controls would interfere with your main form controls and above all they would prevent the file selection dialog from opening (blocking the click event propagation).

During upload the DIV is assigned a "uploading2i" classname, which is again removed once the upload is over (whether the result is a success or a failure). This serves to indicate "upload in progress" state and allows for custom styling, e.g. as follows.

(X)HTML example (part visible while uploading set in bold)

<div class="button2i" id="button_2i_div">Insert image<span>Uploading...</span></div>

CSS example

.button2i {
  border: outset 2px #fff;
  font-size: 0.85em;
  height: 20px;
  text-align: center;
  width: 100px;
}
.button2i span {
  background: #efe;
  display: none;
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  top: 0;
}
.uploading2i {
  background: #f00;
}
.uploading2i span {
  display: block;
}