- Code: Select all
session_start();
/*
* I Created the form for multiple upload like this:
*
* <form action="" method="post" enctype="multipart/form-data">
* <input type="file" name="images[]" />
* <input type="file" name="images[]" />
* <input type="file" name="images[]" />
* <input type="submit" name="submit" value="Send" />
* </form>
*/
$ERRORS = array();
if (!function_exists('imagecreatetruecolor')){
die("GD Library version 2+ is required!");
}
// Define some constants
define('TARGET_PATH', 'gchomeimages2/');
define('UPPER_HEIGHT', 1500);
define('UPPER_WIDTH', 2000);
define('MED_HEIGHT', 400);
define('MED_WIDTH', 533);
define('LARGE_HEIGHT', 1500);
define('LARGE_WIDTH', 2000);
define('THUMB_HEIGHT', 90);
define('THUMB_WIDTH', 125);
function is_valid_type($type){
$valid_types = array("image/jpg",
"image/pjpeg",
"image/jpeg",
"image/bmp",
"image/gif",
"image/x-png",
"image/png");
if (in_array($type, $valid_types)) return true;
return false;
}
function createImage($id){
$imagetype = $_FILES["images"]["type"][$id];
$tmp_name = $_FILES["images"]["tmp_name"][$id];
$image = null;
switch($imagetype){
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
$image = imagecreatefromjpeg($tmp_name);
break;
case "image/x-png":
case "image/png":
$image = imagecreatefrompng($tmp_name);
break;
case "image/gif":
$image = imagecreatefromgif($tmp_name);
break;
}
return $image;
}
function addWatermark($image, $wmimg, $quality, $bufferw, $bufferh){
$img_h = imagesy($image);
$img_w = imagesx($image);
$watermark = imagecreatefrompng($wmimg);
$watermark_w = imagesx($watermark);
$watermark_h = imagesy($watermark);
$x = $img_w - $watermark_w - $bufferw;
$y = $img_h - $watermark_h - $bufferh;
imagecopymerge($image, $watermark, $x, $y, 0, 0, $watermark_w, $watermark_h, $quality);
return $image;
}
function resizeImage($image, $w, $h){
$img_h = imagesy($image);
$img_w = imagesx($image);
$img = imagecreatetruecolor($w, $h);
imagecopyresampled($img, $image, 0, 0, 0, 0, $w, $h, $img_w, $img_h);
return $img;
}
function saveImage($image, $path){
global $ERRORS;
if(!ImageJpeg($image, $path, 100)){
$ERRORS[] = "Image $path couldn't be saved";
return;
}
}
function processImage($id){
global $ERRORS;
// Get some image informations
$filename = $_FILES["images"]["name"][$id];
$name = $filename;
$img_no = $id + 1;
if(($pos = strpos($filename, '.')) > 0){
// Remove file-extension from filename
$name = substr($filename, -$pos);
}
$imagetarget = TARGET_PATH . $filename . "_thumb.jpeg";
$imagetype = $_FILES["images"]["type"][$id];
if(!is_valid_type($imagetype)){
$ERRORS[] = "Image $img_no is a wrong image type. Upload a jpeg, gif or bmp";
return;
}
if(file_exists($imagetarget)){
$ERRORS[] = "A image with the name $filename already exists in target";
return;
}
// using factory to create image
// automatically checks type and returns the right image
$image = createImage($id);
$img_height = imagesy($image);
$img_width = imagesx($image);
$img_large = null;
$img_med = null;
$img_thumb = null;
if($img_height > 0 && $img_height < MED_HEIGHT){
$ERRORS[] = "File $img_no is too medium";
return;
}
if($img_height > UPPER_HEIGHT){
$img_large = resizeImage($image, LARGE_WIDTH, LARGE_HEIGHT);
$img_large = addWatermark($img_large, "watermark.png", 50, (LARGE_WIDTH / 100), (LARGE_HEIGHT / 100) * 2.5);
saveImage($img_large, TARGET_PATH. $filename . "_large.jpeg");
}
if($img_height > MED_HEIGHT || $img_height = MED_HEIGHT){
$img_med = resizeImage($image, MED_WIDTH, MED_HEIGHT);
$img_med = addWatermark($img_med, "watermark.png", 50, 7, 5);
saveImage($img_med, TARGET_PATH . $filename . "_med.jpeg");
$img_thumb = resizeImage($image, THUMB_WIDTH, THUMB_HEIGHT);
saveImage($img_thumb, TARGET_PATH . $filename . "_thumb.jpeg");
}
}
if($_POST['submit']){
if(!$_FILES["images"]["name"][0]) {
echo "Please Upload a jpeg, gif, bmp or png ";
}
foreach ($_FILES["images"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
// will create thumb, med and large images
// if imagefile is "image.png" it will create:
// image_thumb.jpeg, image_med.jpeg, image_large.jpeg
//
processImage($key);
} elseif ($error == UPLOAD_ERR_NO_FILE) {
continue;
} else {
$no_img = $key + 1;
$ERRORS [] = "File $no_img couldn't be uploaded. ERROR CODE:" . $error;}
}
// print errors
echo implode($ERRORS, "<br />");
}
?>
However, the problem I am now having is that I want to be able to store the names of these images into an SQL database, but I am having problems storing the image names - every time I try to store the image name the only name that gets stored is "array[name][1]" for example.
Does anyone know what I would put into the SQL query to make it store the image name properly?
Please help me here guys, thanks

