PHP:如果指定的图像不显示,如何显示默认图像';t存在


PHP: How to display a default Image if the specified one doesn't exists?

我正在做一个小项目,我需要在数据库中显示每个作者的作者图像。窥探下面的代码:

-查询

$getboth_sql = mysql_query(
"SELECT lyrics.lyrics_id, lyrics.lyrics_title, lyrics.lyrics_text,artists.artist_nane,artists.artist_id
FROM lyrics,artists
WHERE lyrics.artist_id = artists.artist_id
ORDER BY lyrics.lyrics_title");
while ($both = mysql_fetch_assoc($getboth_sql)) {
    $lyrics_id = $both[lyrics_id];
    $lyrics_title = $both[lyrics_title];
    $lyrics_text = $both[lyrics_text];
    $artist_name = $both[artist_nane];
    $artist_id = $both[artist_id];
    ?>
    <div class="artimg"><img src="images/artists/<?php echo $artist_name;?>.jpg"  height="50px" width="50px"/></div>        

    <?php
}//END While

上面的代码可以工作,但如果我没有在"艺术家"目录中保存图像,则不会显示该艺术家的图像。

<div class="artimg"><img src="images/artists/<?php echo $artist_name;?>.jpg"  height="50px" width="50px"/></div>

问题:如果找不到指定的,显示默认图像的最佳方式是什么。

我一直在玩空的和文件存在的函数,但我还没有把它做好。请帮忙。

附言:如果这个问题看起来很愚蠢,我就不是职业选手!

有很多方法可以做到这一点。

<?php
if(file_exists("images/artists/$artist_name.jpg"))
    $fileName = "$artist_name.jpg";
else
    $fileName = "default.jpg";
?>
<div class="artimg"><img src="images/artists/<?php echo $fileName;?>"  height="50px" width="50px"/></div>

磁盘访问=缓慢

你有几个选择。一种是每次需要显示文件时都要检查文件是否存在。不幸的是,这并不理想,因为磁盘读取可能会成为性能瓶颈。如果你每分钟加载数百个页面,这不是一个可行的解决方案。在高性能环境中,您希望避免尽可能多的文件统计信息:

$img_dir = '/path/to/artist/images';
$expected_file = $img_dir . '/' . $artist_name . '.jpg';
$img = file_exists($expected_file) ? $artist_name : 'default';

将名称硬编码到代码中

另一个,如果你有少数艺术家的图像,你需要显示的是硬编码的图像名称:

$authors = array('Bill Shakespeare', 'Douglas Adams', 'Ralph Ellison');
$img = in_array($artist_name, $authors) ? $artist_name : 'default';

当然,这并没有特别的帮助,因为每次你的艺术家图像目录发生变化时,你都需要编辑你的代码。

最好的主意

在这种情况下,首选选项是:

由于您已经在数据库中获取艺术家记录,请将相关的图像文件名存储在艺术家表的数据库列中。这样就可以避免多余的磁盘访问。

此方法允许您使用原始查询从数据库中检索文件名。如果该字段为空,您将知道显示默认图像。

尝试

if ( !file_exists( 'images/artists/' . $filename ) ) {
   $artist_name = 'holderthumbnail';
}

在"艺术家"目录中拥有holderthumbnail.jpg

代码:

<?php
     if(file_exists("$your_image.jpg")) $filename = "$your_image.jpg";
     else $filename = "default.jpg";
     echo "<img src='$filename'/>";
?>
<img id="currentPhoto" src="SomeImage.jpg" onerror="this.src='Default.jpg'" width="100" height="120">

试试这个方法:对于Laravel:

  <?php
                    $image = parse_url($user->image);
                    if(isset($image['host'])){
                         $image= $user->image;
                        }
                    else if($image==null){ 
                        $image= Request::root().'/uploads'.'/subsystems_icons/'.'democp.jpg';   
                    }
                    else {
                        $image= Request::root().'/uploads/'.$user->image;
                    }
                ?>
               
            <div class="">
                <img class="image" style="width: 100%; height: 300px; object-fit: contain ;"
                    src="{{$image}}">
            </div>