How to convert text to speech using PHP
In this post, you will learn how to convert text to speech using the PHP programming language.
The text-to-speech conversion process improves the customer experience. Many people like to listen instead of read, like news articles, courses, and much more. It helps to interact with users in multiple languages and tones. The website that uses TTS technology attracts the majority of customers and expands the reach of your business. Text-to-Speech allows developers to create natural-sounding, synthetic human speech as playable audio. It permits you to convert your strings, words, and sentences into the sound of a person (male or female).
There are several APIs available to convert text to speech. In this article, we will use the Google Text-To-Speech or gTTS module.
The gTTS module produces a very natural voice, like a human, while some other APIs provide voice like a robotic. It is freely available and easy to implement, as it simply converts specified text into audio, which we can save as an mp3 file. It has the capacity to read a long length of text with proper intonation, abbreviations, decimals, and more. This API also supports many languages, including English, Hindi, French, German, and much more.
First, create an 'index.php' file that we will call in the browser. In this file, we place a form element to collect text.
<form method="post">
<input id="txt" name="txt" type="textbox" />
<input name="submit" type="submit" value="Convert to Speech" />
</form>
The base url of google translator is-
http://translate.google.com/translate_tts
It has some predefined url attributes that are accepted in GET requests, like-
q- The targeted text to convert to audio,
tl- Translation language, like en-IN for English India, fr for French, it for Italian, and so on,
ie- Input Encoding format, use the default UTF-8,
oe- Output Encoding format.
Next, get the form value and find the special character, if any are in the form. After that, encode the text using rawurlencode(). Next, we will use file_get_contents() to retrieve all the data from the gtts and store it in a variable.
<?php
if(isset($_POST['txt'])){
$txt=$_POST['txt'];
$txt=htmlspecialchars($txt);
$txt=rawurlencode($txt);
$audio=file_get_contents('https://translate.google.com/translate_tts?ie=UTF-8&client=gtx&q='.$txt.'&tl=en-IN');
$speech="<audio controls='controls' autoplay></audio>";
echo $speech;
}
?>
The above code converts the given text into audio and displays something like this in the browser.
Related Articles
PHP calculate percentage of totalConvert stdclass object to array PHP
PHP count average word length and characters in a text file
PHP reverse a string without predefined function
PHP random quote generator
PHP convert string into an array
PHP remove HTML and PHP tags from string
Import Excel File into MySQL using PHP
PHP array length
Import Excel File into MySQL Database using PHP
PHP String Contains
PHP remove last character from string
Import Data Into MySQL From Excel File
How to display PDF file in PHP from database
How to read CSV file in PHP and store in MySQL
Create And Download Word Document in PHP
PHP SplFileObject Standard Library
Simple File Upload Script in PHP
Sending form data to an email using PHP
Recover forgot password using PHP and MySQL
Php file based authentication
Simple PHP File Cache
How to get current directory, filename and code line number in PHP