Php 我可以隐藏损坏的图像吗?

Php 我可以隐藏损坏的图像吗?,php,html,css,wordpress,Php,Html,Css,Wordpress,我正在尝试创建一个边栏,我可以使用自定义字段在我的wordpress cms的后端指定图像,现在我已经让它工作了,只有一个小错误,如果用户输入一个无效的URL,图像链接将显示为断开,不会显示,有没有办法隐藏断开的图像图标 我为父DIV元素设置了一个背景图像,这样如果没有要显示的图像,父DIV元素的背景就会显示出来 以下是PHP代码: //here I get the 'side_image' custom field, which will contain the URL to the side

我正在尝试创建一个边栏,我可以使用自定义字段在我的wordpress cms的后端指定图像,现在我已经让它工作了,只有一个小错误,如果用户输入一个无效的URL,图像链接将显示为断开,不会显示,有没有办法隐藏断开的图像图标

我为父DIV元素设置了一个背景图像,这样如果没有要显示的图像,父DIV元素的背景就会显示出来

以下是PHP代码:

//here I get the 'side_image' custom field, which will contain the URL to the side image    
if (have_posts()) : 
         while (have_posts()) : the_post(); 
             $side = get_post_meta($post->ID, 'side_image', true); 
         endwhile;
 endif;
HTML:


先走一步

您可以尝试以下方法

<!--here is the HTML markup-->
<div id="inner_content_right">
    <img src="<?php if (@getimagesize($side)) echo $side; ?>" />
</div>

" />

Thanx伙计们,我用这个代码得到了它

//check if the string is a valid URL
function checkURL($url)
{
    return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}

//returns a image with a valid URL or nothing at all
function validateImage($one){
if(!checkURL($one))
{
    $errMsg .= "Please enter valid URL including http://";
    //return $errMsg;
} else {
    $headers = get_headers($one, 1);
    $return = $headers[0];
    if($return!='HTTP/1.1 404 Not Found'){
        $string = "<img src='$one' />";
        return $string;
    }
    }
}
//检查字符串是否为有效的URL
函数checkURL($url)
{
返回preg_match(“^http(s)”:/[a-z0-9-]+([a-z0-9-]+)*(:[0-9]+)?(/.*)$i',$url);
}
//返回包含有效URL的图像,或者根本不返回任何内容
函数validateImage($1){
如果(!checkURL($1))
{
$errMsg.=“请输入有效的URL,包括http://”;
//返回$errMsg;
}否则{
$headers=get_headers($1,1);
$return=$headers[0];
如果($return!='HTTP/1.1 404未找到'){
$string=“”;
返回$string;
}
}
}

谢谢你的帮助!

@fabrik我也是。但在这种情况下,整个问题都是关于抑制错误情况,以便容错检查例程看起来是正确的。如果它中断,图片就不会显示,而不是某种gd错误消息。至少对于博客的访问者来说,这是一种更愉快的体验:)
<!--here is the HTML markup-->
<div id="inner_content_right">
    <img src="<?php if (@getimagesize($side)) echo $side; ?>" />
</div>
//check if the string is a valid URL
function checkURL($url)
{
    return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}

//returns a image with a valid URL or nothing at all
function validateImage($one){
if(!checkURL($one))
{
    $errMsg .= "Please enter valid URL including http://";
    //return $errMsg;
} else {
    $headers = get_headers($one, 1);
    $return = $headers[0];
    if($return!='HTTP/1.1 404 Not Found'){
        $string = "<img src='$one' />";
        return $string;
    }
    }
}