PHP code example of moe-mizrak / laravel-google-text-to-speech

1. Go to this page and download the library: Download moe-mizrak/laravel-google-text-to-speech library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

moe-mizrak / laravel-google-text-to-speech example snippets


return [
    'driver' => env('GOOGLE_TEXT_TO_SPEECH_DRIVER', TextToSpeechDriverType::GEMINI->value), // Options: 'gemini', 'cloud'
    'api_endpoint' => env('GOOGLE_TEXT_TO_SPEECH_API_ENDPOINT', 'generativelanguage.googleapis.com'), // For Gemini API use 'generativelanguage.googleapis.com', for Google Cloud API use 'texttospeech.googleapis.com'
    'cloud' => [
        'credentials' => env('GOOGLE_TEXT_TO_SPEECH_CREDENTIALS'), // The path to the Google Cloud credentials JSON file.
    ],
    'gemini' => [
        'api_key' => env('GOOGLE_GEMINI_API_KEY'), // Your Gemini API key
        'model' => env('GOOGLE_GEMINI_MODEL', 'gemini-2.5-flash-preview-tts'), // The Gemini model to use for Text-to-Speech synthesis.
        'temperature' => env('GOOGLE_GEMINI_TEMPERATURE', 0.85),
    ],
];

$textData = new GeminiTextData(
    text: 'Laplace Demon: the hypothetical entity that, with perfect knowledge of the present, could predict all future events based on causal determinism.',
);

$voiceData = new GeminiVoiceData(
    voiceName: 'Algieba',
    modelName: 'gemini-2.5-flash-preview-tts',
);

$audioConfigData = new GeminiAudioConfigData;

$geminiSynthesizeData = new GeminiSynthesizeData(
    $textData,
    $voiceData,
    $audioConfigData,
);

$response = GoogleTextToSpeech::synthesizeSpeech($geminiSynthesizeData);

  file_put_contents('output.pcm', $response);
  

$textData = new CloudTextData(
    text: 'Laplace Demon: the hypothetical entity that, with perfect knowledge of the present, could predict all future events based on causal determinism.',
    isSsml: false,
);

$voiceData = new CloudVoiceData(
    languageCode: 'en-US',
    voiceName: 'en-US-Wavenet-D',
);

$audioConfigData = new CloudAudioConfigData(
    audioEncoding: AudioEncoding::MP3,
);

$cloudSynthesizeData = new CloudSynthesizeData(
    $textData,
    $voiceData,
    $audioConfigData
);

$response = GoogleTextToSpeech::synthesizeSpeech($cloudSynthesizeData);

  file_put_contents('output.mp3', $response);
  

$response = GoogleTextToSpeech::listVoices(languageCode: 'en-US');
bash
  php artisan vendor:publish --tag="laravel-google-text-to-speech"