Announcement

  •  » Extensions
  •  » Include random pictures from a Piwigo gallery: one solution

#1 2014-10-05 15:13:47

mmoy
Member
France
2014-08-18
85

Include random pictures from a Piwigo gallery: one solution

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

 

#2 2014-10-05 22:18:55

flop25
Piwigo Team
2006-07-06
7037

Re: Include random pictures from a Piwigo gallery: one solution

Hi
thx for sharing :)


To get a better help : Politeness like Hello-A link-Your past actions precisely described
Check my extensions : more than 30 available
who I am and what I do : http://fr.gravatar.com/flop25
My gallery : an illustration of how to integrate Piwigo in your website

Offline

 

#3 2014-10-07 09:25:05

SnotRocket
Member
2014-08-15
63

Re: Include random pictures from a Piwigo gallery: one solution

Very nice.. thank you!

Offline

 

#4 2015-09-05 09:56:38

SebNYD
Member
2015-08-29
1

Re: Include random pictures from a Piwigo gallery: one solution

Thanks, this helped me A LOT !

Offline

 

#5 2015-10-02 14:41:15

Scott_T
Member
2015-09-29
9

Re: Include random pictures from a Piwigo gallery: one solution

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

 

#6 2015-10-02 15:25:35

mmoy
Member
France
2014-08-18
85

Re: Include random pictures from a Piwigo gallery: one solution

Code:

<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

 

#7 2015-10-02 15:27:30

mmoy
Member
France
2014-08-18
85

Re: Include random pictures from a Piwigo gallery: one solution

Indeed, it does work if you fix the URL. See: http://www-verimag.imag.fr/~moy/tmp/test-pr.html

Offline

 

#8 2015-10-02 20:40:52

Scott_T
Member
2015-09-29
9

Re: Include random pictures from a Piwigo gallery: one solution

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

 

#9 2015-10-02 21:35:22

mmoy
Member
France
2014-08-18
85

Re: Include random pictures from a Piwigo gallery: one solution

> 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

 

#10 2015-10-03 18:10:42

Scott_T
Member
2015-09-29
9

Re: Include random pictures from a Piwigo gallery: one solution

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&amp;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

 

#11 2015-10-03 18:32:02

mmoy
Member
France
2014-08-18
85

Re: Include random pictures from a Piwigo gallery: one solution

> 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

 

#12 2015-10-04 03:51:03

Scott_T
Member
2015-09-29
9

Re: Include random pictures from a Piwigo gallery: one solution

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

 

#13 2017-05-07 21:54:58

blacktea
Member
2017-05-03
11

Re: Include random pictures from a Piwigo gallery: one solution

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

 

#14 2017-05-07 21:58:45

blacktea
Member
2017-05-03
11

Re: Include random pictures from a Piwigo gallery: one solution

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

 
  •  » Extensions
  •  » Include random pictures from a Piwigo gallery: one solution

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2024 · Contact