Php 是否有一种方法可以自动使用另一个图像作为站点范围内丢失图像的临时占位符?

Php 是否有一种方法可以自动使用另一个图像作为站点范围内丢失图像的临时占位符?,php,image,placeholder,Php,Image,Placeholder,我正在建设一个网站,但现在它有几个图像,我还没有实际的图像。由于这个网站有成千上万的图片或图片应该出现的地方,我不想手动更改每个图片,然后在找到正确的图片后再次更改它们。有没有办法创建一个函数来查找丢失的图像并用指定的图像替换它们,直到找到正确的图像 更新:因为我仍然有点困惑,甚至在哪里放置这个函数,我将添加一个页面的代码,我需要这个,然后也许有人可以帮我找到如何放置它 以下是其中一页的代码: <?php require_once('dbconnection.php'); mysqli_s

我正在建设一个网站,但现在它有几个图像,我还没有实际的图像。由于这个网站有成千上万的图片或图片应该出现的地方,我不想手动更改每个图片,然后在找到正确的图片后再次更改它们。有没有办法创建一个函数来查找丢失的图像并用指定的图像替换它们,直到找到正确的图像

更新:因为我仍然有点困惑,甚至在哪里放置这个函数,我将添加一个页面的代码,我需要这个,然后也许有人可以帮我找到如何放置它

以下是其中一页的代码:

<?php
require_once('dbconnection.php');
mysqli_select_db($conn, $dbname);
$query_master = "SELECT DISTINCT * FROM `master_list` INNER JOIN types_join ON master_list.join_id=types_join.join_id WHERE `type_id` = 171 ORDER BY `item_id`";
$master = mysqli_query($conn, $query_master) or die(mysqli_error());
$row_master = mysqli_fetch_assoc($master);
$totalrows_master = mysqli_num_rows($master);
?>

<!doctype html>
<html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flower Trees</title>
<link href="/css/styles.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" type="image/png" href="img/favicon.png">
</head>

<body>

<div class="wrapper">
    <a id="top"></a>
    <?php require_once('header.php'); ?>
    <?php require_once('nav.php'); ?>
    <div class="category"><h2>Flower Trees</h2></div>           
    <div class="display">
      <?php do { ?>
      <ul>
          <li><a href="details.php?recordID=<?php echo $row_master['master_id']; ?>"><img class="thumb" src="img/<?php echo $row_master['img']; ?>"/>
          <br />
          <span class="name"><?php echo $row_master['name']; ?></span></a></li>
      </ul>
      <?php } while ($row_master = mysqli_fetch_assoc($master)); ?>
    <!-- end .display --></div>
    <?php
    mysqli_free_result($master);
    ?>
    <?php require_once('footer.php'); ?>
<!-- end .wrapper --></div>

<script>
function myFunction() {
  var x = document.getElementById("myTopnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
</script>
</body>
</html>

花树


函数myFunction(){
var x=document.getElementById(“myTopnav”);
如果(x.className==“topnav”){
x、 类名+=“响应”;
}否则{
x、 className=“topnav”;
}
}

使用jQuery,当出现错误时,您可以使用全局
$('img')
处理程序轻松完成一些事情。之后只需将它们与占位符图像交换即可

$('img')。在('error',function()上{
const oldSrc=encodeURIComponent($(this.attr('src'));
$(this.attr('src')`https://via.placeholder.com/300/000000/FFFFFF/?text=${oldSrc}`);
});



使用jQuery,当出现错误时,您可以使用全局
$('img')
处理程序轻松完成一些事情。之后只需将它们与占位符图像交换即可

$('img')。在('error',function()上{
const oldSrc=encodeURIComponent($(this.attr('src'));
$(this.attr('src')`https://via.placeholder.com/300/000000/FFFFFF/?text=${oldSrc}`);
});


我不想手动更改它们中的每一个,然后在找到正确的图像后再次更改它们。有没有办法创建一个函数来查找丢失的图像并用指定的图像替换它们,直到找到正确的图像

这种函数可以写成:

function im($imgName) {
    $pathToImgs = "images/";
    if (file_exists( $pathToImgs . $imgName )) {
        echo $pathToImgs . $imgName;
    }
    else {
        echo $pathToImgs . "placeholder.jpg";
    }
}
然后在html中:

<img src="<?php im("product1.jpg"); ?>">
<img src="<?php im("product2.jpg"); ?>">
<img src="<?php im("product3.jpg"); ?>">
“>
">
">
首先

***编辑1:

给定代码,其中显示:

<img class="thumb" src="img/<?php echo $row_master['img']; ?>"/>
“/>
如果目标图像根本不存在,可以使用插入占位符图像的条件对其进行修改

<img class="thumb" src="<?php 

    if (file_exists("img/" . $row_master['img'])) {
        echo "img/" . $row_master['img'];
    }
    else {
        echo 'img/placeholder.jpg';
    }

?>">
“>
您可以通过将条件转换为php函数来重用此功能,如上所述

我不希望必须手动更改每个图像,然后在找到正确的图像后再次更改它们。是否有方法创建一个函数来查找丢失的图像并用指定的图像替换它们,直到找到正确的图像

这种函数可以写成:

function im($imgName) {
    $pathToImgs = "images/";
    if (file_exists( $pathToImgs . $imgName )) {
        echo $pathToImgs . $imgName;
    }
    else {
        echo $pathToImgs . "placeholder.jpg";
    }
}
然后在html中:

<img src="<?php im("product1.jpg"); ?>">
<img src="<?php im("product2.jpg"); ?>">
<img src="<?php im("product3.jpg"); ?>">
“>
">
">
首先

***编辑1:

给定代码,其中显示:

<img class="thumb" src="img/<?php echo $row_master['img']; ?>"/>
“/>
如果目标图像根本不存在,可以使用插入占位符图像的条件对其进行修改

<img class="thumb" src="<?php 

    if (file_exists("img/" . $row_master['img'])) {
        echo "img/" . $row_master['img'];
    }
    else {
        echo 'img/placeholder.jpg';
    }

?>">
“>

您可以通过将条件转换为php函数(如上文所述)来重用此功能。

因为这就像一个
foreach
循环一样简单,而不是在网页上散布大量图像,所以您可以使用以下方法:

$image = file_exists('img/' . $row_master['img']) ? 'img/' . $row_master['img'] : 'placeholder.png';
完整代码:

<?php
require_once('dbconnection.php');
mysqli_select_db($conn, $dbname);
$query_master = "SELECT DISTINCT * FROM `master_list` INNER JOIN types_join ON master_list.join_id=types_join.join_id WHERE `type_id` = 171 ORDER BY `item_id`";
$master = mysqli_query($conn, $query_master) or die(mysqli_error());
$row_master = mysqli_fetch_assoc($master);
$totalrows_master = mysqli_num_rows($master);
?>

<!doctype html>
<html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flower Trees</title>
<link href="/css/styles.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" type="image/png" href="img/favicon.png">
</head>

<body>

<div class="wrapper">
    <a id="top"></a>
    <?php require_once('header.php'); ?>
    <?php require_once('nav.php'); ?>
    <div class="category"><h2>Flower Trees</h2></div>           
    <div class="display">
      <?php do { 
          $image = file_exists('img/' . $row_master['img']) ? 'img/' . $row_master['img'] : 'placeholder.png';
      ?>
      <ul>
          <li><a href="details.php?recordID=<?php echo $row_master['master_id']; ?>"><img class="thumb" src="<?php echo $image; ?>"/>
          <br />
          <span class="name"><?php echo $row_master['name']; ?></span></a></li>
      </ul>
      <?php } while ($row_master = mysqli_fetch_assoc($master)); ?>
    <!-- end .display --></div>
    <?php
    mysqli_free_result($master);
    ?>
    <?php require_once('footer.php'); ?>
<!-- end .wrapper --></div>

<script>
function myFunction() {
  var x = document.getElementById("myTopnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
</script>
</body>
</html>

花树


函数myFunction(){
var x=document.getElementById(“myTopnav”);
如果(x.className==“topnav”){
x、 类名+=“响应”;
}否则{
x、 className=“topnav”;
}
}

由于这就像一个
foreach
循环一样简单,而不是在网页上散布成吨的图像,因此您可以使用以下方法:

$image = file_exists('img/' . $row_master['img']) ? 'img/' . $row_master['img'] : 'placeholder.png';
完整代码:

<?php
require_once('dbconnection.php');
mysqli_select_db($conn, $dbname);
$query_master = "SELECT DISTINCT * FROM `master_list` INNER JOIN types_join ON master_list.join_id=types_join.join_id WHERE `type_id` = 171 ORDER BY `item_id`";
$master = mysqli_query($conn, $query_master) or die(mysqli_error());
$row_master = mysqli_fetch_assoc($master);
$totalrows_master = mysqli_num_rows($master);
?>

<!doctype html>
<html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flower Trees</title>
<link href="/css/styles.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" type="image/png" href="img/favicon.png">
</head>

<body>

<div class="wrapper">
    <a id="top"></a>
    <?php require_once('header.php'); ?>
    <?php require_once('nav.php'); ?>
    <div class="category"><h2>Flower Trees</h2></div>           
    <div class="display">
      <?php do { 
          $image = file_exists('img/' . $row_master['img']) ? 'img/' . $row_master['img'] : 'placeholder.png';
      ?>
      <ul>
          <li><a href="details.php?recordID=<?php echo $row_master['master_id']; ?>"><img class="thumb" src="<?php echo $image; ?>"/>
          <br />
          <span class="name"><?php echo $row_master['name']; ?></span></a></li>
      </ul>
      <?php } while ($row_master = mysqli_fetch_assoc($master)); ?>
    <!-- end .display --></div>
    <?php
    mysqli_free_result($master);
    ?>
    <?php require_once('footer.php'); ?>
<!-- end .wrapper --></div>

<script>
function myFunction() {
  var x = document.getElementById("myTopnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
</script>
</body>
</html>

花树


函数myFunction(){
var x=document.getElementById(“myTopnav”);
如果(x.className==“topnav”){
x、 类名+=“响应”;
}否则{
x、 className=“topnav”;
}
}

是否必须在网站上的每张图片上都显示这些内容?我说的是一个有数千张图片的网站。就像我说的:这是一个全局脚本。将其添加到页面中一次,将拾取所有
$('img')
标记。该站点有数千张图像还是将有数千张图像?另外,你目前获取页面上图像的方法是什么?手动或自动,例如,在文件名已知的图像阵列上迭代?还是其他方法?@Getset,正在从数据库调用图像。这些表具有数千个图像的潜在链接,但许多实际图像还没有。@GetSet它只是为页面上呈现的每个
启动该函数。当它出错时,将启动此函数,并用占位符图像替换源。是否必须将其放在站点上的每个图像上?我说的是一个有数千张图片的网站。就像我说的:这是一个全局脚本。将其添加到页面中一次,将拾取所有
$('img')
标记。该站点有数千张图像还是将有数千张图像?另外,你目前获取页面上图像的方法是什么?手动或自动,例如,在文件名已知的图像阵列上迭代?还是其他方法?@Getset,正在从数据库调用图像。这些表具有数千个图像的潜在链接,但许多实际图像还没有。@GetSet它只是为页面上呈现的每个
启动该函数。当它出错时,这个函数被激活,并用占位符图像替换源代码。为了安全起见,我实际上复制了你的代码,并在单独保存我的代码后将其放在我的位置。这个代码没有改变任何东西,它改变了。您只需要创建