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

前端 未结 6 1975
不知归路
不知归路 2021-02-06 19:25

I am working on a little project and I need to display the author\'s image for each author in my database. peep the code below:

--THE QUERY--

         


        
相关标签:
6条回答
  • 2021-02-06 20:05
    <img id="currentPhoto" src="SomeImage.jpg" onerror="this.src='Default.jpg'" width="100" height="120">
    
    0 讨论(0)
  • 2021-02-06 20:06

    There are many ways to do this.

    <?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>
    
    0 讨论(0)
  • 2021-02-06 20:07

    Try this way: For 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>
    
    0 讨论(0)
  • 2021-02-06 20:16

    try

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

    have holderthumbnail.jpg in your 'artists' directory

    0 讨论(0)
  • 2021-02-06 20:26

    Code :

    <?php
         if(file_exists("$your_image.jpg")) $filename = "$your_image.jpg";
         else $filename = "default.jpg";
    
         echo "<img src='$filename'/>";
    ?>
    
    0 讨论(0)
  • 2021-02-06 20:31

    Disk Access = slow

    You have a couple of options. One is to check that the file exists each and every time you need to display it. Unfortunately, this is less than ideal because disk reads can be a performance bottleneck. It's not a tenable solution if you're serving up hundreds of page loads a minute. In high-performance environments you want to avoid as many file stats as possible:

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

    Hard-code the names into your code

    Another, if you have a small number of artists whose images you need to display is to hard-code in the names for the images you have:

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

    Of course, this isn't particularly helpful because then you'll need to edit your code each time your catalog of artist images changes.

    The best idea ...

    The preferred option in this instance would be this:

    Since you're already hitting the database to get the artist records, store the relevant image filename in a database column of the artists table. That way you can avoid the superfluous disk access.

    This method allows you to retrieve the filename from the database with your original query. If the field is empty you'll know to display the default image instead.

    0 讨论(0)
提交回复
热议问题