我正在编写一个属性门户网站.我坚持检查图像.我知道如何检查是否设置了图像网址.但问题是检测网址上是否确实存在有效图像.
例如:http://property.images.themovechannel.com/cache/7217/6094437/img_main.jpg
此图片网址已存在但图片现已删除,因此它只在我的搜索页面中显示为空白.有没有办法检查网址上是否有图像,然后显示占位符(如果它不存在).
就像是
$imageURL = "http://property.images.themovechannel.com/cache/7217/6094437/img_main.jpg";
if (exists($imageURL)) { display image }
else { display placeholder }
Run Code Online (Sandbox Code Playgroud)
但所有这一切都是检查网址是否存在,它确实存在那里没有图像
提前致谢
use*_*016 25
使用getimagesize()
以确保网址指向有效的图像.
if (getimagesize($imageURL) !== false) {
// display image
}
Run Code Online (Sandbox Code Playgroud)
function exists($uri)
{
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $code == 200;
}
Run Code Online (Sandbox Code Playgroud)