PHP - How to Dynamically insert images

10 replies
I'm hoping someone can help me sort out some dynamic image insertion using PHP.
All photos are in the same directory and have same file extension .gif
Goal is to dynamically insert a picture based on incoming url. And if incoming url doesn't have the variable, show a default picture.
URL: http://mysite.com/page.php?$img=imagename
Page code:
<php?
if (empty($_GET['$img']))
echo("<img src=http://mysite.com/images/default.gif>");
else ($_GET['$img1']);
echo("<img src http://mysite.com/images/$img.gif >");
?>
I'm learning php program in my spare time and this is beyond me at this point.

Thanks in advance for any help here.
#dynamically #images #insert #php
  • Profile picture of the author Johnny Slater
    Try this:

    $img = $_GET[img];

    if(!$img){
    echo('<img src=http://mysite.com/images/default.gif>');
    }
    else{
    echo('<img src http://mysite.com/images/'.$img.gif.' >');
    }
    Signature

    {{ DiscussionBoard.errors[3937458].message }}
    • Profile picture of the author JayGreen
      Originally Posted by Johnny Slater View Post

      Try this:

      = ;

      if(!){
      echo('<img src=http://mysite.com/images/default.gif>');
      }
      else{
      echo('<img src http://mysite.com/images/'..gif.' >');
      }
      Defualt picture loads but not if I use the variable in the url. http://mysite.com/test.php?img=test

      It works if I add a period after "test" in url. Is it ok to use a period in the url like this:http://mysite.com/test.php?img=test.&morednynamiccode
      {{ DiscussionBoard.errors[3937691].message }}
      • Profile picture of the author mojojuju
        Johnny, it looks like you've got a typo in your code.

        This line...

        echo('<img src http://mysite.com/images/'.$img.gif.' >');

        should look like this....

        echo('<img src "http://mysite.com/images/'.$img.'gif" />');
        Signature

        :)

        {{ DiscussionBoard.errors[3938012].message }}
        • Profile picture of the author JayGreen
          Originally Posted by mojojuju View Post

          Johnny, it looks like you've got a typo in your code.

          This line...

          echo('<img src http://mysite.com/images/'..gif.' >');

          should look like this....

          echo('<img src "http://mysite.com/images/'..'gif" />');
          by adding "=" after src and changing "gif" to ".gif" it worked wonderfully. Thanks!
          {{ DiscussionBoard.errors[3938155].message }}
          • Profile picture of the author mojojuju
            Originally Posted by JayGreen View Post

            by adding "=" after src and changing "gif" to ".gif" it worked wonderfully. Thanks!
            Oops, I didn't even notice that the "=" was missing because it was the position of the single quote that caught my eye. It's a good thing you caught that.
            Signature

            :)

            {{ DiscussionBoard.errors[3938165].message }}
            • Profile picture of the author JayGreen
              Originally Posted by mojojuju View Post

              Oops, I didn't even notice that the "=" was missing because it was the position of the single quote that caught my eye. It's a good thing you caught that.
              lol, I did something right for once.

              Thanks for helping with this. One question if you don't mind:

              Trying to get dynamic keyword insertion for image title tags as well.

              http://mysite.com/test.php?kw=insert text&img=image
              page code:
              <?php
              $img = $_GET[img];
              if(!$img){
              echo('<img src=http://mysite.com/images/test.gif title=default>');}
              else{
              echo('<img src="http://mysite.com/images/'.$img.'.gif" title='.$_GET['kw'].'>');
              }
              ?>
              I tried this but it only shows gets the first word "insert" of the incoming url.
              Any ideas?
              And thanks for the help.
              {{ DiscussionBoard.errors[3938366].message }}
              • Profile picture of the author mojojuju
                Well, your url can't have a space in it...


                Instead of...

                http://mysite.com/test.php?kw=insert text&img=image

                It should look like this...

                http://mysite.com/test.php?kw=insert+text&img=image

                Or this...

                http://mysite.com/test.php?kw=insert%20text&img=image


                ... depending on how you want to do it..
                Signature

                :)

                {{ DiscussionBoard.errors[3938390].message }}
                • Profile picture of the author JayGreen
                  I added + between keywords but still showing first keyword only.
                  {{ DiscussionBoard.errors[3938479].message }}
                  • Profile picture of the author mojojuju
                    It shows both keywords when I do it. Perhaps your browser is caching the previous page. Do a <ctrl>+r or a <CTRL>+<SHIFT>+R (depending on your browser) and see if you get the correct output.

                    And I just noticed that you don't have the title attribute value surrounded by quotes.

                    You should change:

                    echo('<img src="http://mysite.com/images/'.$img.'.gif" title='.$_GET['kw'].'>');

                    to

                    echo('<img src="http://mysite.com/images/'.$img.'.gif" title="'.$_GET['kw'].'" />');
                    Signature

                    :)

                    {{ DiscussionBoard.errors[3938816].message }}
                    • Profile picture of the author JayGreen
                      That did the trick. Thanks for all your help!
                      {{ DiscussionBoard.errors[3940252].message }}

Trending Topics