baidut / markit

a bookmark-sharing website
http://markit.sinaapp.com/
MIT License
1 stars 2 forks source link

默认图片加载问题 #42

Closed baidut closed 8 years ago

baidut commented 8 years ago

php后台处理比较麻烦,需要检查图片是否存在,而file_exists方法有目录权限限制问题,常常在存在文件时返回不存在。使用$_SERVER['DOCUMENT_ROOT']可解决SAE端问题,本地仍然是file_exists返回不存在。

<?php 
// SAE
if( file_exists($_SERVER['DOCUMENT_ROOT'].'/img/'.$th->theme_name.'.jpg')) $img_src = base_url('/img/'.$th->theme_name.'.jpg'); 
 // local still not work
else if( file_exists($_SERVER['DOCUMENT_ROOT'].'/baidut/markit/src/img/'.$th->theme_name.'.jpg')) $img_src = base_url('/img/'.$th->theme_name.'.jpg'); 
else $img_src = base_url('/img/default.jpg');

js图片添加属性 onerror="this.src='Default.jpg'" 可解决 html的object标签,直接加载default,不能解决

<object class="img-responsive" data="<?php echo base_url('/img/default.jpg'); ?>" type="image/jpg">
  <img class="img-responsive" src="<?php echo base_url('/img/'.$th->theme_name.'.jpg'); ?>" alt="<?php echo urlencode($th->theme_name) ?>">
</object>

参考资料: http://stackoverflow.com/questions/7995080/html-if-image-is-not-found http://stackoverflow.com/questions/980855/inputting-a-default-image-in-case-the-src-arribute-of-an-html-img-is-not-valid http://stackoverflow.com/questions/30011955/check-whether-an-image-exists-if-not-set-a-default-image-using-javascript