Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder najkiie's Avatar
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question Load image from urlbar? blah.com/?img=url.. how do i do it?

    Hi.

    I'm a beginner at php, so i don't know so much about it yet.

    I'm trying to create a photo gallery.. When you go to the site it will load a random image, and then it'll have a button that loads a new image if you click it...

    And that is pretty much finished. But, how about if you wanna send the link to a friend? That wont work.

    I would like to know if there is a way to "insert" php into the url... like this example:

    example.com/something/?imgurl=imageurlgoeshere

    I'd also like to know how to load the image from the url bar into the "image box" on my site.

    This is the code i use to get a random image from a directory:
    Code:
    <?
     $imglist='';
     $img_folder = "randomizer/";
    
      mt_srand((double)microtime()*1000);
    
     $imgs = dir($img_folder);
    
     while ($file = $imgs->read()) {
       if (eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file))
         $imglist .= "$file ";
    
     } closedir($imgs->handle);
    
     $imglist = explode(" ", $imglist);
     $no = sizeof($imglist)-2;
    
     $random = mt_rand(0, $no);
     $image = $imglist[$random];
    
     echo '<img src="'.$img_folder.$image.'" class="random-img">';
     ?>
    I would send you a link to the site i'm working on, but i can't do it because i'm working with it on my computer.

    I really hope you understand what i mean, thanks in advance.
    -Nike
    Last edited by najkiie; 05-03-2009 at 06:32 PM.
    -Nike - PTS - Blubbz
    I would love to change the world, but they won't give me the source code.

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by najkiie View Post
    And that is pretty much finished. But, how about if you wanna send the link to a friend? That wont work.

    I would like to know if there is a way to "insert" php into the url... like this example:

    example.com/something/?imgurl=imageurlgoeshere

    I'd also like to know how to load the image from the url bar into the "image box" on my site.
    That's a standard practice. All you're doing is creating a link with a query string attached to it, like http://www.google.com/search?hl=en&q=php+rewrite+url, and sending it in an email or linking it directly in your document is trivial. The $_GET superglobal variable references any data passed in the query string.

    In this specific case, you mention sending a URL to the image. Why? Why not just send the URL to the script that displays the random image itself? Unless your real goal is to have the script pull a random image and push it out as an image file, which is different than simply displaying it as you're doing. For this you'd need to read the image file and then use header() to output the proper content type and force it back to the browser as an actual image. Is this your intent?


    I'd recommend taking the time to read through the PHP Language Reference. I'd also take some time and read up on the HTTP protocol, as that's what you'll be working with most often in scripts like this.

  • #3
    Regular Coder najkiie's Avatar
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thanks for your respond.

    I'll take a look at the link right after i've taken a shower..

    No that's exactly what i want... i want to be able to send a link to a person, and when they go to it the "viewer" will display a certain image...

    I do not want to send the direct url to the image. Sorry if i was unclear.

    Thanks
    -Nike - PTS - Blubbz
    I would love to change the world, but they won't give me the source code.

  • #4
    New Coder
    Join Date
    Jan 2006
    Posts
    73
    Thanks
    2
    Thanked 3 Times in 3 Posts
    It's as simple as:

    PHP Code:
    $imgurl $_GET['imgurl'];

    echo 
    "<img src=\"$imgurl\">"

  • #5
    Regular Coder najkiie's Avatar
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thanks to both of you guys your your responds.

    I just tried it and it's working beautifully! I really appreciate your help, thanks again.
    -Nike - PTS - Blubbz
    I would love to change the world, but they won't give me the source code.

  • #6
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmm interesting, but lets say you have some images stored in a Database like MySQL how would you randomly pull those images so that you can insert them in the "<img src=\"$imgurl\">"?

  • #7
    Regular Coder najkiie's Avatar
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by nitestryker View Post
    hmm interesting, but lets say you have some images stored in a Database like MySQL how would you randomly pull those images so that you can insert them in the "<img src=\"$imgurl\">"?
    Good question. I'd also like to know that.
    -Nike - PTS - Blubbz
    I would love to change the world, but they won't give me the source code.

  • #8
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by nitestryker View Post
    hmm interesting, but lets say you have some images stored in a Database like MySQL how would you randomly pull those images so that you can insert them in the "<img src=\"$imgurl\">"?
    Are you asking how to retrieve an actual image from the database, i.e. one that is stored in a BLOB field as binary data? That goes back to what I said about retrieving the actual image file and using header() to force the content-type to an image and outputting the image itself. If you're simply referring to an image path or an image URI, then it's not much different than that of retrieving the same info from the filesystem, a list/array, etc and grabbing a random value.

    PHP Code:
    <?php
    // let's say you use MySQL
    // connect to the MySQL server, select the db, etc
    // query to retrieve a random image
    $sql'SELECT imgPath FROM images ORDER BY RAND() LIMIT 1';
    $resmysql_query($sql);
    if ( 
    $res !== FALSE ) {
      
    $imgPathmysql_result($res,0);
      echo 
    "<img src='{$imgPath}'>";
    }
    ?>
    The RAND() function is the key; obviously the more images stored, the more random the output can be.

  • #9
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by xGIHavoc View Post
    It's as simple as:

    PHP Code:
    $imgurl $_GET['imgurl'];

    echo 
    "<img src=\"$imgurl\">"
    True, BUT. Make sure you check the 'imgurl' value against a known set of image path values. In other words, don't let them put in something that will allow them to retrieve a file off your server arbitrarily. Also, I'd recommend using base64_encode() to encode the URL value in the query string, then use base64_decode() in the receiving script to retrieve the actual URL value.

  • #10
    Regular Coder najkiie's Avatar
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    If anybody still is monitoring this thread, i have another question. Sorry for being so late...

    When you go to the "image" page, the url wont have anything in the $_GET['imgurl'].

    I'd like it so that when someone goes to that page, it will check if the $_GET['imgurl'] == '' and if it is, then i want it to redirect to a new page.

    This is what i've got so far:
    Code:
    if ($_GET['imgurl'] == '') {
        header( 'location:' . 'the new image url goes here....' );
    }
    else {
        echo '<center><a class="random-img-link" href="' .$_GET['imgurl']. '" target="_blank"> <span><img src="' .$_GET['imgurl']. '" class="random-img" /></span></a></center>';
    }
    But that gives me a parse error?

    Parse error: parse error in C:\xampp\htdocs\wordpress\wp-content\themes\blubbz\random-page.php on line 44

    any suggestions?
    -Nike - PTS - Blubbz
    I would love to change the world, but they won't give me the source code.

  • #11
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts
    What is line 44?

  • #12
    Regular Coder najkiie's Avatar
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sea4me View Post
    What is line 44?
    Oh i didn't think anybody was going to respond

    This is what i have on that line:
    Code:
    if ($_GET['imgurl'] == '') {
    The php code i posted before, is that correct or is it incorrect?
    -Nike - PTS - Blubbz
    I would love to change the world, but they won't give me the source code.

  • #13
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Can you post a few lines before that? The code you posted above shouldn't generate that error.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •