有人知道一种方式,我怎么能知道加载的图像是不是图像,因为例如返回了404错误?
如果出现错误,我想将我的图片网址设置为例如白色图片.
我不能使用像404错误处理或servlet这样的服务器端技术,因为我想用Amazon S3提供我的图像,它没有错误处理或重定向到"默认文件",以防请求的图像不是找到.
我试图替换未找到的图像,或宽度或高度为1px的图像.我设法在PHP中执行此操作:
$imgName = $resultList->IMAGE1; // ex http://www.domain.com/image.png
if (@file_get_contents($imgName) && $imgName !== 'h') {
// detects the image size (NOT based on width and height attributes)
$imgNamearray = getimagesize($imgName);
$size = $imgNamearray[3];
if ($size == 'width="1" height="1"') {
$imgName = $base_path . 'uploads/no-img.jpg';
}
} else {
$imgName = $base_path . 'uploads/no-img.jpg';
}
Run Code Online (Sandbox Code Playgroud)
我在jQuery中尝试了这段代码:
$('body').append('<div id="init"><img id="myimg" src="http://www.google.co.uk/intl/en_com/images/srpr/logo1w.png" /></div>');
$('#init').hide();
$('#myimg').bind("load",function(){
alert(this.height)
})
Run Code Online (Sandbox Code Playgroud)
这里可以找到一个小提琴http://jsfiddle.net/WMpSy/但我是jQuery的菜鸟,请帮助我.非常感谢.
我有很多图像.喜欢:
<img src="src1"/>
<img src="src2"/>
<img src="src3"/>
Run Code Online (Sandbox Code Playgroud)
有些图像可能不存在.因此浏览器将显示损坏的图像图片.这很难看.如何使用Javascript而不是jQuery来确定图像是否存在?如果没有,请提供本地图像以替换它.谢谢.
我尝试了代码,它取代了所有不仅存在的图像.
function imgError(image) {
image.onerror = "";
image.src = "http://www.hi-pda.com/forum/uc_server/data/avatar/000/85/69/99_avatar_middle.jpg?random=10.20420048222877085";
return true;
}
var imgs = document.getElementsByTagName("img");
for (var i = 0; i < imgs.length; i++) {
imgs[i].onerror=imgError(imgs[i]);
}
Run Code Online (Sandbox Code Playgroud)
图像是
<img src="rrrrr" />
<img src="http://www.hi-pda.com/forum/uc_server/data/avatar/000/52/56/39_avatar_middle.jpg"/>
Run Code Online (Sandbox Code Playgroud)
第一张图片不存在,第二张图片存在.当我在chrome中运行时,所有图片都被替换了......