Jan 202013
Article Javascript

This post shows how to use the plupload javascript library to post images to a web server.

The main advantage of plupload is its ability to perform client-side processing of the image, reducing the volume of data to be sent to the server. This is specially convenient for web sites targeted to users of mobile devices. It allows, for instance, uploading to the server pictures taken with the built-in camera. An image of this kind can be several MBytes in size, with a resolution too big to be displayed on a web page. With plupload, it is possible to change the scale, and optionally the quality of the image, to reduce the size by up to 90%.

Besides, plupload uses whatever image processing mechanism is available at the client to perform this processing of the image: html5, flash, gears, silverlight or browserplus, making it compatible with almost any kind of client.

For these reasons, plupload has been adopted as the upload library chosen by many CMS platforms, such as WordPress

 plupload installation

To perform the installation, go to the plupload web site, and download the installation package. The file we downloaded is named plupload_1_5_4.zip ad is 240 KB in size. Next, the file is uncompressed under the root of the web server, creating a “plupload” directory. There is a “js” subdirectory below where the library files are located, together with other directories with the documentation and examples.

Image upload form

Among the available examples, in this post we will analyse the “Custom upload” located in “/plupload/examples/custom.html”. The content of the HTML file is:

1. Loading of libraries

2.Javascript code

We can see that plupload creates a “uploader” object. In the object initialization, several parameters are set. One of these parameters specifies the requested dimensions and quality of the image after processing:

There is another parameter “max_file_size” that limits the maximum size of the image to be uploaded.

Besides, the “url” parameter specifies the destination url where data are to be posted. The value of the “url” parameter in this example is “upload.php” (the absolute url is then “/plupload/examples/upload.php”. This “upload.php” script is also included in the plupload installation package, and must be edited to set the value of the “$targetDir” variable to point to the directory we want to use to store the images received.

We edited the variable to set it to:

using this value, images will be stored under “plupload/examples/uploads”. We must ensure that the userid of the web server process is allowed write access to this directory.

3. HTML <body> with links for selecting and sending the files

Sending the images

Pointing the browser to the url “/plupload/examples/custom.html”, the page displayed includes a link “[Select files]” and another link “[Upload files]”:


We can see also that the “runtime” available at the client to perform the image processing is also displayed (“html5” in the example screenshot)

Clicking on “Select Files”, a popup window is displayed to choose the files to be uploaded:


Finally, clicking on “Upload files”, the actual upload takes place:


Then we inspect the upload directory “pluploads/examples/uploads” to have a look at the result:

We can see that plupload has applied the requested scaling of the images, and therefore the size of the files uploaded to the server is smaller than the size of the original images.

 Posted by at 1:13 pm

 Leave a Reply