By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,652 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,652 IT Pros & Developers. It's quick & easy.

calling a php script using img src ="random.php"

P: n/a
Hey all,

I have a small php script that calls a random image at the following
page.
http://www.2006ymcanationals.com/random.php

IT WORKS IF I go directly to the above link.

I am trying to call that in another page so that i get a random image
the page is http://2006ymcanationals.com/index.php using <img
src="random.php">

It is not working. I have tried using the full path to the
"random.php". I have also tried naming the main page index.html and
index.php. neither work.

what am i missing? Searching these groups it looks like this should
work.

Thanks in advance.

Brad

Jul 17 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a

<bh*****@gmail.com> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...
Hey all,

I have a small php script that calls a random image at the following
page.
http://www.2006ymcanationals.com/random.php

IT WORKS IF I go directly to the above link.

I am trying to call that in another page so that i get a random image
the page is http://2006ymcanationals.com/index.php using <img
src="random.php">

It is not working. I have tried using the full path to the
"random.php". I have also tried naming the main page index.html and
index.php. neither work.

what am i missing? Searching these groups it looks like this should
work.

Thanks in advance.

Brad


It's hard without looking at the code.
The images seem to be different names on the page. Eg. rimage2.jpg,
rimage3.jpg, rimage4.jpg, rimage5.jpg

Brent Palmer.


Jul 17 '05 #2

P: n/a
NC
bh*****@gmail.com wrote:

I have a small php script that calls a random image
at the following page.
http://www.2006ymcanationals.com/random.php

IT WORKS IF I go directly to the above link.

I am trying to call that in another page so that i get
a random image the page is http://2006ymcanationals.com/index.php
using <img src="random.php">


Actually, in your index.php page, it's not <img src="random.php">,
it's <img href="random.php">. Correct it, and everything should
work...

Cheers,
NC

Jul 17 '05 #3

P: n/a
bh*****@gmail.com wrote:
IT WORKS IF I go directly to the above link.

I am trying to call that in another page so that i get a random
image

It is not working.

what am i missing? Searching these groups it looks like this should
work.


You're outputting HTML from your PHP-script, not the image itself.

It's the same as if you try to have <IMG SRC="whatever.html"> - I
don't think you suppose that to work (unless whatever.html actually is
a very strangely named image file, of course).

What you should be doing in your script is to output suitable headers
for the image, and pass the image through your script. Something like
this:

<?php
$img_filename = 'test.gif'; // On serverside
// The above is what you can take as random...
header('Content-type: image/gif');
// header('Content-type: '.mime_content_type($img_filename));
// Use this if your server supports it!
header('Content-length: '.filesize($img_filename));
$file_pointer = fopen($img_filename, 'rb');
fpassthru($file_pointer);
fclose($file_pointer);
exit;
?>

--
Markku Uttula

Jul 17 '05 #4

P: n/a
*** bh*****@gmail.com escribió/wrote (16 Mar 2005 13:33:26 -0800):
http://www.2006ymcanationals.com/random.php

IT WORKS IF I go directly to the above link.

I am trying to call that in another page so that i get a random image
the page is http://2006ymcanationals.com/index.php using <img
src="random.php">


Your script does not return an image. It returns an HTML page that links to
an image. You cannot link to an HTML page with the <img> tag. Maybe you're
thinking of <iframe>?

You could either choose a random filename:

<img src="<?=get_random_img_name()?>">

Or make your script return an actual image:

<?
header('Content-Type: image/gif');
readfile('foo.gif');
?>

--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la intemperie)
++ No envíes tu dudas a mi correo, publícalas en el grupo
-+ Do not send me your questions, post them to the group
--
Jul 17 '05 #5

P: n/a
Thanks for all your help.

I dont know php well enough to write this stuff. I am following
instructions found here:
http://www.gatequest.net/misc/php/random-image.php

will this work?
Alvaro G. Vicario wrote:
*** bh*****@gmail.com escribió/wrote (16 Mar 2005 13:33:26 -0800):
http://www.2006ymcanationals.com/random.php

IT WORKS IF I go directly to the above link.

I am trying to call that in another page so that i get a random image the page is http://2006ymcanationals.com/index.php using <img
src="random.php">
Your script does not return an image. It returns an HTML page that

links to an image. You cannot link to an HTML page with the <img> tag. Maybe you're thinking of <iframe>?

You could either choose a random filename:

<img src="<?=get_random_img_name()?>">

Or make your script return an actual image:

<?
header('Content-Type: image/gif');
readfile('foo.gif');
?>

--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la intemperie) ++ No envíes tu dudas a mi correo, publícalas en el grupo
-+ Do not send me your questions, post them to the group
--


Jul 17 '05 #6

P: n/a

<bh*****@gmail.com> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...
Thanks for all your help.

I dont know php well enough to write this stuff. I am following
instructions found here:
http://www.gatequest.net/misc/php/random-image.php

will this work?


Yes this works fine for me. As I said the images seem to be displaying with
different names which indicate to me that all is working.
What images do you have in the folder? Do they look the same?

Brent Palmer.
Jul 17 '05 #7

P: n/a
*** bh*****@gmail.com escribió/wrote (16 Mar 2005 17:16:58 -0800):
I dont know php well enough to write this stuff. I am following
instructions found here:
http://www.gatequest.net/misc/php/random-image.php

will this work?


It should, given that you link to that script from the <img> tag.
--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la intemperie)
++ No envíes tu dudas a mi correo, publícalas en el grupo
-+ Do not send me your questions, post them to the group
--
Jul 17 '05 #8

P: n/a
On Thu, 17 Mar 2005 00:02:16 +0100, an orbiting mind-control laser
made "Alvaro G. Vicario" <kA*****************@terra.es> write:
*** bh*****@gmail.com escribió/wrote (16 Mar 2005 13:33:26 -0800):
http://www.2006ymcanationals.com/random.php

IT WORKS IF I go directly to the above link.

I am trying to call that in another page so that i get a random image
the page is http://2006ymcanationals.com/index.php using <img
src="random.php">


Your script does not return an image. It returns an HTML page that links to
an image. You cannot link to an HTML page with the <img> tag. Maybe you're
thinking of <iframe>?

You could either choose a random filename:

<img src="<?=get_random_img_name()?>">

Or make your script return an actual image:

<?
header('Content-Type: image/gif');
readfile('foo.gif');
?>


You know... I'm experiencing a very similar issue.

In my case, I am building a counter script (* non-relevant reasons
below) and I have similar behavior. I call the script directly in its
own directory and it outputs the composite image fine. Calling it
from one directory higher from within an HTML file gives a broken
image.

Originally, I copied from another script that output multiple
individual gifs when the script was called. I never understood how
that was supposed to work, other than as a stand-alone script, so I
redid the code to copy each image jpg into one image that I output
from the script.

Relevant code:
<<<
//determine size of images in directory by testing the zero image
$source = $style_folder . "0." . $ext;
$img_size = @getimagesize( $source );
// $width is string length in characters
$dst_img=ImageCreate( $width * $img_size[0], $img_size[1] );

for ($i=0;$i<strlen($countstring);$i++) {
$digit=substr("$countstring",$i,1);
// Build the image URL ...
$source = $style_folder . $digit . "." . $ext;
$src_img = imagecreatefromjpeg( $source );
// $source = $base_url . $style_folder . $digit . "." . $ext;
// echo "<img src=\"$source\" border=0>";
imagecopy( $dst_img, $src_img, $i * $img_size[0], 0, 0, 0,
$img_size[0], $img_size[1]);
}

header( "Content-type: image/jpeg" );
ImageJpeg($dst_img);

exit();
<<<

And from the calling HTML:
<<<
<img src="_Counters/jpbcount.php?link=Tester&style=odometer"
height="20" align="ABSMIDDLE">
<<<

I've tried calling the script with both an <img src= and <img
href= to no avail.

Any pointers?

Thanks.

-JPB

*Non-relevant rationale
I may be migrating my server over to a Linux-based hosting service
from my own Windows 2000-based server. I liberally used a CGI program
on my site and now that I may be going to a Linux-based system, I
figured I'd mimic the functionality of it in a more portable PHP
script.
Jul 17 '05 #9

P: n/a

"Dahak" <Da******@thefifthimperium.com> wrote in message
news:pf********************************@4ax.com...
On Thu, 17 Mar 2005 00:02:16 +0100, an orbiting mind-control laser
made "Alvaro G. Vicario" <kA*****************@terra.es> write:
*** bh*****@gmail.com escribió/wrote (16 Mar 2005 13:33:26 -0800):
http://www.2006ymcanationals.com/random.php

IT WORKS IF I go directly to the above link.

I am trying to call that in another page so that i get a random image
the page is http://2006ymcanationals.com/index.php using <img
src="random.php">


Your script does not return an image. It returns an HTML page that links toan image. You cannot link to an HTML page with the <img> tag. Maybe you'rethinking of <iframe>?

You could either choose a random filename:

<img src="<?=get_random_img_name()?>">

Or make your script return an actual image:

Here's a working example that might help. This checks to see if a website is
up and if it is, it displays a smiley gif and if not, a not so smiley gif in
a table of smileys. Now the page doesn't wait for ALL the smileys to not
time out before displaying; rather it displays the whole table immediately
and these pop up as they finish running. Now if it would only display a
"processing" gif while we waited :>)

==== An Actual Table Row ======

<tr>
<td>Logon.net</td>
<th><img src="/frag/status.php?addr=www.logon.net" width="37"
height="20"></th>
<th><img src="/frag/status.php?addr=mail.logon.net&port=25" width="37"
height="20"></th>
<th><img src="/frag/status.php?addr=mail.logon.net&port=110" width="37"
height="20"></th>
<th><img src="/frag/status.php?addr=mail.logon.net" width="37"
height="20"></th>
<th>&nbsp;</th>
</tr>

==== status.php has to be above Doc Root ===============

<?php
//Web Server Status v 1.2, Copyright 2002 By Ryan Schwiebert, visit
http://www.schwebdesigns.com/
//This script may be freely distributed providing all copyright headers are
kept intact.

//Concept from:
//Abax Server Status v1.04, Copyright 2002 By Nathan Dickman, visit
http://www.NathanDickman.com/
//Location of the live or dead server images

// Updated by John Jarrett 2003

//Please change to your server specifications
$live = "/frag/images/live2.gif";
$dead = "/frag/images/dead2.gif";

//The status checking script
//meddle at your own risk!
//check for port number, default is 80
#list($addr,$port)= explode (':',"$link");
if (empty($port)) {
$port = 80;
}

//Test the server connection

$churl = @fsockopen(server($addr), $port, $errno, $errstr, 20);
#$churl = @fsockopen($addr, $port, $errno, $errstr, 20);
if (!$churl){
header("Location: $dead");
}
else {
header("Location: $live");
}
function server($addr){
if(strstr($addr,"/")){$addr = substr($addr, 0, strpos($addr,
"/"));}
return $addr;
}
?>

====== end of script =====

hth,
John
Jul 17 '05 #10

P: n/a

Tex John wrote:
"Dahak" <Da******@thefifthimperium.com> wrote in message
news:pf********************************@4ax.com...
On Thu, 17 Mar 2005 00:02:16 +0100, an orbiting mind-control laser
made "Alvaro G. Vicario" <kA*****************@terra.es> write:
*** bh*****@gmail.com escribió/wrote (16 Mar 2005 13:33:26 -0800):> http://www.2006ymcanationals.com/random.php
>
> IT WORKS IF I go directly to the above link.
>
> I am trying to call that in another page so that i get a random image> the page is http://2006ymcanationals.com/index.php using <img
> src="random.php">

Your script does not return an image. It returns an HTML page that
links
toan image. You cannot link to an HTML page with the <img> tag.
Maybe
you'rethinking of <iframe>?

You could either choose a random filename:

<img src="<?=get_random_img_name()?>">

Or make your script return an actual image:


Here's a working example that might help. This checks to see if a

website is up and if it is, it displays a smiley gif and if not, a not so smiley gif in a table of smileys. Now the page doesn't wait for ALL the smileys to not time out before displaying; rather it displays the whole table immediately and these pop up as they finish running. Now if it would only display a "processing" gif while we waited :>)

==== An Actual Table Row ======

<tr>
<td>Logon.net</td>
<th><img src="/frag/status.php?addr=www.logon.net" width="37"
height="20"></th>
<th><img src="/frag/status.php?addr=mail.logon.net&port=25" width="37" height="20"></th>
<th><img src="/frag/status.php?addr=mail.logon.net&port=110" width="37" height="20"></th>
<th><img src="/frag/status.php?addr=mail.logon.net" width="37"
height="20"></th>
<th>&nbsp;</th>
</tr>

==== status.php has to be above Doc Root ===============

<?php
//Web Server Status v 1.2, Copyright 2002 By Ryan Schwiebert, visit
http://www.schwebdesigns.com/
//This script may be freely distributed providing all copyright headers are kept intact.

//Concept from:
//Abax Server Status v1.04, Copyright 2002 By Nathan Dickman, visit
http://www.NathanDickman.com/
//Location of the live or dead server images

// Updated by John Jarrett 2003

//Please change to your server specifications
$live = "/frag/images/live2.gif";
$dead = "/frag/images/dead2.gif";

//The status checking script
//meddle at your own risk!
//check for port number, default is 80
#list($addr,$port)= explode (':',"$link");
if (empty($port)) {
$port = 80;
}

//Test the server connection

$churl = @fsockopen(server($addr), $port, $errno, $errstr, 20);
#$churl = @fsockopen($addr, $port, $errno, $errstr, 20);
if (!$churl){
header("Location: $dead");
}
else {
header("Location: $live");
}
function server($addr){
if(strstr($addr,"/")){$addr = substr($addr, 0, strpos($addr,
"/"));}
return $addr;
}
?>

====== end of script =====

hth,
John

I was never able to get this working. Some people say you cannot call a
php script that outputs html from within an html page and others say
that it works. Which is it? Looks like it works in Johns example.

John,
could you post the rest of the html from your example? Is the page
named index.php or index.html?

Is there an opening and closing php statement in the page somewhere?
"<?php" and "?>"

Thanks for your help.
Brad

Jul 17 '05 #11

P: n/a
for a "processing" image use the lowsrc attribute

<img lowsrc="path/processing.gif" src="/frag/status.php?addr=addr.net"
width="37" height="20">

Jul 17 '05 #12

P: n/a
Nobody said that..
what was said that the image tag needs to have a SRC attribute (not
href) and the SRC needs to be a url to a valid image document (whether
generated on the fly via a script or whatever). your image tag just
so happens to reference something that outputs html.. which
not-suprisingly gives a broken image.

Jul 17 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.