k思路:调用讯飞的图片识别和语音输出的API实现图片转文字和语音
**APPID,APIKey,APISecret自己在讯飞网站注册即可**,免费使用的,填在img_audio.php文件最后对应的位置就可以,链接是我自己制作的效果。
代码如下
html里面使用到的是我的本地图片,自己根据需要修改就可以。
img_audio.html
<html>
<head>
<meta charset="utf-8">
<title>图片竟然会说话!!!</title>
<style>
.button
{
background-color: #B0C4DE;
border: none;
color: white;
padding: 15px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
border-radius: 18px;
}
.button:hover {background-color: #5F9EA0}
.button:active {
background-color: #5F9EA0;
box-shadow: 0 5px #666;
transform: translateY(4px);
}
</style>
</head>
<body background="timg.jpg">
<br>
<form action="img_audio.php" method="post" enctype="multipart/form-data" style="text-align: center">
<label style="color:white" for="file" ></label>
<p style="text-align:center;color:white;font-family:STLiti;font-size:50px">你想听见哪张图片说话呢</p>
<input type="file" class="button" name="file" id="file" style="text-align:center;font-family:STZhongsong;font-size:20px"><br>
<p style="text-align:center;color:white;font-family:STLiti;font-size:50px">你想听见哪个人的声音呢</p>
<select name="vcn" class="button" style="text-align:center;font-family:STZhongsong;font-size:20px">
<option value="xiaoyan">小燕</option>
<option value="aisjiuxu">许久</option>
<option value="aisxping">小萍</option>
<option value="aisjinger">小婧</option>
<option value="aisbabyxu">许小宝</option>
</select>
<br><br><br>
<input type="submit" class="button" name="submit" value="我选好了" style="text-align:center;font-family:STZhongsong;font-size:20px">
</form>
</body>
</html>
img_audio.php
<html>
<head>
<meta charset="utf-8">
<title>图片竟然会说话!!!</title>
<style>
.button {
background-color: #B0C4DE;
border: none;
color: white;
padding: 15px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
border-radius: 18px;
}
.button:hover {background-color: #5F9EA0}
.button:active {
background-color: #5F9EA0;
box-shadow: 0 5px #666;
transform: translateY(4px);
}
</style>
</head>
<body background="timg.jpg" style="color:white">
<h1 style="text-align:center;color:white;font-family:STLiti;font-size:50px"><b>你听,图片也会说话</b></h1>
<p style="text-align:center;">
<img src="audio.jpg" width="310" height="200";><br>
<audio controls="controls" class="button">
<source src="tts.mp3" type="audio/mp3" style="text-align:center;">
Your browser does not support the audio element.
</audio>
<h1 style="text-align:center;color:white;font-family:STLiti;font-size:50px"><b>它说的话在这里</b></h1>
</p>
<p style="text-align:center;color:white;font-family:STZhongsong;font-size:25px">
<?php
include "vendor/autoload.php";
include "vendor/textalk/websocket/lib/Base.php";
include "vendor/textalk/websocket/lib/Client.php";
include "vendor/textalk/websocket/lib/ConnectionException.php";
if(rename( $_FILES["file"]["tmp_name"],"./audio.jpg"));else echo "保存文件失败";
class img_text{
function xfyun(){
$daytime=strtotime('1970-1-1T00:00:00 UTC');
// 印刷文字识别 webapi 接口地址
$api = "http://webapi.xfyun.cn/v1/service/v1/ocr/general";
// 应用ID (必须为webapi类型应用,并印刷文字识别服务,参考帖子如何创建一个webapi应用:http://bbs.xfyun.cn/forum.php?mod=viewthread&tid=36481)
$XAppid = "5edf3ad1";
// 接口密钥(webapi类型应用开通印刷文字识别服务后,控制台--我的应用---印刷文字识别---服务的apikey)
$Apikey = "9687c297598a2c247f332e50ee09ee1f";
$XCurTime =time();
$XParam ="";
$XCheckSum ="";
// 语言类型和文本位置信息(默认不返回)
$Param= array(
"language"=>"cn|en",
"location"=>"false",
);
// 上传图片文件地址并base64位编码
$image=file_get_contents("audio.jpg");
$image=base64_encode($image);
$Post = array(
'image' => $image,
);
$XParam = base64_encode(json_encode($Param));
$XCheckSum = md5($Apikey.$XCurTime.$XParam);
// 组装http请求头
$headers = array();
$headers[] = 'X-CurTime:'.$XCurTime;
$headers[] = 'X-Param:'.$XParam;
$headers[] = 'X-Appid:'.$XAppid;
$headers[] = 'X-CheckSum:'.$XCheckSum;
$headers[] = 'Content-Type:application/x-www-form-urlencoded; charset=utf-8';
return $this->http_request($api, $Post, $headers);
}
/**
* 发送post请求
* @param string $url 请求地址
* @param array $post_data post键值对数据
* @return string
*/
function http_request($url, $post_data, $headers) {
$postdata = http_build_query($post_data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => $headers,
'content' => $postdata,
'timeout' => 15 * 60 // 超时时间(单位:s)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$result = explode('{"content":"',$result);
unset($result[0]);
//print_r($result);
$str = "";
foreach ($result as $word){
$word = explode('"}',$word);
$str = $str.$word[0];
//print_r($word);
}
return $str;
//echo $str;
// 错误码链接:https://www.xfyun.cn/document/error-code (code返回错误码时必看)
return 'success';
}
}
class text_audio{
public $APPID;
public $APIKey;
public $APISecret;
public $Text;
public $vcn;
# 初始化
function __construct($APPID, $APIKey, $APISecret, $Text,$vcn){
$this->APPID = $APPID;
$this->APIKey = $APIKey;
$this->APISecret = $APISecret;
$this->Text = $Text;
$this->vcn = $vcn;
}
# 生成url
function create_url(){
$url = 'wss://tts-api.xfyun.cn/v2/tts';
# 生成RFC1123格式的时间戳
$date = gmdate('D, d M Y H:i:s') . ' GMT';
# 拼接字符串
$signature_origin = "host: "."ws-api.xfyun.cn"."\n";
$signature_origin."date: ".$date."\n";
$signature_origin."GET "."/v2/tts "."HTTP/1.1";
# 进行hmac-sha256进行加密
$signature_origin = "host: "."ws-api.xfyun.cn"."\n";
$signature_origin = $signature_origin."date: ".$date."\n";
$signature_origin = $signature_origin."GET "."/v2/tts "."HTTP/1.1";
$signature_sha = base64_encode(hash_hmac('sha256', $signature_origin,$this->APISecret, true));
$authorization_origin = "api_key=\"$this->APIKey\", algorithm=\"hmac-sha256\", headers=\"host date request-line\", signature=\"$signature_sha\"";
$authorization = base64_encode($authorization_origin);
# 将请求的鉴权参数组合为数组
$v = [
"authorization"=> $authorization,
"date"=> $date,
"host"=> "ws-api.xfyun.cn"];
# 拼接鉴权参数,生成url
$url = $url .'?'.http_build_query($v);
return $url;
}
function get_message(){
$url = $this->create_url();
$client = new webSocket\Client($url);
$data = [
'common'=>[
'app_id'=>$this->APPID
],
'business'=>[
'aue'=>"lame", //mp3
'sfl'=>1,
'vcn'=>$this->vcn,
'tte'=>"UTF8",
],
'data'=>[
'text'=>base64_encode($this->Text),
'status'=>2
]
];
$client = new WebSocket\Client($url);
$client->send(json_encode($data));
$result = "";
while (true) {
try {
$message = json_decode($client->receive());
//echo $message->data->audio;
switch($message->data->status){
case 0: break;
case 1: $result.=base64_decode($message->data->audio);break;
case 2: $result.=base64_decode($message->data->audio);break 2;
}
// Act on received message
// Break while loop to stop listening
} catch (\WebSocket\ConnectionException $e) {
// Possibly log errors
echo "错误<br>";
sleep(3);
break;
}
}
file_put_contents("tts.mp3",$result);
$client->close();
}
}
switch($_POST["vcn"]){
case 'xiaoyan':
$vcn='xiaoyan';break;
case 'aisjiuxu':
$vcn='aisjiuxu';break;
case 'aisxping':
$vcn='aisxping';break;
case 'aisjinger':
$vcn='aisjinger';break;
case 'aisbabyxu':
$vcn='aisbabyxu';break;
}
$test = new img_text();
$Text = $test->xfyun();
echo $Text;
$APPID="*********";
$APIKey="***********************************";
$APISecret="*************************************";
#上述三个均是在讯飞网站申请的,填入自己的即可
$b = new text_audio($APPID, $APIKey, $APISecret, $Text,$vcn);
$b->get_message();
?>
</p>
</body>
</html