File: /var/www/html/text-speech/index.php
<form method="post">
<input id="txt" name="txt" type="textbox" />
<input name="submit" type="submit" value="Convert to Speech" />
</form>
<?php
if(isset($_POST['txt'])){
$txt=$_POST['txt'];
$text = urlencode($txt);
$lang = urldecode("en-IN");
$file = "audio/" . md5($text) .".mp3";
if (!file_exists($file) || filesize($file) == 0) {
$mp3 = file_get_contents('https://translate.google.co.in/translate_tts?ie=UTF-8&tl='.$lang.'&q='.$text.'&client=gtx');
if(file_put_contents($file, $mp3)){
?>
<audio controls>
<source src="<?php echo $file; ?>" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<?php
}else{
echo "Wasn't able to save it !<br>";
}
} else {
echo "Already exist<br>";
}
}
?>