class.upload.php is a powerful and mature PHP class to manage uploaded files, and manipulate images in many ways. The script is available under a GPL license.
Well yes, but you can't both insert the file into the database and to the server as a file if you also need to modify the file. Reason being, each time you call process() everything gets reset.
So in my case, I want to convert the image format of the original, then save it on disk and also to the database. Then I want to create a thumbnail version, which I also want to save on disk as well as to the database. So in essence I have 4 processes to go through for each uploaded file:
1) Reformat the file and save it on disk. 2) Reformat the file as in #1, then save it to the database. 3) Resize the file as a thumbnail and save it on disk. 4) Resize the file as in #3 and save it to the database.
I can somewhat reduce the amount of code required by creating function "A" that will handle the image reformatting, and function "B" to handle the image resizing. I can then call on function "A" for steps 1 and 2, and function "B" for steps 3 and 4.
But if I've still got this all wrong, feel free to jump in! And thanks for your quick feedback!Reply
include('class.upload.php');
$foo = new upload($_FILES['form_field']);
if ($foo->uploaded) {
// Your first pass at the image
$foo->image_convert = 'png';
$foo->image_resize = true;
$foo->image_x = 500;
$foo->image_ratio_y = true;
// Call process() without an argument to get the image in return
$img = $foo->process();
// File WAS processed successfully.
if ($foo->processed) {
// Prepare the query (and then run it). The content of the image is in $img
$query = " INSERT INTO my_table(image_file) VALUES ('" . addslashes($img) . "')";
// Store the picture on file, using PHP functions
imagepng($img, '/home/a/b/c/image.png');
// Do not delete the temp file as we will do another pass
}
// Your second pass at the image (thumbnail)
$foo->image_convert = 'png';
$foo->image_resize = true;
$foo->image_x = 100;
$foo->image_ratio_y = true;
// Call process() without an argument to get the image in return
$img = $foo->process();
// File WAS processed successfully.
if ($foo->processed) {
// Prepare the query (and then run it). The content of the image is in $img
$query = " INSERT INTO my_table(thumbnail_file) VALUES ('" . addslashes($img) . "')";
// Store the picture on file, using PHP functions
imagepng($img, '/home/a/b/c/thumbnail.png');
// Delete the temporary file(s).
$foo->clean();
}
}
So in my case, I want to convert the image format of the original, then save it on disk and also to the database. Then I want to create a thumbnail version, which I also want to save on disk as well as to the database. So in essence I have 4 processes to go through for each uploaded file:
1) Reformat the file and save it on disk.
2) Reformat the file as in #1, then save it to the database.
3) Resize the file as a thumbnail and save it on disk.
4) Resize the file as in #3 and save it to the database.
I can somewhat reduce the amount of code required by creating function "A" that will handle the image reformatting, and function "B" to handle the image resizing. I can then call on function "A" for steps 1 and 2, and function "B" for steps 3 and 4.
But if I've still got this all wrong, feel free to jump in! And thanks for your quick feedback!