显示图片的curl,是不是有bug?

图片显示类

namespace app\mp\controller;
class Show
{
/**
* @param string $url
*/
public function image($url = '')
{

$url = urldecode($url);
if (!empty($Arr = explode('wx_fmt=', $url))) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$file = curl_exec($ch);
curl_close($ch);
$types = array(
'gif' => 'image/gif',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'png' => 'image/png',
);
foreach ($Arr as $value) {
if (in_array($value, $types)) {
$type = $types[$value];
} else {
$type = 'image/jpeg';
}
}
header("Content-type: " . $type);
echo $file;
exit();
}
}
}


//$Arr 数组的第一个值应该是url 第二个值应该是这张图片的类型
下面这个循环中 in_array() 只能判断一个$value 是否在数组$types的value中不能判断是否在key中 所以最后的结果 $type = 'image/jpeg';
foreach ($Arr as $value) {
if (in_array($value, $types)) {
$type = $types[$value];
} else {
$type = 'image/jpeg';
}
}
是否应该改为这样:
if(key_exists($Arr[1],$types)){
$type = $types[$Arr[1]];
}else{
$type = 'image/jpeg';
}
已邀请:

wxjing - 90后IT男...

赞同来自:

版本:GitHub v1.5.0

geeson

赞同来自:

多谢反馈,你这个方法是可行的!

要回复问题请先登录注册