<?php if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}



if (!function_exists('send_text_msg_new')) {
    
   function send_text_msg_new($msg)
    {
        
        $url=$msg;    
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$send_details = curl_exec($ch);
		curl_close($ch);
		if(!empty($send_details)){
			//   if(!empty($send_details)){
			$status = json_encode($send_details) ;
			// return $status->status ;
			return $status ;
			// return $url ; 
		}else{
			$json_data = file_get_contents($url);
			$response_data = json_encode($json_data);
			if(!empty($response_data)){
				return $response_data; 
			}else{
				return $url ;
			}
		}
		// return $ddddddd ;  

    }

}



if (!function_exists('send_text_msg')) {
    function send_text_msg($msg)
    {
       
    
            $before = substr($msg, 0, strpos($msg, "message="));
                
            $after = substr($msg, strpos($msg, "&message=") + 1); 

            $string = $after ;

            $remove = 'message=' ;
            $now = trim(str_replace($remove,'',$string));

            // $url = $before. $remove . urlencode($now) ;
            $url = $before.$remove.urlencode($now) ;
          
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $send_details = curl_exec($ch);
            curl_close($ch);
            
           

                    if(!empty($send_details)){
                        //   if(!empty($send_details)){
                        $status = json_decode($send_details) ;
                        // return $status->status ;
                        return $status ;
                        // return $url ; 
                    }else{
            $json_data = file_get_contents($url);

            $response_data = json_decode($json_data);

            if(!empty($response_data)){
                // $status = $response_data->status ;
                
                        return $response_data; 
                
            }
            else{
            return $url ;

            }

                    }


         return $ddddddd ;  

    }
}


    function convert_to_webp($source_path ,$destination_path) {
     
        // $source_path = './upload/source_img.jpeg'; // Path to the source JPG file
        // $destination_path = './upload/converted_image.webp'; // Path to save the converted WebP file

        if ($this->convert_jpg_to_webp($source_path, $destination_path)) {
            echo "Image converted successfully!";
        } else {
            echo "Image conversion failed.";
        }
    }

    function convert_jpg_to_webp($source, $destination) {
      
        try {
            $image = new Imagick($source);
            $image->setImageFormat('webp');

            // Adjust the quality if necessary (0 to 100)
            $image->setImageCompressionQuality(80);

            // Write the image to the destination path
            $image->writeImage($destination);

            // Clear resources
            $image->clear();
            $image->destroy();

            return true;
        } catch (Exception $e) {
            log_message('error', $e->getMessage());
            return false;
  }
       }
       
       
    function convert_to_webp_image($input_path, $output_path) {
        $image = imagecreatefromstring(file_get_contents($input_path));
        if ($image === false) {
            return false;
        }
    
        // Save as WebP
        $result = imagewebp($image, $output_path, 100);
        imagedestroy($image);
        return $result;
    }


function sendNotificationapp($token,$body,$title,$notificaton_id='',$app_url=''){
    //$url="https://fcm.googleapis.com/v1/projects/bitsmultiservice/messages:send";
    $url="https://asatvindia.in:5001/api/notification";

    
    $fields=array(
        "message"=>array("token"=>$token,
          "data"=>array(
          // "notificaton_id"=>$notificaton_id,
           "title"=>$title,
           "body"=>$body,
           "channel_id"=>"ride123",
           //"url"=>$app_url,
           )
        )
    );
        
    
    $headers=array(
        'Content-Type:application/json'
    );

    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST,true);
    curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($fields));
    $result=curl_exec($ch);
    //print_r($result);
    curl_close($ch);
}


function sendNotificationFlutter($token,$body,$title,$notificaton_id='',$app_url=''){
    //$url="https://fcm.googleapis.com/v1/projects/bitsmultiservice/messages:send";
    $url="https://asatvindia.in:5001/api/notification";

    
    $fields=array(
        "message"=>array("token"=>$token,
          "notification"=>array(
           "title"=>$title,
           "body"=>$body
           )
        )
    );
        
    
    $headers=array(
        'Content-Type:application/json'
    );

    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST,true);
    curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($fields));
    $result=curl_exec($ch);
    curl_close($ch);
}
