Java Image Upload With Client Side Resizing

Saturday, March 29th, 2008

JumpLoader ScreenshotI was recently looking for a way to upload large images for the web and automatically create different sizes (small, medium and large.) The only viable way to do it is with Java because it can all be done client side. If you do the work server side, you’ll be uploading a large file then using a lot of processor power to resize it.

I’ve tried a few and the one that came out on top was the JumpLoader it’s good looking, functional and customisable. When uploading several versions of an image at different sizes though it will zip them up (for transfer purposes,) you’ll then have to unzip them - here’s the PHP to do that.

PHP to extract and rename images from JumpLoader

Effectively it will extract the resized zipped images to the same directory and prefix each with the original title then the instance name e.g. ‘original-title_small.jpg. Check the JumpLoader forums for info on resizing image instances.

<?php
//———————————————-
// upload file handler script
//———————————————-//
// specify file parameter name
$file_param_name = ‘file’;//
// retrieve uploaded file name
$file_name = $_FILES[ $file_param_name ][ 'name' ];//
// retrieve uploaded file path (temporary stored by php engine)
$source_file_path = $_FILES[ $file_param_name ][ 'tmp_name' ];

//
// construct target file path (desired location of uploaded file) -
// here we put to the web server document root (i.e. ‘/home/wwwroot’)
// using user supplied file name
$target_file_path = $file_name;
$target_file_path = rtrim($target_file_path,’jpg’);
$target_file_path = $target_file_path.’zip’;

//
// move uploaded file
echo “Moving file ” . $source_file_path . ” > ” . $target_file_path . “: “;
if( move_uploaded_file( $source_file_path, $target_file_path ) ) {
echo “success”;
} else{
echo “failure”;
}

// set a temporary location for unzipped files
$unzipDir = ‘unzipped’;
// remove the file extension from the filename (for prefixing)
$file_name = substr($file_name,0,-4);
// find the current directory (absolute server val)
$currentDir = dirname(__FILE__);
// create the zip object
$zip = new ZipArchive;
if ($zip->open($target_file_path) === TRUE) {
// extract the individual items to the temp folder
$zip->extractTo($unzipDir.’/');
$zip->close();
// open the temp folder
$handle = opendir($unzipDir);
// loop every file that isn’t a shortcut
while (FALSE !== ($file = readdir($handle))) {
if (’.’ !== $file && ‘..’ !== $file) {
// source file is in the unzipped dir
$source = $currentDir.’\\’.$unzipDir.’\\’.$file;
// target file is prefixed with the original filename and moved up one
// NOTE *nix users might need to fiddle with back slashes here.
$target = $currentDir.’\\’.$file_name.’_’.$file;
rename ($source,$target);
unlink ($target_file_path);
}
}
closedir($handle);
}

//
// below is trace of variables
?>
<html>
<body>
<h1>GET content</h1>
<pre><?print_r( $_GET );?></pre>
<h1>POST content</h1>
<pre><?print_r( $_POST );?></pre>
<h1>FILES content</h1>
<pre><?print_r( $_FILES );?></pre>
</body>
</html>


Comment On This Post