...

View Full Version : Load image from urlbar? blah.com/?img=url.. how do i do it?



najkiie
05-03-2009, 07:28 PM
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:


<?
$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

bdl
05-03-2009, 07:59 PM
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 (http://us2.php.net/manual/en/reserved.variables.get.php) 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 (http://www.php.net/langref). I'd also take some time and read up on the HTTP protocol (http://www.google.com/search?&q=HTTP%20protocol), as that's what you'll be working with most often in scripts like this.

najkiie
05-03-2009, 09:24 PM
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 :)

xGIHavoc
05-03-2009, 09:36 PM
It's as simple as:


$imgurl = $_GET['imgurl'];

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

najkiie
05-03-2009, 10:00 PM
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. :)

nitestryker
05-03-2009, 10:18 PM
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\">"?

najkiie
05-03-2009, 10:19 PM
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.

bdl
05-03-2009, 10:46 PM
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
// 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';
$res= mysql_query($sql);
if ( $res !== FALSE ) {
$imgPath= mysql_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.

bdl
05-03-2009, 10:48 PM
It's as simple as:


$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() (http://www.php.net/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.

najkiie
05-10-2009, 11:12 AM
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:


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?

sea4me
05-10-2009, 06:54 PM
What is line 44?

najkiie
05-10-2009, 09:02 PM
What is line 44?

Oh i didn't think anybody was going to respond :D

This is what i have on that line:

if ($_GET['imgurl'] == '') {The php code i posted before, is that correct or is it incorrect?

venegal
05-10-2009, 09:43 PM
Can you post a few lines before that? The code you posted above shouldn't generate that error.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum