/*************************************************************\
* Image resizer, version 2.2 - August01,2003 *
* Returns true on success, false if there are errors *
* If destination file is specified, resized picture will be *
* saved with the specified name. Else, it is shown to browser *
* Version 2: Now scales both up and down, still preserving *
* aspect ratio, and it accepts not only jpeg, but *
* also png and possibly bmp *
* Version 2.1: Genereal optimization and bugfixing. *
* No longer requires GD-lib to be >=2.0.1 *
* Version 2.2: Removed use of image_type_to_mime_type() *
* Added the array $mimes instead *
}*************************************************************{
* Created by XyborX (
www.xyborx.dk) *
* Feel free to use, modify and distribute, without profit. *
* I cannot be held liable for any damage this script might do *
\*************************************************************/
function imageresize($sourcefile,$maxwidth,$maxheight,$destinationfile=false)
{
$types=Array();
$types[1]='gif';
$types[2]='jpeg';
$types[3]='png';
$types[6]='wbmp';
$mimes=Array();
$mimes['gif']='image/gif';
$mimes['jpeg']='image/jpeg';
$mimes['png']='image/png';
$mimes['wbmp']='image/bmp';
list($width,$height,$type)=getimagesize($sourcefile);
$type_name=$types[$type];
$imagecreatefromtype='imagecreatefrom'.$type_name;
$imagetype='image'.$type_name;
if(!function_exists($imagecreatefromtype) || !function_exists($imagetype))
{
return false;
}
if($img_src=$imagecreatefromtype($sourcefile))
{
$ratio_height=$maxheight/$height;
$ratio_width=$maxwidth/$width;
$ratio=min($ratio_width,$ratio_height);
$newwidth=$width*$ratio;
$newheight=$height*$ratio;
if(function_exists('imagecreatetruecolor'))
{
$img_dst=imagecreatetruecolor($newwidth,$newheight);
}
elseif(function_exists('imagecreate'))
{
$img_dst=imagecreate($newwidth,$newheight);
}
else
{
return false;
}
if(function_exists('imagecopyresampled'))
{
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
elseif(function_exists('imagecopyresized'))
{
imagecopyresized($img_dst, $img_src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
else
{
imagedestroy($img_src);
imagedestroy($img_dst);
return false;
}
if($destinationfile==false)
{
if(isset($mimes[$type]))
{
header('Content-type: '.$mimes[$type_name]);
}
$imagetype($img_dst);
}
else
{
$imagetype($img_dst,$destinationfile);
}
imagedestroy($img_src);
imagedestroy($img_dst);
return true;
}
else
{
return false;
}
}
// max size in kB
$max_file_size = 100;
// max size
$max_file_width = 250;
$max_file_height = 250;
// convert kB -> B
$max_file_size = $max_file_size * 1024;
if (isset($_POST['up'])) {
$i = 0;
require ("../db.php");
while (list($name, $value) = each($_FILES['upfil']['name'])) {
if ($value) {
if ($_FILES['upfil']['size'][$i] <= $max_file_size) {
$dist = $_SERVER["DOCUMENT_ROOT"] ."/fbbgalleri/img/";
$files = $value;
$temparr = explode(".", $files);
$temparr[count($temparr)-2] = time();
$files = implode(".", $temparr);
$rand = rand(1,100000);
$files = "$rand$files";
move_uploaded_file($_FILES['upfil']['tmp_name'][$i], $dist . $files);
list($width, $height, $type, $attr) = getimagesize($dist . $files);
if (($width > $max_file_width) || ($height > $max_file_height) {
imageresize($dist.$files, $max_file_width, $max_file_height, $dist.$files);
}
mysql_query("INSERT INTO gallery (time, filename) VALUES (now(), '$files')");
}
else
echo "Filen er for stor!";
}
$i++;
}
}