Image Uploader PHP is a collection of classes which are meant to deploy Image Uploader and handle uploaded data easier. Using this collection, PHP developers can work with Image Uploader in a usual way as with a common PHP object. Image Uploader PHP provides several advantages over a standard way of deployment described in the Quick Start with Image Uploader JavaScript article:
To add Image Uploader to a web page you just need to perform the following steps:
Copy the /ImageUploaderPHP
folder to an application.
It can be found in Image Uploader installation folder (usually this is
C:\Program Files\Aurigma\Image Uploader 7.0.37\PHPClasses\ImageUploaderPHP
).
Link the Uploader.class.php
file with the page which will host Image Uploader.
<?php require_once "ImageUploaderPHP/Uploader.class.php"; ?>
Create a new instance of the Uploader class. Then configure Image Uploader in the way described in the following sections. After you have initialized necessary parameters call the render() method which generates Image Uploader embedding code and writes it into the page.
<?php $uploader = new Uploader("Uploader1"); //configure Image Uploader $uploader->render(); ?>
Call the render() method in the position of the page you want to insert Image Uploader into.
Here we discuss the use Image Uploader PHP by the example of a simple file catalog. Suppose, the catalog requires to upload files of various types and displays links to download these files. To implement this we should configure an Uploader object and save uploaded files on a server. The sections below describe these steps in detail.
To configure Image Uploader parameters you should set corresponding properties of the Uploader object. For example, the ID property specifies a unique identifier of the control.
Here is the source code of the main page of our catalog (index.php
).
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Image Uploader PHP Sample</title> </head> <body> <?php require_once "ImageUploaderPHP/Uploader.class.php"; // create Uploader object and specify its ID and size $uploader = new Uploader("Uploader1"); $uploader->setWidth("650px"); $uploader->setHeight("480px"); // specify a license key $uploader->setLicenseKey("XXXXX-XXXXX-XXXXX-XXXXX-XXXXX-XXXXXX;YYYYY-YYYYY-YYYYY-YYYYY-YYYYY-YYYYYY"); // configure upload settings $uploader->getUploadSettings()->setActionUrl("upload.php"); $uploader->getUploadSettings()->setRedirectUrl("catalog.php"); //configure uploaded files $converters = &$uploader->getConverters(); // one converter for uploading source file $converter = new Converter(); $converter->setMode("*.*=SourceFile"); $converters[] = $converter; // render Uploader $uploader->render(); ?> </body> </html>
On this page we host Image Uploader and configure it, namely, specify its ID and size, server upload script and the page to which the user will be redirected when the upload successfully completes. To configure Image Uploader to send original files we used the Uploader.Converters property which returns a set of Converter instances. Each instance specifies what will be uploaded (original file as is, thumbnail created from original image file, icon associated with original file, or original file compressed into ZIP archive) for each of the user-selected files. Since we need to upload original files only, we set one converter with the SourceFile mode. Read more about converters in the Configuring Files to be Uploaded topic.
One more significant property is Uploader.LicenseKey which specifies trial or full
license key. If license key is not set Image Uploader will not send files (the exception is usage
of uploader on localhost
domain). See the Evaluating and Registering Image Uploader topic for details.
All the parameters should be specified before the call to the render() method; otherwise, parameters added after it will be ignored.
The uploaded data can be saved using the UploadHandler.saveFiles(string) method which
saves all the received files to the specified folder. This method returns an array of full filenames of saved files. According to
the catalog requirements, we store files set to upload by a user in /Catalog
folder.
Here is the source code of the upload script (upload.php) used in our catalog.
<?php require_once "ImageUploaderPHP/UploadHandler.class.php"; $uploadHandler = new UploadHandler(); $uploadHandler->saveFiles("Catalog/"); ?>
For versions of PHP earlier than 6.0 disable the magic_quotes_gpc
directive in your
php.ini
file.
On the configuring step we specified the UploadSettings.RedirectUrl property
to the "catalog.php"
value. It means that Image Uploader will
redirect users to this page after the upload is successfully completed. It would be convenient to list links to the uploaded
files here. To implement this functionality we iterate through all the files stored in the /Catalog
folder and display a link to each of them. See the source code below.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Catalog</title> </head> <body> <?php $catalogPath = "Catalog/"; //Open catalog directory if (is_dir($catalogPath)) { if ($directory = opendir($catalogPath)) { //Iterate through all files while (($file = readdir($directory))){ //Process files only if (!is_dir($file)){ echo "<a href=\"$catalogPath$file\" target=\"_blank\">\n"; echo " $file\n"; echo "</a><br />\n"; } } closedir($directory); } } ?> </body> </html>
You may run the file catalog application considered here on your server. To do it, just paste source code
to index.php
, upload.php
, and
catalog.php
files to your server using the following folder structure:
/Catalog /ImageUploaderPHP catalog.php index.php upload.php
Make sure that the /Catalog
folder has
enough permissions to save files to.
One more example of Image Uploader usage is an image gallery. Suppose, it requires a user to upload original images along with their downsized copies. This task can be divided on the same phases as the previous one.
The configuration of the Uploader object used for our image gallery is almost the same as for the file catalog described above.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Image Uploader PHP Sample</title> </head> <body> <?php require_once "ImageUploaderPHP/Uploader.class.php"; // create Uploader object and specify its ID and size $uploader = new Uploader("Uploader1"); $uploader->setWidth("650px"); $uploader->setHeight("480px"); // specify a license key $uploader->setLicenseKey("XXXXX-XXXXX-XXXXX-XXXXX-XXXXX-XXXXXX;YYYYY-YYYYY-YYYYY-YYYYY-YYYYY-YYYYYY"); // configure upload settings $uploader->getUploadSettings()->setActionUrl("upload.php"); $uploader->getUploadSettings()->setRedirectUrl("gallery.php"); //configure uploaded files $converters = &$uploader->getConverters(); // one converter for uploading source file $converter = new Converter(); $converter->setMode("*.*=SourceFile"); $converters[] = $converter; // and second - for uploading thumbnail $converter = new Converter(); $converter->setMode("*.*=Thumbnail"); // set thumbnail size $converter->setThumbnailWidth(120); $converter->setThumbnailHeight(120); $converter->setThumbnailFitMode("Fit"); $converters[] = $converter; // render Uploader $uploader->render(); ?> </body> </html>
The difference is that we set an additional converter to specify a thumbnail which will be created for each image file and will be fitted to 120x120 pixels. Read more about converters in the Configuring Files to be Uploaded topic.
Here we use the UploadHandler.saveFiles(string) method to save uploaded
files to the /Gallery
folder, as it is demonstrated in upload.php
source code below.
<?php require_once "ImageUploaderPHP/UploadHandler.class.php"; $uploadHandler = new UploadHandler(); $uploadHandler->saveFiles("Gallery/"); ?>
Pay attention to the fact that the names of files stored in the destination folder are not equal to the original filenames.
Each name has a suffix corresponding to the converter used to create this file(except for the SourceFile
one). In our example we set two converters,
that is why there are two files stored for each user-selected file:
name.ext
, the original image specified with the first convertername.ext_Thumbnail1.jpg
, a downsized copy of the original image configured with the
second converterSee the ConvertedFile.Name property description for a full list of possible suffixes.
On the configuring step we specified the UploadSettings.RedirectUrl property to the
"gallery.php"
value. It means that Image Uploader will redirect users
to this page after the upload is successfully completed. It would be convenient to display uploaded images here. To implement this
functionality we iterate through all the files stored in the destination folder and display each thumbnail (file with
_Thumbnail1
suffix) as a link to its original image (no suffix).
See comments in the source code below.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Gallery</title> </head> <body> <?php $galleryPath = "Gallery/"; //Open catalog directory if (is_dir($galleryPath)) { if ($directory = opendir($galleryPath)) { //Iterate through all files while (($file = readdir($directory))){ //Process files only if (!is_dir($file)){ $pos = strpos($file, "_Thumbnail1"); //The file is not a thumbnail, so it is a source file if ($pos == false){ //Construct name of the corresponded thumbnail $thumbnailName = $file."_Thumbnail1.jpg"; //Check if this thumbnail exists if (file_exists("$galleryPath$thumbnailName")){ echo "<a href=\"$galleryPath$file\" target=\"_blank\">\n"; echo " <img src=\"$galleryPath$thumbnailName\" />\n"; echo "</a> \n"; } } } } closedir($directory); } } ?> </body> </html>
You may run the image gallery application on your server. To do this, just paste source code
to index.php
, upload.php
, and
gallery.php
files to your server using the following folder structure:
/Gallery /ImageUploaderPHP gallery.php index.php upload.php
Make sure that /Gallery
folder has
enough permissions to save files to.