images with php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cazconv2007
    New Member
    • Feb 2007
    • 30

    #1

    images with php

    how do i put an image in my php script. i want to be able to do it so that the whatever choice they make a picture comes up , i can do if else if loops for everything but cant put an image in could someone give me an outline on adding images

    thanx
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Originally posted by cazconv2007
    how do i put an image in my php script.
    Alrightey.

    Here's the fast way to do it:

    [PHP]
    <?php
    header('Content-type: image/jpeg');
    readfile($_REQU EST['imageNameOrWha tever'] . '.jpg');
    exit;
    ?>
    [/PHP]

    Nothing should be before the opening <?php tag, or else your output won't look right.

    Note that the request variable doesn't contain the full name of the file; this would prevent a resourceful cracker (or clueless User) from loading up, say, ssh_root_passwo rd.txt ("I'd been meaning to get rid of that!").

    As for the good way to do it, well, you'll probably want to validate your $_REQUEST['imageNameOrWha tever'] variable, and just to be safe, you should probably prepend the path, so that the User only has control over the name of the image. E.g.,

    [PHP]
    readfile("$_SER VER[DOCUMENT_ROOT]/images/local/stuff/and/whatnot/$_REQUEST[imageNameOrWhat ever].jpg");
    [/PHP]

    Beyond that, go nuts; just make sure that the contents of the image file is the ONLY thing that gets output.

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #3
      Are you trying to generate an image using PHP (or read one dynamically), or just embed an image into the HTML?

      Originally posted by cazconv2007
      how do i put an image in my php script. i want to be able to do it so that the whatever choice they make a picture comes up , i can do if else if loops for everything but cant put an image in could someone give me an outline on adding images

      thanx

      Comment

      • cazconv2007
        New Member
        • Feb 2007
        • 30

        #4
        i have a if loop where if the users chooses an option a price comes up i want images which i already have to come up depending on the option

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Originally posted by cazconv2007
          i have a if loop where if the users chooses an option a price comes up i want images which i already have to come up depending on the option
          In that case, your best bet would likely be just to generate a bunch of img elements.

          [PHP]
          $image = (isset($_GET['imageName'])
          ? $_GET['imageName']
          : 'default'
          );

          print("<img src=\"images/$image.jpg\" alt=\"$image\" />");
          [/PHP]

          Where $_GET['imageName'] is set by reloading the page when the User chooses an option. Or something like that.

          Incidentally, to address something I said in a previous post, when you use a PHP script to output an image (like in my first post), you should only need to use that when the image is OUTSIDE of your server's document root (such as stored in a database or a temp directory, for example). Otherwise, that's just way overkill when a simple img tag will do very nicely.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Originally posted by cazconv2007
            i have a if loop where if the users chooses an option a price comes up i want images which i already have to come up depending on the option
            I dont really understand your problem. Could you post your code?

            Comment

            • cazconv2007
              New Member
              • Feb 2007
              • 30

              #7
              $extras = $_GET ['extras'];


              if ($extras == "mints")
              {
              $extracost = 5;
              }

              elseif ($extras == "shovel")
              {
              $extracost = 8;
              }


              elseif ($extras == "straw")
              {
              $extracost = 20;
              }


              elseif ($extras == "camel food")
              {
              $extracost = 10;
              }


              elseif ($extras == "leather saddle")
              {
              $extracost = 15;
              }


              elseif ($extras == "brush")
              {
              $extracost = 10;
              }


              elseif ($extras = "file")
              {
              $extracost = 8;
              }

              else
              {
              $extracost = 0;
              }


              echo "
              <p>The extra you choose was an $extras at the cost of £$extracost</p>";


              thats part of my code now i want an image to come up when they pick the extra option i already have the images

              Comment

              • code green
                Recognized Expert Top Contributor
                • Mar 2007
                • 1726

                #8
                [PHP]elseif ($extras == "brush")
                {
                $extracost = 10;
                $img = 'www.address/of/your/image.gif';
                $alt = 'description of image';
                }


                elseif ($extras = "file")
                {
                $extracost = 8;
                $img = 'www.address/of/your/image.gif';
                $alt = 'description of image';
                }

                else
                {
                $extracost = 0;
                $img = 'www.address/of/your/image.gif';
                $alt = 'description of image';
                }
                echo "
                <p>The extra you choose was an $extras at the cost of £$extracost</p>
                <img src=\"$img\" height=\"size\" width=\"size\" alt=\"$alt\";[/PHP]

                Comment

                Working...