Calling random image code

KAC

Joined: 2004-12-01
Posts: 164
Posted: Tue, 2005-04-19 13:54

I am using the following code on a page to call a random image:

Quote:
<?php @readfile('http://www.mywebsite.com/g2gallerymain/main.php?g2_view=imageblock:External&g2_blocks=randomImage&g2_show=title'); ?>

It works great!

What code could I add to it to show the "Jump to album" block under it?

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Tue, 2005-04-19 14:34

probably use a local template for modules/imageblock/templates/ImageBlock.tpl

 
djhomeless

Joined: 2005-04-27
Posts: 23
Posted: Thu, 2005-04-28 00:01

Do you have to do anything to enable the random block to execute outside of gallery?

Here is the code I'm using:

<?php @readfile('http://www.mysite.com/gallery/main.php?g2_view=imageblock:External&g2_blocks=randomImage&g2_show=title'); ?>

If I go to the gallery page, the image shows. If I execute the URL above in the browser, the image shows. But on my php page, no image shows. I've even removed the @ symbol but I don't get any errors!

Any ideas? Thanks.

Geoffrey

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Thu, 2005-04-28 00:06

djhomeless, you have to install and activate the g2 site admin -> random / popular module (imageblock module).
if it's active, you should get the block with readfile(...). and if it doesn't work, create a php file with just:

<?php readfile('http://www.mysite.com/gallery/main.php?g2_view=imageblock:External&g2_blocks=randomImage&g2_show=title'); ?>

to see all errors (no further html).

 
djhomeless

Joined: 2005-04-27
Posts: 23
Posted: Thu, 2005-04-28 06:46

Could it possibly be something to do with my htaccess file or Apache? I tried creating an empty php file before and I had put just that code inside but nothing showed (even after removing the @ symbol).

Even stranger if I put in an incorrect hostname, I still don't get any errors.

But this code is working inside the gallery app so I'm really confused why it won't execute remotely.

Thanks again,

Geoffrey

 
djhomeless

Joined: 2005-04-27
Posts: 23
Posted: Thu, 2005-04-28 11:38

Hmm, the plot thickens.

It turns out my php.ini settings had allow_url_fopen disabled. Once I enabled this, I can at least see the errors now. As before, if I call the file directly the image loads. If I drop the code into a blank php file, it throws the following:

Warning: readfile(http://www.idiotabroad.com/gallery2/main.php?g2_view=imageblock:External&g2_blocks=randomImage&g2_show=title): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in /home/e-smith/files/ibays/blog/html/foo.php on line 2

Anyone run into this?

Thanks,

Geoffrey

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Thu, 2005-04-28 13:43

when i enter this url, i get html, i.e. a HTTP 200 success code. But i also get a G2 security violation. you might have to set other image permissions in G2.

 
djhomeless

Joined: 2005-04-27
Posts: 23
Posted: Thu, 2005-04-28 16:23

What G2 security violation are you getting? I changed the permissions on my album to allow All Users to view items, but I am still getting the error.

I'm really lost on why this is happening!

Geoffrey

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Thu, 2005-04-28 17:01

djhomeless, just logout from G2 and browse to
http://www.idiotabroad.com/gallery2/main.php?g2_view=imageblock:External&g2_blocks=randomImage&g2_show=title

then you should see that the item is NOT viewable by guests.

 
djhomeless

Joined: 2005-04-27
Posts: 23
Posted: Thu, 2005-04-28 17:21

Ok, I see where this is going. But I'm still a bit confused.

I've given user guest All Access permissions in the root of Gallery, and the direct URL is now working even when logged off.

Unfortunately, the simple php file is still giving me the same error as before. The file is here:

http://www.idiotabroad.com/foo.php

Thanks again for the help!

Geoffrey

 
djhomeless

Joined: 2005-04-27
Posts: 23
Posted: Fri, 2005-04-29 15:59

Does anyone have any ideas? I'm really desperate to get this working!

Thanks in advance.

Geoffrey

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Fri, 2005-04-29 16:37

djhomeless, try other urls with readfile().. you need to get your php working with readfile. looks like your imageblock is ok now.

 
djhomeless

Joined: 2005-04-27
Posts: 23
Posted: Fri, 2005-04-29 23:32

I'm pretty sure its environmental, sadly I don't know enough about my OS to work this through.

Basically I can do all kinds of includes in php with no trouble. However, it always has trouble if I try to execute a script via the hostname.

One thing I was able to figure out, if I replace the hostname, with the local IP I was able to get the script to execute.

<?php readfile('http://127.0.0.1/blog/photos/main.php?g2_view=imageblock:External&g2_blocks=randomImage&g2_show=title'); ?>

However, since I'm not local to the machine it obviously won't stream the image.

My server uses SME Linux (www.contribs.org), which is a heavily secured variant of RedHat 9. I've cross posted this there so hopefully someone may have a clue. If you think of anything I'd appreciate some help as well, this is really frustrating to be so close! :(

Thanks,

Geoffrey

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Sat, 2005-04-30 00:10

try 84.9.63.165

 
ozgreg
ozgreg's picture

Joined: 2003-10-18
Posts: 1378
Posted: Sat, 2005-04-30 01:29

I posted this before .. a lot of Hosting firms are turning off php readfile() because of the security concerns..

Try using the Curl Library with something like this.. (I use them embedded calls so this is defunct for me)

<?php

// Get Random Image - Curl Security rather than @openfile()

$curl_handle = curl_init();
// Where should we get the data?
curl_setopt ($curl_handle, CURLOPT_URL, "http://yoursite.com/galleryurl/main.php?g2_view=imageblock:External&g2_blocks=randomImage&g2_maxSize=165&g2_show=title");
// Setup the Curl Options.
curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl_handle, CURLOPT_CONNECTTIMEOUT, 1);
// Make the call.
$buffer = curl_exec($curl_handle);
// Close Curl
curl_close($curl_handle);

print $buffer

?>
 
djhomeless

Joined: 2005-04-27
Posts: 23
Posted: Sat, 2005-04-30 06:56

Sadly this code didn't work (thanks for trying!). I tried both using the IP, and your Curl workaround. The curl code didn't execute or stream an exception back to the browser. I've checked the sys_log but theres nothing useful in there either.

Surely this has something to do with some sort of security restriction or dns?

If I use localhost, or the local ip, the script executes but doesn't stream an image.
If I use the external IP or host, I get a 404.
If I use just a relative path, like /gallery2/main.php, it says there is no such file or directory.

*sigh*
Geoffrey

 
ozgreg
ozgreg's picture

Joined: 2003-10-18
Posts: 1378
Posted: Sat, 2005-04-30 11:54

Try something like this using the embedded calls in G2. This is from a testing program so it is pretty crude but you should be able to modify it..


<?php
/***************************************************
Gallery2  Get Random Image

***************************************************

     * 'blocks' is a pipe (|) separated list, of one or more possible blocks which are:
     * randomImage|recentImage|viewedImage|randomAlbum|recentAlbum|viewedAlbum|specificItem
     * dailyImage|weeklyImage|monthlyImage|dailyAlbum|weeklyAlbum|monthlyAlbum

*/

require_once(dirname(__FILE__) . '/gallery2/embed.php');

$ret = GalleryEmbed::init(array('embedUri' => 'wp-gallery2.php', 'relativeG2Path' => 'gallery2/', 'activeUserId' => '', 'fullInit' => true ) );

list ($ret, $g2data) = GalleryEmbed::getImageBlock(array ('blocks' => 'randomImage', 'show' => 'none', 'activeUserId' => '', 'maxSize' => '165'));

print $g2data;

?>
 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Sat, 2005-04-30 12:11

and don't forget the GalleryEmbed::done(); at the end of the G2 interactions!
same code, added error status checks and >>done(); at the end:

<?php
/***************************************************
Gallery2  Get Random Image

***************************************************

     * 'blocks' is a pipe (|) separated list, of one or more possible blocks which are:
     * randomImage|recentImage|viewedImage|randomAlbum|recentAlbum|viewedAlbum|specificItem
     * dailyImage|weeklyImage|monthlyImage|dailyAlbum|weeklyAlbum|monthlyAlbum

*/

require_once(dirname(__FILE__) . '/gallery2/embed.php');

$ret = GalleryEmbed::init(array('embedUri' => 'wp-gallery2.php', 'embedPath' => '/', 'relativeG2Path' => 'gallery2/', 'activeUserId' => '', 'fullInit' => true ) );
if ($ret->isError()) {
   print $ret->getAsHtml();
}

list ($ret, $g2data) = GalleryEmbed::getImageBlock(array ('blocks' => 'randomImage', 'show' => 'none', 'activeUserId' => '', 'maxSize' => '165'));
if ($ret->isError()) {
   print $ret->getAsHtml();
}
/* end transactions */
$ret = GalleryEmbed::done();
if ($ret->isError()) {
   print $ret->getAsHtml();
}

print $g2data;

?>
 
Skit

Joined: 2003-10-07
Posts: 3
Posted: Fri, 2005-10-21 04:55

Valiant, I've modified your code above to produce a number of thumbnails for use in a horizontal Imageblock. Could it be done any better for multiple blocks?

<?php /* Call Gallery Functions for Imageblock */

require_once('/gallery/embed.php');

$ret = GalleryEmbed::init(array('embedUri' => 'wp-gallery2.php', 'embedPath' => '/gallery/', 'relativeG2Path' => '/gallery/', 'activeUserId' => '', 'fullInit' => 
true ) );

/* Call Thumbnails */

/* Thumbnail 1 */
if ($ret->isError()) {
   print $ret->getAsHtml();
}
list ($ret, $thumb1) = GalleryEmbed::getImageBlock(array ('blocks' => 'randomImage', 'show' => 'none', 'activeUserId' => '', 'maxSize' => '80'));
if ($ret->isError()) {
   print $ret->getAsHtml();
}

/* Thumbnail 2 */
if ($ret->isError()) {
   print $ret->getAsHtml();
}
list ($ret, $thumb2) = GalleryEmbed::getImageBlock(array ('blocks' => 'randomImage', 'show' => 'none', 'activeUserId' => '', 'maxSize' => '80'));
if ($ret->isError()) {
   print $ret->getAsHtml();
}

/* Thumbnail 3 */
if ($ret->isError()) {
   print $ret->getAsHtml();
}
list ($ret, $thumb3) = GalleryEmbed::getImageBlock(array ('blocks' => 'randomImage', 'show' => 'none', 'activeUserId' => '', 'maxSize' => '80'));
if ($ret->isError()) {
   print $ret->getAsHtml();
}

/* Thumbnail 4 */
if ($ret->isError()) {
   print $ret->getAsHtml();
}
list ($ret, $thumb4) = GalleryEmbed::getImageBlock(array ('blocks' => 'randomImage', 'show' => 'none', 'activeUserId' => '', 'maxSize' => '80'));
if ($ret->isError()) {
   print $ret->getAsHtml();
}

/* Thumbnail 5 */
if ($ret->isError()) {
   print $ret->getAsHtml();
}
list ($ret, $thumb5) = GalleryEmbed::getImageBlock(array ('blocks' => 'randomImage', 'show' => 'none', 'activeUserId' => '', 'maxSize' => '80'));
if ($ret->isError()) {
   print $ret->getAsHtml();
}

/* end transactions */
$ret = GalleryEmbed::done();

if ($ret->isError()) {

   print $ret->getAsHtml();

}
?>

					<table width=520 cellpadding="0" cellspacing="0">
						<tr>
							<td>
							<?php print $thumb1; ?>
							</td>
							<td>
							<?php print $thumb2; ?>
							</td>
							<td>
							<?php print $thumb3; ?>
							</td>
							<td>
							<?php print $thumb4; ?>
							</td>
							<td>
							<?php print $thumb5; ?>
							</td>
						</tr>
					</table>	

http://skit.id.au

 
gpinzone

Joined: 2006-10-08
Posts: 4
Posted: Fri, 2006-11-10 23:15

I'm using your solution, ozgreg. I was able to customize the output without touching any of the templates or classes in Gallery by using regular expressions:

// Strip unnecessary whitespace and add line breaks between tags
$buffer = preg_replace ('@\s+@', ' ', $buffer);
$buffer = preg_replace ('@\s*>\s*(.*?)\s*<\s*@', '>\\1<', $buffer);
$buffer = preg_replace ('@><@', ">\n<", $buffer);
$buffer = $buffer . "\n";
// Strip out empty 'longdesc'
$buffer = preg_replace ('@\s*longdesc="\s*"\s*@i', '', $buffer);
// Change 'one-image' to 'randomImage'
$buffer = preg_replace ('@class\s*=\s*"one-image"@i', 'class="randomImage"', $buffer);
// Change 'giDescription' to 'randomImage'
$buffer = preg_replace ('@class\s*=\s*"giDescription"@i', 'class="randomImage"', $buffer);
// Change 'giThumbnail' to 'randomImage'
$buffer = preg_replace ('@class\s*=\s*"giThumbnail"@i', 'class="randomImage"', $buffer);
// Change alt value to '[Thumbnail]'
$buffer = preg_replace ('@alt\s*=\s*"[^"]*"@i', 'alt="[Thumbnail]"', $buffer);
// Change '<h4>' tags to '<p>' tags
$buffer = preg_replace ('@<(\/?)h4([^>]*)>@i','<\\1p\\2>', $buffer);

print $buffer;

Just replace the "print $buffer;" line with the above code.

This change gives you more flexibility to define your css for this random image output independently of Gallery's css declarations. I changed the <h4> tags to <p> tags because non-css browsers always treat the contents of the "h" tags as headings. I removed empty "longdesc" statements since HTMLTidy is anal about that.

 
fabianv

Joined: 2007-08-17
Posts: 6
Posted: Fri, 2007-08-17 21:03

I managed to get this script to work but I have a problem with multiple images. Randomimages works fine but when I use Recentimages it just prints the same thumbnail repeatedly.

Is there a way that I can put it in a loop?

here is my code:

Quote:
<?php
/***************************************************
Gallery2 Get Random Image

***************************************************

* 'blocks' is a pipe (|) separated list, of one or more possible blocks which are:
* randomImage|recentImage|viewedImage|randomAlbum|recentAlbum|viewedAlbum|specificItem
* dailyImage|weeklyImage|monthlyImage|dailyAlbum|weeklyAlbum|monthlyAlbum

*/

require_once(dirname(__FILE__) . '/gallery/embed.php');

$ret = GalleryEmbed::init(array('embedUri' => '/gallery', 'relativeG2Path' => '/gallery/', 'activeUserId' => '', 'fullInit' => true ) );

list ($ret, $g2data) = GalleryEmbed::getImageBlock(array ('blocks' => 'recentImage', 'show' => 'none', 'activeUserId' => '', 'maxSize' => '67'));
list ($ret, $g2data2) = GalleryEmbed::getImageBlock(array ('blocks' => 'recentImage', 'show' => 'none', 'activeUserId' => '', 'maxSize' => '67'));

print $g2data;
print $g2data2;

?>

 
fabianv

Joined: 2007-08-17
Posts: 6
Posted: Fri, 2007-08-17 21:37

Ahhh, found a way!

Quote:
list ($ret, $g2data) = GalleryEmbed::getImageBlock(array ('blocks' => 'recentImage|recentImage', 'show' => 'none', 'activeUserId' => '', 'maxSize' => '67'));

And since im feeling friendly today :D Heres a horizontal bar solution!

After "?>" add

Quote:
<STYLE TYPE="text/css">
<!--
.one-image { float:left; }
a img{border:0px;}
-->
</STYLE>
<?php print $g2data; ?>

And you get a very nice looking horizontal bar!

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Sat, 2007-08-18 15:50
 
lingate
lingate's picture

Joined: 2003-08-21
Posts: 46
Posted: Mon, 2009-12-07 08:02
Quote:
a lot of Hosting firms are turning off php readfile() because of the security concerns

..

if thats the case, how do u turn it back ON and where do you looking for it, php.ini ?

TT