($dir)"));
if($_POST['upload'])
{
//cheking is file selected
if($_FILES['file']['name']=="")
die(stopUpload('alert', $DLG['select_file']));
//geting file/path info
$info = swampy_pathinfo($_FILES['file']['name']);
$filename = ($_POST['filename'] != "") ? $_POST['filename'] : $info['filename'];
$filename = format_filename($filename);
//cheking file name
if(ereg("[^a-zA-Z0-9._-]", $filename))
die(stopUpload('alert',$DLG['invalid_filename']));
//check is file name exists
if(is_filename_exists($directory, $filename))
die(stopUpload('alert', $DLG['file_exists']));
$src_path = $_FILES['file']['tmp_name']; // Source image path from uploaded file
$image_info = getimagesize($src_path); // Geting source image info width height type
// Selecting operation to load image by its type
switch($image_info[2])
{
case 1:// GIF
$extension = ".gif";
$src_image = @ ImageCreateFromGIF ($src_path);
break;
case 2:// JPEG
$extension = ".jpg";
$src_image = @ ImageCreateFromJPEG ($src_path);
break;
case 3:// PNG
$extension = ".png";
$src_image = @ ImageCreateFromPNG ($src_path);
break;
default:
die(stopUpload('error', $DLG['invalid_img']));
break;
}
//checking is image loaded
if(!$src_image)
die(stopUpload('error', $DLG['invalid_image']));
$IMAGE_FORMATS['custom'] = $_POST['format']; // Adds custom format to IMAGE FORMAT list from configure file
array_push($_POST['formats'], "mthumb"); // Adds manager thumb format
//seting orginal width and height from image info
$src_w = $image_info[0]; //width
$src_h = $image_info[1]; //height
$src_ratio = $src_w / $src_h; //source image width/height ratio
umask(0002);
//making images by selected formats
foreach($_POST['formats'] as $fid)
{
$format = $IMAGE_FORMATS[$fid];
if(!$format['width'] && $format['height'])
$format['width'] = round($format['height'] * $src_ratio);
if($format['width'] && !$format['height'])
$format['height'] = round($format['width'] / $src_ratio);
$width = $format['width'] ? $format['width'] : $src_w;
$height = $format['height'] ? $format['height'] : $src_h;
$dst_x = $dst_y = $src_x = $src_y = 0;
$format_ratio = $width / $height; //image format ratio
//formating image parameter by scale type
switch($format['scale'])
{
case "crop":
$dst_w = ($format_ratio < $src_ratio) ? round($height * $src_ratio) : $width;
$dst_h = ($format_ratio > $src_ratio) ? round($width / $src_ratio) : $height;
$dst_x = ($width/2) - ($dst_w/2);
$dst_y = ($height/2) - ($dst_h/2);
break;
case "addbg":
$dst_w = ($format_ratio > $src_ratio) ? round($height * $src_ratio) : $width;
$dst_h = ($format_ratio < $src_ratio) ? round($width / $src_ratio) : $height;
$dst_x = ($width/2) - ($dst_w/2);
$dst_y = ($height/2) - ($dst_h/2);
break;
case "max":
$width = $dst_w = ($format_ratio > $src_ratio) ? round($height * $src_ratio) : $width;
$height = $dst_h = ($format_ratio < $src_ratio) ? round($width / $src_ratio) : $height;
break;
case "stretch":
$dst_w = $width;
$dst_h = $height;
break;
case "orginal":
default:
$width = $dst_w = $src_w;
$height = $dst_h = $src_h;
break;
}
$dst_image = imageCreateTrueColor($width, $height);
//creates image backgound color
if($format['scale'] == "addbg")
{
$r = hexdec(substr($format['bg'],0,2));
$g = hexdec(substr($format['bg'],2,2));
$b = hexdec(substr($format['bg'],4,2));
$color = imagecolorallocate($dst_image, $r, $g, $b);
imagefill($dst_image, 0, 0, $color);
}
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
//image masking
if($format['mask'])
{
$mask_image = ImageCreateFromPNG($PUBLIC_HTML_DIR.$format['mask']);
imagealphablending($mask_image, 1);
imagecopy($dst_image, $mask_image, 0, 0, 0, 0, $dst_w, $dst_h);
}
if(!file_exists($directory.$format['dir'])) //Creating directory if needed
mkdir($directory.$format['dir'], 0775);
$ext = $format['ext'] ? ".".$format['ext'] : $extension; //seting extension format
$file = $filename.$ext; //seting full file name
$dst_path = $directory.$format['dir'].$file; //image destination path
switch($ext) //creating image by selected extension format
{
case ".jpg":
imagejpeg($dst_image, $dst_path);
break;
case ".png":
imagepng($dst_image, $dst_path, 9);
break;
case ".gif":
imagegif($dst_image, $dst_path);
break;
}
imagedestroy($dst_image); //frees any memory associated with destination image image
}
$file = $filename.$extension;
$msg = rawurlencode("'$filename' {$DLG['upload_success']}
{$DLG['insert_uploaded']}");
die(stopUpload('done', $msg));
}
?>