php echo中的OnError图像处理


OnError image handling within php echo

我试图处理从omdbapi.com拉404图像,但我的代码不工作。我正在循环遍历一个数组来构建一个电影列表。

echo '<a href="'.$oIMDB->getUrl().'" target="_new"><img class="img-responsive img-thumbnail posterImage" onError='"this.onError=null;this.src='error.png'; this.onclick=null;'" src="https://img.omdbapi.com/?i=' . $user['imdbID'] . '&apikey=secret" height="300"></a>';

当我检查在Chrome中的代码,它看起来像这样:

<img class="img-responsive img-thumbnail posterImage" this.onclick="null;'&quot;" height="300" data-pagespeed-url-hash="282594047" src="https://img.omdbapi.com/?i=tt5898034&amp;apikey=secret" onerror="this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);" data-pagespeed-lazy-replaced-functions="1">

首先,我认为问题出在echo中的单/双引号上,但后来我看到在onerror中间出现了lazyload。

是懒惰负载导致的问题?我并没有故意在我的页面中加入lazyload。也许当包含jQuery时它是默认打开的?

任何输入将不胜感激!

function imageError(e){
	e.setAttribute("src","error.png");
	e.removeAttribute("onError");
	e.removeAttribute("onclick");
}

将JavaScript代码添加到您的项目中并编辑PHP:

echo '<a href="'.$oIMDB->getUrl().'" target="_new"><img class="img-responsive img-thumbnail posterImage" onError="imageError(this)" src="https://img.omdbapi.com/?i=' . $user['imdbID'] . '&apikey=secret" height="300"></a>';

你可以这样编辑PHP(不添加自定义JavaScript函数):

echo '<a href="'.$oIMDB->getUrl().'" target="_new"><img class="img-responsive img-thumbnail posterImage" onError="this.setAttribute("src","error.png");this.removeAttribute("onError");this.removeAttribute("onclick");" src="https://img.omdbapi.com/?i=' . $user['imdbID'] . '&apikey=secret" height="300"></a>';
   $t='<img src="img/1facemale.jpg"onerror="this.onerror=null;this.src=''img/facemale.jpg'';">
   ';
   echo $t;