SimplePortal

Customization => Blocks and Modifications => Block Requests => Topic started by: Burke Knight on June 02, 2013, 09:31:53 PM

Title: Coppermine Gallery Random Block
Post by: Burke Knight on June 02, 2013, 09:31:53 PM
On one site, I use Coppermine Gallery in a sub-domain.
I need a block in SP for a Random Image from there.
Can anyone help with that?
Title: Re: Coppermine Gallery Random Block
Post by: Chen Zhen on June 02, 2013, 09:51:16 PM

BurkeKnight,

  You want to display images from a page that has nothing to do with the SMF database, correct?
Imo you would use cURL or a socket connection to gather the html in a string and then extract the images into an array ie. using DOM. From that point you randomly display however many images you want.
Title: Re: Coppermine Gallery Random Block
Post by: Burke Knight on June 02, 2013, 10:46:40 PM
I was hoping someone else had used the two like I am and had made a block.
Well, maybe just have to wait, then. Thanks. :)
Title: Re: Coppermine Gallery Random Block
Post by: Chen Zhen on June 02, 2013, 11:10:34 PM

BurkeKnight,

  I can make the block for you but I need more details. For instance...

Title: Re: Coppermine Gallery Random Block
Post by: Chen Zhen on June 02, 2013, 11:43:32 PM
Here is an example of scraping images from the coppermine main site & displaying 5 of them at random:

Code: [Select]
global $sourcedir;
require_once($sourcedir . '/Subs-Package.php');
$url = 'http://coppermine-gallery.net/demo/cpg15x';
$classname = 'image';
$amount = 5;
$html = fetch_web_data($url) ? fetch_web_data($url) : false;
$myimages = array();
if ($html)
{
$dom = new domDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_use_internal_errors(false);
$dom->preserveWhiteSpace = false;
$xpath = new DOMXPath($dom);
$images = $xpath->query('//img[@class="'.$classname.'"]');

foreach ($images as $image)
$myimages[] = $image->getAttribute('src');
}

if (!empty($myimages))
{
$myimages = array_unique($myimages);
shuffle($myimages);
echo '
<table style="width:100%">
<tr class="centertext">';

for ($i = 0; $i < ($amount); $i++)
{
$base = basename($myimages[$i]);
$pos = strpos($base,'thumb_');
if ($pos !== false)
$base = substr_replace($base,'',$pos,strlen('thumb_'));

$ext = '.' . pathinfo($base, PATHINFO_EXTENSION);
echo '
<td>
<img style="width:128px;height:128px;" src="',$url, '/',  $myimages[$i], '" alt="" title="'.str_replace($ext, '', $base).'" />
</td>';
}

echo '
</tr>
</table>';
}

  Since you wanted it at random I made it scrape all the images that met a specific class attribute. The problem with scraping is that when a lot of data exists on the page there is a noticeable delay especially when the page contains images (in the case there are many). One could attempt to use cURL to see if there is less lag.

\\Edit -> Added array_unique in case of duplicate images & omitted thumb_ prefix for file names (1st occurrence).
Title: Re: Coppermine Gallery Random Block
Post by: Burke Knight on June 03, 2013, 05:37:13 AM
Thank you very much, this is just what I was hoping for. :)
SimplePortal 2.3.8 © 2008-2024, SimplePortal