<?php //mp3格式 echo "http://www.codepearl.com/files/212.html";
2.demo
<?php /** * http://www.codepearl.com */ include 'php_text2speech.class.php'; $t2s = new php_text2speech; ?> <audio controls="controls" autoplay="autoplay"> <source src="<?php echo $t2s->speak('代码珠玑 , 提供编程中 需要的各种工具类 , 算法 , 函数 , 解决方案 , 技术文档 , 致力于做您最好的代码 , 分享平台',"zh-cn"); ?>" type="audio/mp3" /> </audio>
3.操作类
<?php //http://www.codepearl.com /****************************************************************** example: <?php $t2s = new php_text2speech; ?> // simple example <audio controls="controls" autoplay="autoplay"> <source src="<?php echo $t2s->speak('www.codepearl.com','en'); ?>" type="audio/mp3" /> </audio> // example use of other language <audio controls="controls" autoplay="autoplay"> <source src="<?php echo $t2s->speak('代码珠玑', 'zh-cn'); ?>" type="audio/mp3" /> </audio> ******************************************************************/ class php_text2speech { /** max text characters * @var integer */ var $maxstrlen = 100; /** text len * @var integer */ var $textlen = 0; /** no of words * @var integer */ var $wordcount = 0; /** language of text (iso 639-1) * @var string * @link https://en.wikipedia.org/wiki/list_of_iso_639-1_codes */ var $lang = 'en'; /** text to speak * @var string */ var $text = null; /** file name format * @var string */ var $mp3file = "%s.mp3"; /** directory to store audio file * @var string */ var $audiodir = "audio/"; /** contents * @var string */ var $contents = null; /** function make request to google translate, download file and returns audio file path * @param string $text - text to speak * @param string $lang - language of text (iso 639-1) * @return string - mp3 file path * @link https://en.wikipedia.org/wiki/list_of_iso_639-1_codes */ function speak($text, $lang = null) { if ($lang !== null) { $this->lang = $lang; } // create dir if not exists if (!is_dir($this->audiodir)) { mkdir($this->audiodir, 0755) or die('could not create audio dir: ' . $this->audiodir); } // try to set writing permissions for audio dir. if (!is_writable($this->audiodir)) { chmod($this->audiodir, 0755) or die('could not set appropriate permissions for audio dir: ' . $this->audiodir); } // can not handle more than 100 characters so split text if (strlen($text) > $this->maxstrlen) { $this->text = $text; // generate unique mp3 file name $file = sprintf($this->mp3file, $this->audiodir . md5($this->text)); if (!file_exists($file)) { $texts = array(); $words = explode(' ', $this->text); $i = 0; $texts[$i] = null; foreach ($words as $w) { $w = trim($w); if (strlen($texts[$i] . ' ' . $w) < $this->maxstrlen) { $texts[$i] = $texts[$i] . ' ' . $w; } else { $texts[++$i] = $w; } } // get get separated files contents and marge them into one foreach ($texts as $txt) { $pfile = $this->speak($txt, $this->lang); $this->contents .= $this->striptags(file_get_contents($pfile)); unlink($pfile); } unset($words, $texts); // save file file_put_contents($file, $this->contents); $this->contents = null; } } else { // generate unique mp3 file name $file = sprintf($this->mp3file, $this->audiodir . md5($text)); if (!file_exists($file)) { // text lenght $this->textlen = strlen($text); // words count $this->wordcount = str_word_count($text); // encode string $text = urlencode($text); // download new file $this->download("http://translate.google.com/translate_tts?ie=utf-8&q={$text}&tl={$this->lang}&total={$this->wordcount}&idx=0&textlen={$this->textlen}", $file); } } // returns mp3 file path return $file; } /** function to find the beginning of the mp3 file * @param string $contents - file contents * @return integer */ function getstart($contents) { for($i=0; $i < strlen($contents); $i++){ if(ord(substr($contents, $i, 1)) == 255){ return $i; } } } /** function to find the end of the mp3 file * @param string $contents - file contents * @return integer */ function getend($contents) { $c = substr($contents, (strlen($contents) - 128)); if(strtoupper(substr($c, 0, 3)) == 'tag'){ return $c; }else{ return false; } } /** function to remove the id3 tags from mp3 files * @param string $contents - file contents * @return string */ function striptags($contents) { // remove start $start = $this->getstart($contents); if ($start === false) { return false; } else { return substr($contents, $start); } // remove end tag if ($this->getend($contents) !== false){ return substr($contents, 0, (strlen($contents) - 129)); } } /** function to download and save file * @param string $url - url * @param string $path - local path */ function download($url, $path) { // is curl installed? if (!function_exists('curl_init')){ // use file get contents $output = file_get_contents($url); }else{ // use curl $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_autoreferer, true); curl_setopt($ch, curlopt_useragent, "mozilla/5.0 (windows; u; windows nt 5.1; rv:1.7.3) gecko/20041001 firefox/0.10.1"); curl_setopt($ch, curlopt_header, 0); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_timeout, 10); $output = curl_exec($ch); curl_close($ch); } // save file file_put_contents($path, $output); } }
