Colin Verot is working on a very fine image upload and image transformation php class:php class upload

Although the current version displays a miserable “0.19” , the class is already perfectly usable in a production environment.

The main advantage i find in using it (besides its ease of use) is that, from one unique upload, you can generate as many images as necessary. For instance, I’ve used it successfully inside of a CMS where from one image, the process would be:

1. upload

2. generate thumbnail

3. if original dimension width> 800 pixels, save as “highres_”. filename

4. if original dimension width > 400 pixels, redimension it to 400 pixels

5. save a copy at 400 pixels and call it “preview_”.filename

the coding could not be easier. i’ve pasted my implementation here so you can have a feel of how it works, feel free to comment if you have questions, yet full documentation is available at colin Verot‘s website:

[PHP]
// 0. UPLOAD
$image = new Upload($_FILES[‘highres_path’]);
$uploadsuccessful = 0;
if ($image->uploaded) {
// 1. SAVE AS HIGHRES
$hr_newname = $highres_prefix . $_POST[‘img_base_name’];
$image->file_new_name_body = $hr_newname;
$image->image_convert = jpg;
$image->jpeg_quality = 100;
$image->Process($imgfolder_local);
if ($image->processed) {
$hr_newname = $image->file_dst_name_body;
$hr_newname .= ‘.’ . $image->file_dst_name_ext;
// store raw image info
$imagesize = getimagesize($image->file_src_pathname);
$imagewidth = $imagesize[0];
} else {
// store error message and redirect with error display
$_SESSION[‘error’][] = “Highres creation: ERROR could not create ‘$hr_newname’ :” . $image->error;
header(‘location: index.php?action=add_pub_plays&step=step2’);
exit;
}
// 2. SAVE AS WEB
$newname = $_POST[‘img_base_name’];
$image->file_new_name_body = $newname;
$image->image_convert = jpg;
if ($imagewidth > $web_width) {
$image->image_resize = true;
$image->image_x = $web_width;
$image->image_ratio_y = true;
}
$image->Process($imgfolder_local);
if ($image->processed) {
$newname = $image->file_dst_name_body;
$newname .= ‘.’ . $image->file_dst_name_ext ;
} else {
// store error message and redirect with error display
$_SESSION[‘error’][] = “Web version creation error : ” . $image->error;
header(‘location: index.php?action=add_pub_plays&step=step2’);
exit;
}
// 3. SAVE THUMBNAIL
$th_newname = $thumb_prefix . $_POST[‘img_base_name’];
$image->file_new_name_body = $th_newname;
$image->image_resize = true;
$image->image_x = $thumb_width;
$image->image_ratio_y = true;
$image->image_convert = jpg;
$image->Process($imgfolder_local);
if ($image->processed) {
$th_newname = $image->file_dst_name_body;
$th_newname .= ‘.’ . $image->file_dst_name_ext;
} else {
$_SESSION[‘error’][] = “Thumbnail creation: resize error : ” . $image->error;
}
// 4. STORE IN DB
$image->Clean();
// continue to mysql insert query ….
[/PHP]

isn’t this easy or what ?