<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('image_thumb')){
function image_thumb($courseBanner, $userId){
$CI =& get_instance();
$filename_ext = pathinfo($courseBanner, PATHINFO_EXTENSION);
$course_thumb_Banner = preg_replace('/^(.*)\.' . $filename_ext . '$/', '$1_thumb.' . $filename_ext, $courseBanner);
$url = base_url()."private/".$userId."/".$course_thumb_Banner;
$headers=get_headers($url);
$is_banner_there = stripos($headers[0],"200 OK")?true:false;
$url = ($is_banner_there)?$url: base_url()."public/images/placeholder.jpg";
return $url;
}
}
在上面的代码中,有时$ courseBanner没有来,所以我想发送这个base_url()."public/images/placeholder.jpg
怎么办?
答案 0 :(得分:1)
建议:
我认为如果字符串(例如$ courseBanner)为空,您可以设置默认值
示例:强>
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('image_thumb')){
function image_thumb($userId, $courseBanner = ''){
$CI =& get_instance();
$courseBanner = !empty($courseBanner) ? $courseBanner : base_url()."public/images/placeholder.jpg";
$filename_ext = pathinfo($courseBanner, PATHINFO_EXTENSION);
$course_thumb_Banner = preg_replace('/^(.*)\.' . $filename_ext . '$/', '$1_thumb.' . $filename_ext, $courseBanner);
$url = base_url()."private/".$userId."/".$course_thumb_Banner;
$headers=get_headers($url);
$is_banner_there = stripos($headers[0],"200 OK")?true:false;
return $url;
}
}
这仅供参考,希望对此有所帮助