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.
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();
}
}