<?php
$comp_id="";
$AppID="";
 include_once('opendb.php');
if (isset($_GET["comp_id"]))
{
   $comp_id = $_GET["comp_id"];
}
else {
    die ("Error, No Company ID");
}
if ($comp_id==''){
    die ("Error, Company ID empty");
}
if (isset($_GET["AppID"])){
  $AppID=$_GET["AppID"] ;  
}
if ($AppID==''){
    die ("Error, APP ID empty");
}    
$sql = "SELECT * FROM company WHERE app_id like '".$AppID."' and comp_id =".$comp_id.";";
$sresult = $conn->query($sql);
if ($sresult->num_rows == 0 )
{ 
    $conn->close();
    die ("Error, invalid company id or App ID"); 
}

 
$file = $_FILES['file']['tmp_name']; 
    $source_properties = getimagesize($file);
    $image_type = $source_properties[2]; 
   /* if( $image_type == IMAGETYPE_JPEG ) {   
        $image_resource_id = imagecreatefromjpeg($file);  
        $target_layer = fn_resize($image_resource_id,$source_properties[0],$source_properties[1]);
        imagejpeg($target_layer,trim($comp_id)."_photos/thump_".$_FILES['file']['name'] );       
    }
     elseif( $image_type == IMAGETYPE_GIF )  {  
        $image_resource_id = imagecreatefromgif($file);
        $target_layer = fn_resize($image_resource_id,$source_properties[0],$source_properties[1]);
        imagegif($target_layer,trim($comp_id)."_photos/thump_".$_FILES['file']['name'] );
    }
    elseif( $image_type == IMAGETYPE_PNG ) {
        $image_resource_id = imagecreatefrompng($file); 
        $target_layer = fn_resize($image_resource_id,$source_properties[0],$source_properties[1]);
        imagepng($target_layer,trim($comp_id)."_photos/thump_".$_FILES['file']['name'] );
    }*/

if (move_uploaded_file($_FILES["file"]["tmp_name"], trim($comp_id)."_photos/" . $_FILES["file"]["name"])){
  $sql = "SELECT * FROM appsettings WHERE set_category='GENERAL' and set_name='photo_edit_time' and comp_id=" .$comp_id;
    $result = $conn->query($sql);
    if ($result->num_rows == 0 ) {
           $sql = "INSERT INTO appsettings (set_category,set_name,val_type,comp_id) VALUES ('GENERAL','photo_edit_time','S',".$comp_id.")";
           $conn->query($sql);
    }
    $Set_Value=date("YmdHis");
    $sql = "UPDATE appsettings SET set_value='".$Set_Value."' WHERE set_category='GENERAL' and set_name='photo_edit_time' and comp_id=" .$comp_id;
    $conn->query($sql);
    $pcode=$_FILES['file']['name'];
    $pcode=substr($pcode, 0, -4);
    $sql = "UPDATE products SET photo_edit_time=".$Set_Value." WHERE prod_code='$pcode' and comp_id=" .$comp_id;
    $conn->query($sql);
    
    $convertedFile =  trim($comp_id)."_photos/" . pathinfo($_FILES["file"]["name"], PATHINFO_FILENAME) . ".jpg";
    $image = imagecreatefromjpeg(trim($comp_id)."_photos/" . $_FILES["file"]["name"]);
    imagewebp($image, $convertedFile, 85); // 80 is the quality value (0-100)
    
    echo "OK";
    
}
else {
    $upload_dir= trim($comp_id)."_photos/";
    if (is_dir($upload_dir) && is_writable($upload_dir)) {
        echo "Error";
    } else {
        echo 'Upload directory is not writable, or does not exist.';
    }
    
}
$conn->close();

function fn_resize($image_resource_id,$width,$height) {
$target_width =80;
$target_height =80;
$target_layer=imagecreatetruecolor($target_width,$target_height);
imagecopyresampled($target_layer,$image_resource_id,0,0,0,0,$target_width,$target_height, $width,$height);
return $target_layer;
}
?>