displaying random images with an appropriate caption [message #175931] |
Tue, 08 November 2011 22:34 |
|
Is there any way to write something that will choose not just a random
image but the random image and its caption as a pair?
This is my code for my random images as it exists now but,
unfortunately, it doesn't work very well if I don't have a caption
with the photo (even if just to credit the photographer):
<div style="float:right; width:363px;
margin: 1.0em 2.75em 1.0em 1.0em; padding: 0.25em;
text-align:center; background-color: #fff">
<?php
$dir="/library/images/slideshow";
$images = array("$dir/farm-640x480.gif" ,
"$dir/ranch-640x480.gif" ,
"$dir/plantation-640x480.gif");
$format=' <img src="%s"
class="rounded-corners"
style="width:360px;height:270px" />';
printf("$format", $images[array_rand($images)]);
echo "\n";
?>
</div>
Thanks for any help and/or suggestions.
|
|
|
|
Re: displaying random images with an appropriate caption [message #175934 is a reply to message #175932] |
Tue, 08 November 2011 22:54 |
|
On Nov 8, 7:51 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 11/8/2011 10:34 PM, paris2venice wrote:
>
>
>
>
>
>
>
>
>
>> Is there any way to write something that will choose not just a random
>> image but the random image and its caption as a pair?
>
>> This is my code for my random images as it exists now but,
>> unfortunately, it doesn't work very well if I don't have a caption
>> with the photo (even if just to credit the photographer):
>
>> <div style="float:right; width:363px;
>> margin: 1.0em 2.75em 1.0em 1.0em; padding: 0.25em;
>> text-align:center; background-color: #fff">
>> <?php
>> $dir="/library/images/slideshow";
>> $images = array("$dir/farm-640x480.gif" ,
>> "$dir/ranch-640x480.gif" ,
>> "$dir/plantation-640x480.gif");
>> $format='<img src="%s"
>> class="rounded-corners"
>> style="width:360px;height:270px" />';
>> printf("$format", $images[array_rand($images)]);
>> echo "\n";
>> ?>
>> </div>
>
>> Thanks for any help and/or suggestions.
>
> A better question is - how do you want to relate the caption to its
> picture? Once you've decided how you want to relate them on your
> server, displaying the related caption is easy.
>
> It could be a database... file name similarity... different
> directories... about anything you want.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================
I just want the captions in an array just like the filenames for the
images. I just don't know how to make the randomizer get the match
from each array.
|
|
|
Re: displaying random images with an appropriate caption [message #175935 is a reply to message #175931] |
Tue, 08 November 2011 22:39 |
|
On Nov 8, 7:34 pm, paris2venice <paris2ven...@gmail.com> wrote:
> Is there any way to write something that will choose not just a random
> image but the random image and its caption as a pair?
>
> This is my code for my random images as it exists now but,
> unfortunately, it doesn't work very well if I don't have a caption
> with the photo (even if just to credit the photographer):
>
> <div style="float:right; width:363px;
> margin: 1.0em 2.75em 1.0em 1.0em; padding: 0.25em;
> text-align:center; background-color: #fff">
> <?php
> $dir="/library/images/slideshow";
> $images = array("$dir/farm-640x480.gif" ,
> "$dir/ranch-640x480.gif" ,
> "$dir/plantation-640x480.gif");
> $format=' <img src="%s"
> class="rounded-corners"
> style="width:360px;height:270px" />';
> printf("$format", $images[array_rand($images)]);
> echo "\n";
> ?>
> </div>
>
> Thanks for any help and/or suggestions.
I neglected to mention that I am not using MySQL or any other database
so I can't use that (at least not right now). Thanks.
|
|
|
Re: displaying random images with an appropriate caption [message #175936 is a reply to message #175935] |
Wed, 09 November 2011 02:51 |
houghi
Messages: 45 Registered: September 2011
Karma: 0
|
Member |
add to buddy list ignore all messages by this user
|
|
paris2venice wrote:
> On Nov 8, 7:34 pm, paris2venice <paris2ven...@gmail.com> wrote:
>> Is there any way to write something that will choose not just a random
>> image but the random image and its caption as a pair?
>>
>> This is my code for my random images as it exists now but,
>> unfortunately, it doesn't work very well if I don't have a caption
>> with the photo (even if just to credit the photographer):
>>
>> <div style="float:right; width:363px;
>> margin: 1.0em 2.75em 1.0em 1.0em; padding: 0.25em;
>> text-align:center; background-color: #fff">
>> <?php
>> $dir="/library/images/slideshow";
>> $images = array("$dir/farm-640x480.gif" ,
>> "$dir/ranch-640x480.gif" ,
>> "$dir/plantation-640x480.gif");
>> $format=' <img src="%s"
>> class="rounded-corners"
>> style="width:360px;height:270px" />';
>> printf("$format", $images[array_rand($images)]);
>> echo "\n";
>> ?>
>> </div>
>>
>> Thanks for any help and/or suggestions.
>
> I neglected to mention that I am not using MySQL or any other database
> so I can't use that (at least not right now). Thanks.
Put it all on one line. Put that in a file. Each line will be an image
with the caption. Then use whatever randomizer you want to include a
line.
houghi
--
> This is written under the influence of tv channel
> Some Channel (as there was nothing else on).
|
|
|
Re: displaying random images with an appropriate caption [message #175937 is a reply to message #175934] |
Wed, 09 November 2011 07:06 |
|
On Tue, 08 Nov 2011 19:54:42 -0800, paris2venice wrote:
> I just want the captions in an array just like the filenames for the
> images. I just don't know how to make the randomizer get the match from
> each array.
given two text files, one with a list of image paths and names based on
webroot, the other with a list of captions, so that 7th caption in the
caption file goes with the 7th url in the images file, and images and
captions are each listed on a single line in their respective files:
<?php
$flags = FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES;
$captions = file("captionfile",$flags); // read array of captions, 1 per
line
$images = file("imagesfile", $flags); // read array of filenames, 1 per
line
$index = rand(0, count($captions)-1); // get a random index
$imgfile = $images[$index];
if (strpos($mgfile,"/")!==0) $imgfile = "/" . $imgfile;
$captiontext = ucwords($captions[$index]);
echo <<<EOT
<p style="text-align:center;margin:0 auto">
<img src="http://www.myhost.domain{imgfile}" alt="{$captiontext}"><br>
{$captiontext}</p>
EOT;
?>
imagesfile might contain:
---8<--- cut here ---8<---
/dir/dir/myface.jpg
/dir2/clown.jpg
lorry.png
/dir/dir3/cat.gif
/dir/dir3/dog.gif
---8<--- cut here ---8<---
captionfile might contain:
---8<--- cut here ---8<---
a mugshot, so if you see me you'll know me
my clown face
the lorry I drive
my pet cat
the dog next door
---8<--- cut here ---8<---
Rgds
Denis McMahon
|
|
|
Re: displaying random images with an appropriate caption [message #175963 is a reply to message #175937] |
Fri, 11 November 2011 01:27 |
|
On Nov 9, 4:06 am, Denis McMahon <denismfmcma...@gmail.com> wrote:
> On Tue, 08 Nov 2011 19:54:42 -0800, paris2venice wrote:
>> I just want the captions in an array just like the filenames for the
>> images. I just don't know how to make the randomizer get the match from
>> each array.
>
> given two text files, one with a list of image paths and names based on
> webroot, the other with a list of captions, so that 7th caption in the
> caption file goes with the 7th url in the images file, and images and
> captions are each listed on a single line in their respective files:
>
> <?php
>
> $flags = FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES;
>
> $captions = file("captionfile",$flags); // read array of captions, 1 per
> line
> $images = file("imagesfile", $flags); // read array of filenames, 1 per
> line
>
> $index = rand(0, count($captions)-1); // get a random index
>
> $imgfile = $images[$index];
> if (strpos($mgfile,"/")!==0) $imgfile = "/" . $imgfile;
> $captiontext = ucwords($captions[$index]);
>
> echo <<<EOT
>
> <p style="text-align:center;margin:0 auto">
> <img src="http://www.myhost.domain{imgfile}" alt="{$captiontext}"><br>
> {$captiontext}</p>
>
> EOT;
>
> ?>
>
> imagesfile might contain:
>
> ---8<--- cut here ---8<---
> /dir/dir/myface.jpg
> /dir2/clown.jpg
>
> lorry.png
>
> /dir/dir3/cat.gif
> /dir/dir3/dog.gif
>
> ---8<--- cut here ---8<---
>
> captionfile might contain:
>
> ---8<--- cut here ---8<---
>
> a mugshot, so if you see me you'll know me
> my clown face
> the lorry I drive
>
> my pet cat
>
> the dog next door
> ---8<--- cut here ---8<---
>
> Rgds
>
> Denis McMahon
Hi Denis,
Thanks a bunch for your help. I liked your solution a lot and tried
to implement it with only partial success. Below is the URL for a
test page of what I have implemented with your code. The page
appeared blank until I commented out the here document. I also
implemented var_dumps on the variables to be sure I was getting
accurate data.
http://www.geol.ucsb.edu/graduate/aa.php
I also corrected a couple of syntax errors but mostly the code is
exactly as in your code above.
This is what is commented out:
//echo <<<EOT
//<p style="text-align:center;margin:0 auto">
//<img src="http://www.geol.ucsb.edu{$imgfile}" alt="{$captiontext}" /
>
//<br />
//{$captiontext}</p>
//EOT;
As you can see, it appears fine with the here doc commented. I just
can't see what is wrong with the here doc. Thanks for any further
help.
|
|
|
Re: displaying random images with an appropriate caption [message #175965 is a reply to message #175963] |
Fri, 11 November 2011 05:50 |
|
On Thu, 10 Nov 2011 22:27:43 -0800, paris2venice wrote:
> I also corrected a couple of syntax errors but mostly the code is
> exactly as in your code above.
Yeah, I used $mages where I should have used $images, and missed the $
from {$imgfile} in the heredoc.
> This is what is commented out:
>
> //echo <<<EOT
> //<p style="text-align:center;margin:0 auto"> //<img
> src="http://www.geol.ucsb.edu{$imgfile}" alt="{$captiontext}" /
>>
> //<br />
> //{$captiontext}</p>
> //EOT;
>
> As you can see, it appears fine with the here doc commented. I just
> can't see what is wrong with the here doc. Thanks for any further help.
All the heredoc does is wrap the image and caption in a centered
paragraph, html 4.01 type markup. It was an example of how to output the
caption and image. Did you try it (a) without the style attributes (b)
using a div instead?
I wasn't trying to generate markup and css that would fit neatly into
your existing page layout, I was generating a standalone paragraph.
http://www.sined.co.uk/tmp/randomPic.php
http://www.sined.co.uk/tmp/randomPic.txt (source)
Rgds
Denis McMahon
|
|
|