verot.net – class.upload.php

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:
  1. // 0. UPLOAD
  2. $image = new Upload($_FILES['highres_path']);
  3. $uploadsuccessful = 0;
  4. if ($image->uploaded) {
  5. // 1. SAVE AS HIGHRES
  6. $hr_newname = $highres_prefix . $_POST['img_base_name'];
  7. $image->file_new_name_body = $hr_newname;
  8. $image->image_convert = jpg;
  9. $image->jpeg_quality = 100;
  10. $image->Process($imgfolder_local);
  11. if ($image->processed) {
  12. $hr_newname = $image->file_dst_name_body;
  13. $hr_newname .= '.' . $image->file_dst_name_ext;
  14. // store raw image info
  15. $imagesize = getimagesize($image->file_src_pathname);
  16. $imagewidth = $imagesize[0];
  17. } else {
  18. // store error message and redirect with error display
  19. $_SESSION['error'][] = "Highres creation: ERROR could not create '$hr_newname' :" . $image->error;
  20. header('location: index.php?action=add_pub_plays&step=step2');
  21. }
  22. // 2. SAVE AS WEB
  23. $newname = $_POST['img_base_name'];
  24. $image->file_new_name_body = $newname;
  25. $image->image_convert = jpg;
  26. if ($imagewidth> $web_width) {
  27. $image->image_resize = true;
  28. $image->image_x = $web_width;
  29. $image->image_ratio_y = true;
  30. }
  31. $image->Process($imgfolder_local);
  32. if ($image->processed) {
  33. $newname = $image->file_dst_name_body;
  34. $newname .= '.' . $image->file_dst_name_ext ;
  35. } else {
  36. // store error message and redirect with error display
  37. $_SESSION['error'][] = "Web version creation error : " . $image->error;
  38. header('location: index.php?action=add_pub_plays&step=step2');
  39. }
  40. // 3. SAVE THUMBNAIL
  41. $th_newname = $thumb_prefix . $_POST['img_base_name'];
  42. $image->file_new_name_body = $th_newname;
  43. $image->image_resize = true;
  44. $image->image_x = $thumb_width;
  45. $image->image_ratio_y = true;
  46. $image->image_convert = jpg;
  47. $image->Process($imgfolder_local);
  48. if ($image->processed) {
  49. $th_newname = $image->file_dst_name_body;
  50. $th_newname .= '.' . $image->file_dst_name_ext;
  51. } else {
  52. $_SESSION['error'][] = "Thumbnail creation: resize error : " . $image->error;
  53. }
  54. // 4. STORE IN DB
  55. $image->Clean();
  56. // continue to mysql insert query ....

isn't this easy or what ?