Hello/Hi/Greetings,
I had written a small piece of PHP to include random images taken from my Piwigo gallery. I took a bit of time to clean it up and make it usable by others.
All this is now available here: https://github.com/moy/piwigo-random
If you want to see what it looks like in real-life, it's the small image at the top right of each page on http://www-verimag.imag.fr/~moy/
I think it would make sense to turn this into a Piwigo plugin: for now, there's a piece of PHP to host somewhere, and it could be hosted in the gallery itself. For now, I'm too lazy, but if anyone's interested, let me know, it should be easy.
Feedback welcome, of course.
Offline
Hi
thx for sharing :)
Offline
Very nice.. thank you!
Offline
Thanks, this helped me A LOT !
Offline
MMoy,
Appreciate you putting this out there, and I think MANY folks could us this. I've not gotten it to work yet, but if I could, it would be a solution I could use.
I am trying to show a random thumbnail in a side 'block' of a webpage. It is actually a Tinyportal block on a SMF forum. Shold be pretty strait forwad, but for some reaon I am getting no display....Any assistance appreciated, maye the simple answer is the script does not work with 2.7.4?
Piwigo version: 2.7.4
PHP version: 5.3.29
MySQL version: 5.5.45-MariaDB
Piwigo URL: http://true-grit.org/piwigo
Here is the 'block' code I used:
<span id="random_image">
<script type="text/javascript"
src="//http://www.true-grit.net/piwigo-random-backend.php"
async>
</script>
</span>
The php file being in my root. Tried permissions a few different ways to no avail....And here is the edited PHP, maybe I missed something?
<?php // -*- mode:php c-basic-offset: 2 -*-
// +-----------------------------------------------------------------------+
// | Piwigo RANDOM - Insert images from a Piwigo Galery in a website |
// +-----------------------------------------------------------------------+
// | Copyright(C) 2014 Matthieu Moy |
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License as published by |
// | the Free Software Foundation; either version 2 of the License, or |
// | (at your option) any later version. |
// | |
// | This program is distributed in the hope that it will be useful, but |
// | WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
// | General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to the Free Software |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA. |
// +-----------------------------------------------------------------------+
// Your piwigo gallery here:
$site = "http://www.true-grit.org/piwigo/";
$maximages = 1;
$cat_id = null;
$element_name = 'random_image';
$mode = 'javascript';
$target = '_blank';
if (is_numeric($_GET['maximages']))
{
$maximages = intval($_GET['maximages']);
}
if (is_numeric($_GET['cat_id']))
{
$cat_id = intval($_GET['cat_id']);
}
if (isset($_GET['element_name']))
{
$element_name = $_GET['element_name'];
}
if (isset($_GET['target']))
{
$element_name = $_GET['target'];
}
if (isset($_GET['mode']) && $_GET['mode'] == 'html') {
$mode = 'html';
}
header('Content-Type: text/javascript');
$url = $site . "ws.php" .
"?format=php" .
"&method=pwg.categories.getImages" .
($cat_id ? "&cat_id=" . $cat_id : "") .
"&recursive=true" .
"&per_page=" . $maximages .
"&page=1" .
"&order=random";
$response = file_get_contents($url);
$thumbc = unserialize($response);
if ($thumbc["stat"] === 'ok')
{
foreach ($thumbc["result"]["images"] as $image)
{
$image_url = (string)$image['derivatives']['thumb']['url'];
$page_url = (string)$image['page_url'];
$comment = (string)$image['comment'];
if ($comment === '') {
$comment = "Random image";
}
$comment .= "\n(click for full-size)";
if ($mode === 'javascript') {
// Would be a bit simpler with jquery, but let's not
// force it for such a simple piece of code.
?>
var newImg = document.createElement("img");
newImg.src = <?php echo json_encode($image_url); ?>;
newImg.alt = "";
newImg.title = <?php echo json_encode($comment) ?>;
var newLink = document.createElement("a");
newLink.href = <?php echo json_encode($page_url); ?>;
newLink.id = "rndpic-a";
newLink.appendChild(newImg);
newLink.target = <?php echo json_encode($target); ?>;
var target = document.getElementById(<?php echo json_encode($element_name); ?>);
if (!target)
{
// Could not find #random_image. As a
// fall-back, try to find the parent of the
// <script> tag calling us.
// http://stackoverflow.com/questions/6932 … ipt-tag-is
var target = document.documentElement;
while (target.childNodes.length && target.lastChild.nodeType == 1)
{
target = target.lastChild;
}
target = target.parentNode;
}
target.appendChild(newLink);
<?php
} else if ($mode === 'html') {
echo '<a id="rndpic-a" href="' . htmlspecialchars($page_url) . '"><img src="'
. htmlspecialchars($image_url) . '" alt="" title="' . htmlspecialchars($comment) . '" />'
. '</a>';
}
}
}
else
{
// Silent error.
// echo "Error";
}
Offline
<span id="random_image"> <script type="text/javascript" src="//http://www.true-grit.net/piwigo-random-backend.php" ...
Is the "//" in front of the URL intentional? I think it should be src="http://www.true-grit.net/piwigo-random-backend.php" or possibly src="//www.true-grit.net/piwigo-random-backend.php" if you want to reuse the same protocol.
Offline
Indeed, it does work if you fix the URL. See: http://www-verimag.imag.fr/~moy/tmp/test-pr.html
Offline
LOL...I hit refresh on your test and a bulldog popped up....
I tried it a few different ways, noticing the //
And now I got it...Your proof helped....
So it was the type of block....In the portal I have to choose , HTML code/Java, RSS, PHP, etc.....I was choosing PHP not thinking.....
I am truly pumped this is working.... I think it will be good for the folks using a portal with block code....BIG WIN!
Is there a way adjust the size of the thumbnail a bit?
THANKS!
Scott
Offline
> So it was the type of block....In the portal I have to choose , HTML code/Java, RSS, PHP, etc.....I was choosing PHP not thinking.....
There's also a mode where you can do the random picture stuff server-side in PHP (https://github.com/moy/piwigo-random#al … javascript), but the recommanded way does not need PHP at all, it's plain HTML.
> Is there a way adjust the size of the thumbnail a bit?
I've just pushed a version with a "size" parameter. There was no such thing until now.
Offline
So I'm not clear on this:
One can change slightly the behavior of piwigo-random by adding GET parameters to the piwigo-random-backend.php URL (e.g. src="//example.com/piwigo-random-backend.php?variable1=value&variable2=value):
Do I change the php file(if so where?) and then also add the ?size=small to the script? :
<span id="random_image">
<script type="text/javascript"
src="http://www.true-grit.net/piwigo-random-backend.php?size=small"
async>
</script>
</span>
Sorry I'm not a programmer at all...just a hack....
Regrds,
ST
Offline
> Do I change the php file
The only modification you need to do on piwigo-random-backend.php is to set your gallery's URL, which you already did.
> add the ?size=small to the script?
Yes, just like you did.
See also the examples (I've added some with size=... yesterday):
http://www-verimag.imag.fr/~moy/piwigo- … -demo.html
(see the source code, and adapt to your needs)
> src="http://www.true-grit.net/piwigo-random-backend.php?size=small"
Actually, I would recommend removing http: and keep only src="//www.true-grit.net/piwigo-random-backend.php?size=small". This way, if your page is accessed through HTTPS, the script is also downloaded through HTTPS (browsers do not like loading plain HTTP scripts from an HTTPS webpage).
Offline
Removing the http: actually seemed to speed up the thumbnail load. Appreciate that...So far, this is just perfect. I hope the search function will pick up, guess I can verify that real quick, that this is a great solution for SMF forums running a portal front end and a thumbnail inside a block.
ST
Edit: shows up in search.
Last edited by Scott_T (2015-10-04 03:52:08)
Offline
Hi, does this still work with ver. 2.9?
I have changed website address at line 515 of piwigo-random-backend.php but it doesnt work :-/
Offline
Don't mind of my previous message .... it works !!!
Thank you so much for this usefull piece of code ... it could be hours and hours of work!
Offline