Skip to content

Instantly share code, notes, and snippets.

@arthursoares
Created June 30, 2014 17:49
Show Gist options
  • Save arthursoares/9b4f9054fbf8c013fe96 to your computer and use it in GitHub Desktop.
Save arthursoares/9b4f9054fbf8c013fe96 to your computer and use it in GitHub Desktop.
<?php
$text = "Olá, como vai você ?";
$lang = "pt-BR";
// Name of the MP3 file generated using the MD5 hash
// Added things to prevent bug if you want the same sentence in two different languages
$file = md5($lang."?".urlencode($text));
// Save the MP3 file in this folder with the .mp3 extension
$file = "audio/" . $file .".mp3";
// Verify if folder exists, if it doesn't, create it, if exists, verify CHMOD
if(!is_dir("audio/"))
mkdir("audio/");
else
if(substr(sprintf('%o', fileperms('audio/')), -4)!="0777")
chmod("audio/", 0777);
// If the MP3 file exists, do not create a new request
if (!file_exists($file))
{
// Download the content
$mp3 = file_get_contents(
'http://translate.google.com/translate_tts?ie=UTF-8&q='. urlencode($text) .'&tl='. $lang .'&total=1&idx=0&textlen=5&prev=input');
file_put_contents($file, $mp3);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment