<?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"); 
}
$conn->close();
 $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"])){
    
    echo "OK";
}
else {
    echo "Error";
}



function fn_resize($image_resource_id,$width,$height) {
$target_width =150;
$target_height =150;
$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;
}
?>