請注意,我不是說它是否打算在那里。我的意思是如果找不到/定位影像。
例如下面的第一個,如果 monoph.png 不存在有沒有辦法顯示占位符?
if ($d == 1 && $h >= 1 && $h < 14) $img = 'img/hosts/monophy.png';
else if ($d == 1 && $h >= 14 && $h < 16) $img = 'img/hosts/mon24.jpg';
else if ($d == 1 && $h >= 16 && $h < 18) $img = 'img/hosts/mon46.jpg';
else if ($d == 1 && $h >= 18 && $h < 20) $img = 'img/hosts/mon68.jpg';
else if ($d == 1 && $h >= 20 && $h < 22) $img = 'img/hosts/mon810.jpg';
else if ($d == 1 && $h >= 22 && $h < 24) $img = 'img/hosts/MON1012.png';
else if ($d == 1 && $h >= 19) $img = 'img/hosts/monophy.png';
else if ($d == 2 && $h < 0) $img = 'img/hosts/monophy.png';
uj5u.com熱心網友回復:
您可以在服務器端或客戶端或兩者都執行此操作。在服務器端,您可以使用 php 的 file_exists 函式在使用前檢查檔案是否存在。
https://www.php.net/manual/en/function.file-exists.php
在客戶端,您可以使用 img 標簽 one rror 屬性來顯示后備影像。
jQuery/JavaScript 替換損壞的影像
uj5u.com熱心網友回復:
我在您的原始代碼中添加了幾行,現在如果該檔案不存在,它將回退到您的后備影像。
// First I initialize $img with an empty string
$img = '';
// Your block of code
if ($d == 1 && $h >= 1 && $h < 14) {
$img = 'img/hosts/monophy.png';
} else if ($d == 1 && $h >= 14 && $h < 16) {
$img = 'img/hosts/mon24.jpg';
} else if ($d == 1 && $h >= 16 && $h < 18) {
$img = 'img/hosts/mon46.jpg';
} else if ($d == 1 && $h >= 18 && $h < 20) {
$img = 'img/hosts/mon68.jpg';
} else if ($d == 1 && $h >= 20 && $h < 22) {
$img = 'img/hosts/mon810.jpg';
} else if ($d == 1 && $h >= 22 && $h < 24) {
$img = 'img/hosts/MON1012.png';
} else if ($d == 1 && $h >= 19) {
$img = 'img/hosts/monophy.png';
} else if ($d == 2 && $h < 0) {
$img = 'img/hosts/monophy.png';
}
// Finally we check if $img exist otherwise we fallback on your fallback image
$img = file_exists($img) ? $img : 'path/to/your/fallback/image'
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/374951.html
