<?php
session_start();
require_once('inc/dbcon.php');
require_once('inc/function.php');
require_once('inc/config.php');
$id=$_GET['id'];
if($_GET['t']=='toko'){
	if($id){
		$qimg=mysqli_query($connect, "select gambar_toko from toko where ID='$id'") or die(mysqli_error($connect));
	}
}elseif($_GET['t']=='user'){
	if($id){
		$qimg=mysqli_query($connect, "select user_foto from users where ID='$id'") or die(mysqli_error($connect));
	}
}elseif($_GET['t']=='admin'){
	if($id){
		$qimg=mysqli_query($connect, "select foto from admin where ID='$id'") or die(mysqli_error($connect));
	}
}elseif($_GET['t']=='produk'){
	if($id){
		$qimg=mysqli_query($connect, "select gambar_produk from produk where ID='$id'") or die(mysqli_error($connect));
	}
}elseif($_GET['t']=='kasir'){
	if($id){
		$qimg=mysqli_query($connect, "select foto from karyawan where ID='$id'") or die(mysqli_error($connect));
	}
}

$jimg=mysqli_num_rows($qimg);
if($jimg==1){
	$bimg=mysqli_fetch_array($qimg);
	if($bimg[0]){
		$uri = getDataURI('assets/images/'.$_GET['t'].'/'.$bimg[0]);
		$percent 	 = (isset($_GET['s'])) ? $_GET['s'] : 50;
	}else{
		$uri = getDataURI('assets/images/placeholder.png');
		$percent 	 = 100;
	}
}else{
	$uri = getDataURI('assets/images/placeholder.png');
	$percent 	 = 100;
}
$image = explode(';',$uri);
$imgdata = explode('base64,',$image[1]);
$gambar = base64_decode($imgdata[1]);
$type = explode(':',$image[0]);
$imgtype = $type[1];
$im = imagecreatefromstring($gambar);
if($im!==false){
	header('Content-type: '.$imgtype);
	header("access-control-allow-origin: *");
	$lebar       = imagesx($im);
	$tinggi      = imagesy($im);
	$tmb_lebar   = $lebar*($percent/100);
	$tmb_tinggi  = $tinggi*($percent/100);
	$image_tmb   = imagecreatetruecolor($tmb_lebar,$tmb_tinggi);
	imagealphablending($image_tmb, false);
	imagesavealpha($image_tmb, true);
	imagecopyresampled($image_tmb,$im,0,0,0,0,$tmb_lebar,$tmb_tinggi,$lebar,$tinggi);
	imagepng($image_tmb);
	imagedestroy($im);
	imagedestroy($image_tmb);
}
?>