hello,
I have written a code that allows me, through a form, to store info in the database + upload files on the server.
I would like to upgrade my script by storing on the server thumbnails of the images I can already upload.
I would like to make a variation in the name of the thumbnail too (not too hard...)
main image: 20071116_randomname.jpg
thumbnail: 20071116_thumb_randomname.jpg
BUT I have a problem with my code right now:
I am using a code that work like a charm as a standalone script: I use one image, that returns the thumbnail. but I have problems adapting this to my current script.
the thumbnail code return the path and filename in which i am working instead of the thumbnails themselves...
can anyone give me some clues on how i can handle that please?
thanks!
<?php
session_start();
if (isset($_SESSION['level']) && $_SESSION['level'] == 2){}
else
{
$_SESSION['error'] = "3";
header('Location: ../../error.php');
}
require("../../includes/config.php");
require('../../includes/functions.tpl.php');
//initiate the array in which the files names will be stored
$i = 0;
$name_array = array();
$thumb_array = array();
while(list($key,$value) = each($_FILES['image']['name']))
{
if(!empty($value))
{
//$filename = $value;
//echo $_FILES['image']['name'][$key];
//print "<br>$filename<br>";
$uploaddir = "../../gifts";
$allowed_ext = array( 'jpg', 'gif', 'png' );
$max_size = "50000";
$max_height = "300";
$max_width = "300";
$upload = '';
//get the file's extension
$ext = pathinfo($_FILES['image']['name'][$key]);
$extension = $ext['extension'];
//compare uploaded file with authorized extensions
if (in_array($extension,$allowed_ext))
{
//check the file's size in kb
if($_FILES[image][size][$key] > $max_size)
{
print "File '$key' size is too big!";
$upload = false;
}
//check the file's dimension WxH
if ($max_width && $max_height)
{
list($width, $height, $type, $w) = getimagesize($_FILES['image']['tmp_name'][$key]);
if($width > $max_width || $height > $max_height)
{
print "<br>File '$key' height and/or width are too big!";
$upload = false;
}
//upload the file!
else
{
//echo $_FILES['image']['tmp_name'][$key];
if(empty($upload) && is_uploaded_file($_FILES['image']['tmp_name'][$key]))
{
//create a random name for the uploaded file
$date = date ("Ymd");
$name = random_string();
$uploadname = $date.'_'.$name.'.'.$extension;
$name_array[] = "$uploadname";
//move_uploaded_file($_FILES['image']['tmp_name'][$key],$uploaddir.'/'.$uploadname);
//Start Thumbnail script
//This will set our output to 50% of the original size
$size = 0.50;
// This sets it to a .jpg, but you can change this to png or gif
header('Content-type: image/jpeg');
// Setting the resize parameters
list($width, $height) = getimagesize($uploadname);
$modwidth = $width * $size;
$modheight = $height * $size;
// Resizing the Image
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($uploadname);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
// Outputting a .jpg, you can make this gif or png if you want
//notice we set the quality (third value) to 100
imagejpeg($tn, null, 70);
$i++;
$upload = true;
}
}
}
}
else
{
print "Incorrect file extension!";
}
}
}
if ($upload = true)
{
//insert fom values into database
$fields = array_keys($_POST);
for ($i = 0; $i < count($fields); $i++)
{
$array = array($_POST[$fields[2]], $_POST[$fields[1]], $_POST[$fields[0]]);
$start = implode("-", $array);
$array = array($_POST[$fields[5]], $_POST[$fields[4]], $_POST[$fields[3]]);
$expiry = implode("-", $array);
$name = addslashes($_POST[$fields[6]]);
$description = addslashes($_POST[$fields[7]]);
$stock = $_POST[$fields[8]];
$points_redeem = $_POST[$fields[9]];
$points_play = $_POST[$fields[10]];
$type = $_POST[$fields[11]];
$link = $_POST[$fields[12]];
}
$query= ("INSERT INTO gifts (start, expiry, name, description, stock, points_redeem, points_play, type, link, pic1, pic2, pic3, pic4, pic5, pic6)
VALUES ('$start', '$expiry', '$name', '$description', '$stock', '$points_redeem', '$points_play', '$type', '$link', 'gifts/$name_array[0]', 'gifts/$name_array[1]', 'gifts/$name_array[2]', 'gifts/$name_array[3]', 'gifts/$name_array[4]', 'gifts/$name_array[5]' )");
mysql_query($query) or die('Invalid query: ' . mysql_error());
print "Your file has been uploaded successfully! Yay!";
}
else
{
print "<br>failed uploading the file";
}
?>
the idea is to store the thumbnails on the server with a variation in the name AND to store the thumbnails' path and name in the table (so I need to store the thumbnails' name in $thumb_array[])
hope someone can help me out 🙂