i have this code to upload files to a folder (uploads). In a file fileupload.php i have a input with the type file multiple. I the use th file (upload.php) that contains the code below, to upload files (images) to the folder (upload) and this works fine. But i also would like to create thumbnails to put in to (uploads/thumbs). And also i need to set the size for these thumbnails. Thank you.
My code so far:
<?php
ready = "OK, Uppladdat";
uploaddir = "images/" . $imagepath;
uploaddir_th = "images/thumbs/" . $imagepath;
allowed = array('jpg','jpeg','gif','png');
max_size = 5048 * 1024;
while (list ($key, $val) = each ($_FILES))
{
if ($_FILES[$key]['size'] <= $max_size)
{
$file_ext = pathinfo($_FILES[$key]['name'],PATHINFO_EXTENSION);
$file_name = basename($_FILES[$key]['name'],'.'.$file_ext);
if (in_array(strtolower($file_ext),$allowed))
{
$name = $_FILES[$key]['name'];
$x = 1;
while (file_exists($uploaddir.'/'.$name))
{
$name = $file_name.'['.$x.'].'.$file_ext;
$x++;
}
if (move_uploaded_file($_FILES[$key]['tmp_name'],$uploaddir.'/'.$name))
{
chmod($uploaddir.'/'.$name, 0644);
}
else
{
die(error_get_last());
}
}
else
{
die("Invalid file type");
}
}
else
{
die("File size too big");
}
//thumbnail image making part
list($width, $height) = getimagesize($uploaddir.'/'.$name);
$modwidth = 200;
$modheight = 140;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($uploaddir.'/'.$name);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight);
/* echo "Thumbnail: <img src='images/thumbs".$imagepath."'>"; */
imagejpeg($image, $uploaddir_th.'/'.$name, 100);
}
echo $ready;
?>