<?php
// Slaed Upload Module
// Copyright Nisoft.ru and Slaed.net
// WaterMark code, programmed by Nitro.
// Other code, programmed by Eduard.
if (!defined('MODULE_FILE')) {
	Header("Location: ../../index.php");
	exit;
}
$module_name = basename(dirname(__FILE__));
get_lang($module_name);
$pagetitle = "$defis "._TITLUPLOAD."";

include ("header.php");
#include ("watermark.php");
OpenTable();

// File size
$max_size = 1024000;



$max_ok = number_format(($max_size/1024), 1, ',', '.');
echo "<center><font class=\"title\"><b>"._TITLUPLOAD."</b></font><br><br>"
.""._INSTRUCTIONUPLOAD."<br>"._MAX_IMAGE.": ".$max_ok." KB"
."<form enctype=\"multipart/form-data\" action=\"index.php?name=Upload\" method=\"post\"><b>"
._FILETYPE.":</b> <select name=\"path\">\n"
."<option value=\"\">------------</option>\n"
."<option value=\"uploads/images/\">"._FILENEWS."</option>\n"
."<option value=\"uploads/images/\">"._FILEGAME."</option></select>\n"
."&nbsp;&nbsp;&nbsp;<b>"._FILEPOSITION.":</b> <input name=\"userfile\" type=\"file\" size=\"30\"><br><br>\n"
."<input type=\"submit\" name=\"submit\" value=\""._SEND."\"></center></form>\n";

if ($path != "") {
	echo "<hr>";
	if (!isset($_FILES['userfile'])) exit;
	if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
		if ($_FILES['userfile']['size'] > $max_size) {
			echo "<center><font class=\"boxtitle\"><b>"._TOOBIG."</b></font></center><br>";
		} else {
			$img_userfile_type = $_FILES['userfile']['type'];
			$img_type = end(explode(".", $_FILES['userfile']['name']));
			if (($img_userfile_type == "image/gif" || $img_userfile_type == "image/pjpeg" || $img_userfile_type == "image/jpeg") && ($img_type != "php" || $img_type != "php3" || $img_type != "php4" || $img_type != "pl" || $img_type != "htm" || $img_type != "html" || $img_type != "phtml" || $img_type != "asp" || $img_type != "cgi")) {
				if (file_exists($path . $_FILES['userfile']['name'])) {
					echo "<center><font class=\"boxtitle\"><b>"._EXIST."</b></font></center><br>";
				} else {
				        $imgfile = $_FILES['userfile']['tmp_name'];
				        $photoImage=imagecreatefromstring(file_get_contents($imgfile));
                                        $margin = 7;
                                        $minimage=100;
                                        
$watermark_image_light = 'modules/Upload/watermark_light.png';
$watermark_image_dark =  'modules/Upload/watermark_dark.png';

        $image_width = imagesx($photoImage);
        $image_height = imagesy($photoImage);

        list($watermark_width, $watermark_height)
            = getimagesize($watermark_image_light);


                $watermark_x = $image_width - $margin - $watermark_width;
                $watermark_y = $image_height - $margin - $watermark_height;


        $watermark_x2 = $watermark_x + $watermark_width;
        $watermark_y2 = $watermark_y + $watermark_height;

        if ($watermark_x < 0 OR $watermark_y < 0 OR
            $watermark_x2 > $image_width OR $watermark_y2 > $image_height OR
         $image_width < $min_image OR $image_height < $min_image)
        {
           return;
        }


        $test = imagecreatetruecolor(1, 1);
        imagecopyresampled($test, $photoImage, 0, 0, $watermark_x, $watermark_y, 1, 1, $watermark_width, $watermark_height);
        $rgb = imagecolorat($test, 0, 0);

        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;

        $max = min($r, $g, $b);
        $min = max($r, $g, $b);
        $lightness = (double)(($max + $min) / 510.0);
        imagedestroy($test);

        $watermark_image = ($lightness < 0.5) ? $watermark_image_light : $watermark_image_dark;

        $watermark = imagecreatefrompng($watermark_image);

        imagealphablending($photoImage, TRUE);
        imagealphablending($watermark, TRUE);

        imagecopy($photoImage, $watermark, $watermark_x, $watermark_y, 0, 0,$watermark_width, $watermark_height);
        ImageJPEG($photoImage, $_FILES['userfile']['tmp_name']);
        imagedestroy($watermark);
        imagedestroy($photoImage);
                                        $res = copy($_FILES['userfile']['tmp_name'], $path . $_FILES['userfile']['name']);
					if (!$res) {
						echo "<center><font class=\"boxtitle\"><b>"._ERROR_UP."</b></font></center><br>";
					} else {
						echo "<center><font class=\"boxtitle\"><b>"._YOURFILE."</font> ".$_FILES['userfile']['name']." <font class=\"boxtitle\">"._SUCCESS."</b></font><br><br>";
						$dimensione = $_FILES['userfile']['size'];
						$kdimensione = number_format(($dimensione/1024), 1, ',', '.');
						echo "<b>"._FILESIZE.":</b> ".$kdimensione." KB<br><br>";
						echo "<b>"._FILEURL.":</b> <input type=\"text\" value=\"".$path . $_FILES['userfile']['name']."\" size=\"100\"><br><br>\n";
						echo "<b>"._IMAGE."</b><br><br>";
						echo "<img src=\"".$path . $_FILES['userfile']['name']."\"></center>";
					}
				}
			} else {
				echo "<center><font class=\"boxtitle\"><b>"._ERROR_FILE."</b></font></center><br>";
			}
		}
	}
}
CloseTable();
include ("footer.php");
?>
