<?php
require_once('../wu_init.php');
if (!wu_token()) { exit('wu-error'); }
if (!USER_LOGGED) { exit('3'); }

$id = intval($_POST['item']);
if (empty($id)) { exit('3'); }
$nu = mysqli_fetch_assoc(mysqli_query($connect_db, "SELECT id,usr FROM ".DB_PREFIX."_es WHERE id='$id' LIMIT 1"));
if ($nu['usr'] != $u_id) { exit('3'); }
$ns = mysqli_fetch_assoc(mysqli_query($connect_db, "SELECT count(`id`) AS `c` FROM ".DB_PREFIX."_es_i WHERE m = '$id'"));
if ($ns['c'] > 20) { exit('5'); }
$valid_types =  array('gif','jpg', 'png', 'jpeg', 'GIF', 'JPG', 'PNG', 'JPEG');
$path = '../../eshop/images/';
$si = getimagesize($_FILES['scr']['tmp_name']);
$exts = substr($_FILES['scr']['name'], 1 + strrpos($_FILES['scr']['name'], "."));
$size = $_FILES['scr']['size'];
if($size>(1024*1024)) { exit('4'); }
if (!in_array($exts, $valid_types)) { exit('0'); }
if ($si[0] == 0 || $si[1] == 0) { exit('3'); }
$rand = rand(0,999);
$src_s = $path.$dt.'_'.$rand.'.'.$exts;
$srcs = $dt.'_'.$rand.'.'.$exts;
if (move_uploaded_file($_FILES['scr']['tmp_name'], $src_s)) {
$image_path = '../../wu-static/img/watermark.png';
$type = array_pop(explode('.',$src_s));
list($owidth,$oheight) = getimagesize($src_s);
if($type=='png') {
$im = imagecreatetruecolor($owidth, $oheight);
$img_src = imagecreatefrompng($src_s);
imagecopyresampled($im, $img_src, 0, 0, 0, 0, $owidth, $oheight, $owidth, $oheight);
}
if($type=='gif') {
$im = imagecreatetruecolor($owidth, $oheight);
$img_src = imagecreatefromgif($src_s);
imagecopyresampled($im, $img_src, 0, 0, 0, 0, $owidth, $oheight, $owidth, $oheight);}
if($type=='jpeg') {$im = imagecreatefromjpeg($src_s);}
if($type=='jpg') {$im = imagecreatefromjpeg($src_s);}
if($type=='PNG') {
$im = imagecreatetruecolor($owidth, $oheight);
$img_src = imagecreatefrompng($src_s);
imagecopyresampled($im, $img_src, 0, 0, 0, 0, $owidth, $oheight, $owidth, $oheight);
}
if($type=='GIF') {
$im = imagecreatetruecolor($owidth, $oheight);
$img_src = imagecreatefromgif($src_s);
imagecopyresampled($im, $img_src, 0, 0, 0, 0, $owidth, $oheight, $owidth, $oheight);}
if($type=='JPEG') {$im = imagecreatefromjpeg($src_s);}
if($type=='JPG') {$im = imagecreatefromjpeg($src_s);}
$watermark = imagecreatefrompng($image_path);
list($w_width, $w_height) = getimagesize($image_path);
$pos_x = $owidth - $w_width-5;
$pos_y = $oheight - $w_height-5;
imagecopy($im, $watermark, $pos_x, $pos_y, 0, 0, $w_width, $w_height);
if($type=='png') {unlink($src_s); imagepng($im, $src_s, 9);}
//if($type=='gif') {unlink($src_s); imagegif($im, $src_s, 60);}
if($type=='jpeg') {unlink($src_s); imagejpeg($im, $src_s, 60);}
if($type=='jpg') {unlink($src_s); imagejpeg($im, $src_s, 60);}
if($type=='PNG') {unlink($src_s); imagepng($im, $src_s, 9);}
//if($type=='GIF') {unlink($src_s); imagegif($im, $src_s, 60);}
if($type=='JPEG') {unlink($src_s); imagejpeg($im, $src_s, 60);}
if($type=='JPG') {unlink($src_s); imagejpeg($im, $src_s, 60);}
imagedestroy($im);
mysqli_query($connect_db, "INSERT INTO `".DB_PREFIX."_es_i` (m,img) VALUES ('$id','$srcs')");
mysqli_query($connect_db, "UPDATE ".DB_PREFIX."_users SET rat=rat+0.02 WHERE uid='$u_id' LIMIT 1");
exit('1');
} else {
exit('3');
}
?>