<?php
class ImageUpload{


function uploadImage($filename,$data,$companyname){
    
    date_default_timezone_set('Asia/Calcutta');
   
    $year=date('Y');
    $month=date('m');
    $date=date('d');
    
    $imageFolder = "workreportimages";
    //dynamic folders creation starts
   $startFolder = "../../$imageFolder/";
    $folder = $startFolder;
    if(!file_exists($folder)){
        mkdir($folder);
    }
    
    $folder = "../../$imageFolder/$companyname/";
    if(!file_exists($folder)){
        mkdir($folder);
    }
                
    $folder = "../../$imageFolder/$companyname/$year/";
    if(!file_exists($folder)){
        mkdir($folder);
    }
                
    $folder = "../../$imageFolder/$companyname/$year/$month/";
    if(!file_exists($folder)){
        mkdir($folder);
    }
    
    $folder = "../../../$imageFolder/$companyname/$year/$month/$date/";
    if(!file_exists($folder)){
        mkdir($folder);
    }
    
    $datetime=date('YmdHis');
    if(empty($data)){
    echo $filename;
    }
    $imageName = $filename.".jpg";
    $iamgePath = $folder.$imageName;

    if(!file_exists($iamgePath)){
        $imageName = $filename."1.jpg";
        $iamgePath = $folder.$imageName;
    }
    

    $imageData = base64_decode($data);
    $source = imagecreatefromstring($imageData);
    //$rotate = imagerotate($source, $angle, 0); // if want to rotate the image
    $imageSave = imagejpeg($source,$iamgePath,100);
    imagedestroy($source);
    
    return str_replace($startFolder,"",$iamgePath);
} 

function uploadImage2($filename,$data){
    
    date_default_timezone_set('Asia/Calcutta');
    $folder = "../../fieldcloudimages/";
    if(!file_exists($folder)){
        mkdir($folder);
    }
    $datetime=date('YmdHis');
    if(empty($data)){
    echo $filename;
    }
    $imageName = $filename.".jpg";
    $iamgePath = $folder.$imageName;

    if(!file_exists($iamgePath)){
        $imageName = $filename."1.jpg";
        $iamgePath = $folder.$imageName;
    }
    

    $imageData = base64_decode($data);
    $source = imagecreatefromstring($imageData);
    $rotate = imagerotate($source, $angle, 0); // if want to rotate the image
    $imageSave = imagejpeg($rotate,$iamgePath,100);
    imagedestroy($source);
    return $imageName;
} 

function uploadImage2_c($filename,$data,$companyname){
    
    
    date_default_timezone_set('Asia/Calcutta');
    $imageFolder = "fieldcloudimages";
   
    $year=date('Y');
    $month=date('m');
    $date=date('d');
    
    //dynamic folders creation starts
   
     //dynamic folders creation starts
   $startFolder = "../../$imageFolder/";
    $folder = $startFolder;
    if(!file_exists($folder)){
        mkdir($folder);
    }
    
    
    $folder = "../../$imageFolder/$companyname/";
    if(!file_exists($folder)){
        mkdir($folder);
    }
                
    $folder = "../../$imageFolder/$companyname/$year/";
    if(!file_exists($folder)){
        mkdir($folder);
    }
                
    $folder = "../../$imageFolder/$companyname/$year/$month/";
    if(!file_exists($folder)){
        mkdir($folder);
    }
    
    $folder = "../../$imageFolder/$companyname/$year/$month/$date/";
    if(!file_exists($folder)){
        mkdir($folder);
    }
    
    $datetime=date('His');
    if(empty($data)){
    echo $filename;
    }
    $imageName = $filename.".jpg";
    $iamgePath = $folder.$imageName;

    if(!file_exists($iamgePath)){
        $imageName = $filename."1.jpg";
        $iamgePath = $folder.$imageName;
    }
    

    $imageData = base64_decode($data);
    $source = imagecreatefromstring($imageData);
    //$rotate = imagerotate($source, $angle, 0); // if want to rotate the image
    $imageSave = imagejpeg($source,$iamgePath,100);
    imagedestroy($source);
    
    
     return str_replace($startFolder,"",$iamgePath);
} 

}
?>