How to not display image if blank

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • StJust
    New Member
    • May 2010
    • 4

    #1

    How to not display image if blank

    Is it possible to set an "if" condition to check if image is available then display it?
    I am loading images from an url that will have the ID loaded from the database. Image URL links with bad id will show like this: Example

    now on the page it will display as the small blank box, how can i make it display something else or hide it all together?

    This is my code

    Code:
    <a href="http://us.playstation.com/publictrophy/index.htm?onlinename={vb:raw userinfo.field5}">
    <img src="http://fp.profiles.us.playstation.com/playstation/psn/pid/{vb:raw userinfo.field5}.png">
    </a><br  /><br  />
    Thanks
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    If the image you are loading does not exist, or the server refuses to serve it to you, it is usually safe to assume the server response includes a header to reflect that. You can use the get_headers function to fetch the headers for your image, and check that to see if the server is willing to serve you the image. You can then, based on that, either try to use the image or serve alternate content.

    For example, consider this:
    [code=php]<?php
    // The URL to the image you want to
    // use in your code.
    $imgUrl = "http://fp.profiles.us. playstation.com/playstation/psn/pid/wrongurlexample .png";

    // Fetch the response headers the server
    // sends when the image is requested.
    $headers = get_headers($im gUrl);

    // See if the first line includes 200,
    // which is the HTTP response code for success.
    if(stristr($hea ders[0], "200"))
    {
    // The server can successfully serve
    // the image, so we can use it!
    echo "<img src=\"{$imgUrl} \" alt=\"My Image\">";
    }
    else {
    // The server can not serve the image.
    // Do whatever you want to do instead,
    // like serve an error image.
    echo '<img src="error.png" alt="An error occured">';
    }
    ?>[/code]

    Comment

    • StJust
      New Member
      • May 2010
      • 4

      #3
      Originally posted by Atli
      Hey.

      If the image you are loading does not exist, or the server refuses to serve it to you, it is usually safe to assume the server response includes a header to reflect that. You can use the get_headers function to fetch the headers for your image, and check that to see if the server is willing to serve you the image. You can then, based on that, either try to use the image or serve alternate content.

      For example, consider this:
      [code=php]<?php
      // The URL to the image you want to
      // use in your code.
      $imgUrl = "http://fp.profiles.us. playstation.com/playstation/psn/pid/wrongurlexample .png";

      // Fetch the response headers the server
      // sends when the image is requested.
      $headers = get_headers($im gUrl);

      // See if the first line includes 200,
      // which is the HTTP response code for success.
      if(stristr($hea ders[0], "200"))
      {
      // The server can successfully serve
      // the image, so we can use it!
      echo "<img src=\"{$imgUrl} \" alt=\"My Image\">";
      }
      else {
      // The server can not serve the image.
      // Do whatever you want to do instead,
      // like serve an error image.
      echo '<img src="error.png" alt="An error occured">';
      }
      ?>[/code]
      Hey,

      Thank you for your response. This is the code I implemented for my page

      Code:
      <?php
      // The URL to the image you want to
      // use in your code.
      $imgUrl = "http://fp.profiles.us.playstation.com/playstation/psn/pid/{vb:raw userinfo.field5}.png";
      
      // Fetch the response headers the server
      // sends when the image is requested.
      $headers = get_headers($imgUrl);
      
      // See if the first line includes 200,
      // which is the HTTP response code for success.
      if(stristr($headers[0], "200"))
      {
         // The server can successfully serve
         // the image, so we can use it!
         echo '<a href="http://us.playstation.com/publictrophy/index.htm?onlinename={vb:raw userinfo.field5}"><img src=\"{$imgUrl}\"></a>';
      };
      else {
         // The server can not serve the image.
         // Do whatever you want to do instead,
         // like serve an error image.
         echo '<a href="http://us.playstation.com/publictrophy/index.htm?onlinename={vb:raw userinfo.field5}"><img src="cpstyles/darkvision/misc/nopid.png"></a>';
      }
      ?>
      Now everything disappeared. Theres absolutely nothing displaying.

      What seems to be the problem and how do i fix it?

      Thanks

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Do you have the debug messages turned on?

        I'm guessing the problem is the extra semi-colon on line #17. It could also be the value you insert into the URLs: "{vb:raw userinfo.field5 }". I'm not sure exactly what that is, but I'm pretty sure it won't work as-is.

        But, in any case, if you turn on the debug messages, you should get a message telling you what the problem is.

        Comment

        • StJust
          New Member
          • May 2010
          • 4

          #5
          Hey

          about that code in the imgurl, it is a variable that is loaded from an SQL database. I tried in this code as well but still gave exact same result so i figured its not that

          Code:
          $imgUrl = "http://fp.profiles.us.playstation.com/playstation/psn/pid/" & {vb:raw userinfo.field5} & ".png";
          when i remove that extra semi-colon it displays with an error (a screenshot is attached, notice both images are in there. Furthermore, this image is not supposed to be blank but it is supposed to be displayed from the url with the variable from the database)

          that was the reason that i added it which led everything to disappear

          also in that screenshot the debugger is enabled in the code, no debug message just error in display


          Any suggestions?

          Thanks
          Attached Files

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Ok, I see.

            It seems that, for some reason, a part of the PHP code is not being executed, but rather printed to the screen. Can you tell us which type of server you are using, and which versions?

            And could you show us the code that is fetching the data from your SQL database? Might help us understand what exactly is going on with that URL.

            [code=php]$imgUrl = "http://fp.profiles.us. playstation.com/playstation/psn/pid/" & {vb:raw userinfo.field5 } & ".png";[/code]
            This is invalid. In PHP we concatenate strings using a single dot, not an ampersand. (I believe the ampersand is used in VB-like languages.)

            Comment

            • StJust
              New Member
              • May 2010
              • 4

              #7
              yes sorry thats my main language used (vb6)

              This code is from a template out of like 100 templates. I am trying to embed this in a vBulletin forum V.4.0.3

              The template doesnt have a header or a footer as it is a part of a huge PHP system.

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Ahh ok, so this is a part of a vBulletin template? I've never used that before myself, but as I understand it, it has it's own syntax. You can't just put PHP syntax in there.

                You need to recreate the PHP code using this vBulletin template syntax, and use that instead. See the vBulletin Manual for details on how to use that syntax. It seems easy enough to use. (Well, it should be, as it is made to be an clean and easy replacement.)

                Comment

                Working...