, Guest!
Already a Member? Login or Register.

Menu



Showcase


Due to the volume of spam happening on our forums, posting is now restricted to verified members only.  If you're not verified, drop us a note with your username.

Home > FlexCMS Support Forum > User Help > Code Snippets > Random photo album image in a block

FlexCMS Support Forum


Random photo album image in a block
Started August 6, 2008 @ 1:13am by Wolf
Post Message 
Wolf




Posts: 65
 
Random photo album image in a blockAugust 6, 2008 @ 1:13am
O Great and Powerful Oz...

I'd like to pull a random image from a random photo album and display it in a block.

Would you have a down and dirty way of doing that?

Thanks,
Wolf
 
DCSun
Administrator



Posts: 625
 
August 6, 2008 @ 1:21pm
Hey Wolfie,

You're in luck, we happen to have just the thing you're looking for:


Code

$query = "select * from `".$Settings['DBPrefix']."pg-Settings`";
$result = mysql_query($query) or die (mysql_error());
while ($row = mysql_fetch_array($result)) {
$pgSettings[$row['Name']] = $row['Value'];
}

if ($pgSettings['StorageLocationHTTP'] == '') {
$pgSettings['StorageLocationHTTP'] = $BaseURL.'/pg_images';
}
if ($pgSettings['StorageLocationLocal'] == '') {
$pgSettings['StorageLocationLocal'] = './pg_images';
}

$Extensions[2] = '.jpg';
$Extensions[3] = '.png';

$ThumbExtensions[2] = '-thumb.jpg';
$ThumbExtensions[3] = '-thumb.png';

if (substr($pgSettings['StorageLocationHTTP'],0,1) == '/') {
$ImageBaseHTTP = $BaseURL.$pgSettings['StorageLocationHTTP'];
}
else {
$ImageBaseHTTP = $pgSettings['StorageLocationHTTP'];
}



$query = "select * from `".$Settings['DBPrefix']."pg-Images` ORDER BY RAND() limit 1";
$result = mysql_query($query) or die (mysql_error());
$row = mysql_fetch_array($result);


// figure out thumbnail dimensions
$NewWidth = $pgSettings['ThumbMaxWidth'];
$NewHeight = $pgSettings['ThumbMaxHeight'];

$WidthRatio = $row['ImageWidth'] / $NewWidth;
$HeightRatio = $row['ImageHeight'] / $NewHeight;

if ($WidthRatio >= $HeightRatio) {
$NewWidthCalc = number_format($row['ImageWidth'] / $WidthRatio, 0);
$NewHeightCalc = number_format($row['ImageHeight'] / $WidthRatio, 0);
}
else {
$NewWidthCalc = number_format($row['ImageWidth'] / $HeightRatio, 0);
$NewHeightCalc = number_format($row['ImageHeight'] / $HeightRatio, 0);
}

$ImageWidth = $NewWidthCalc;
$ImageHeight = $NewHeightCalc;


$query2 = "select * from `".$Settings['DBPrefix']."pg-Galleries` where RecordNumber='".$row['GalleryNumber']."' limit 1";
$result2 = mysql_query($query2) or die (mysql_error());
$row2 = mysql_fetch_array($result2);

print '<a href="'.$MainURL.'/photos/'.$row2['RecordNumber'].'.html" class="BlockColors"><img src="'.$ImageBaseHTTP.'/'.$row['FileName'].$ThumbExtensions[$row['FileType']].'" width="'.$ImageWidth.'" height="'.$ImageHeight.'" border="0" alt=""><br>'.$row2['GalleryTitle'].'</a>';



David


FlexCMS v3.2 Has Been Released!
 
Wolf




Posts: 65
 
August 6, 2008 @ 2:40pm
OMGosh... that is SO COOL. Thanks heaps! You really rock.

Thanks,
Wolfie
 
welby


Posts: 34
 
Great Code SnippetAugust 10, 2010 @ 7:53pm
I just added this to one of my sites and its fantastic.

Thanks for sharing.

Simply the best CMS and support around.

welby
 
welby


Posts: 34
 
Random photo image in a blockOctober 12, 2011 @ 10:02pm
Hello David/Otter.

I know the code in the previous thread populates in a block if you already have a photo album with photos but I was wondering is it possible to randomly display an image from a image directory which contains photos. Another words I don't have a photo album but I do have many images in my image directory.

Thanks for any consideration.

Welby
 
DCSun
Administrator



Posts: 625
 
October 12, 2011 @ 10:09pm
Hi Welby,

I don't think there's any existing code that will do that, but it can certainly be done. Are the images going to be displayed at the same size (ie they're already small enough to fit in a block), or do they need to be dynamically resized into a thumbnail version for use in the block?


David


FlexCMS v3.2 Has Been Released!
 
Wolf


Posts: 11
 
October 13, 2011 @ 12:13am
Couldn't you upload them using file manager to a directory that flex knows about and then run some code against that directory like this...


Code


$query = "SELECT `Location` , `Filename` FROM `core-Files` WHERE `Location` = \"/newsletters/pdf/\" ORDER BY Filename desc";
$result = mysql_query($query) or die (mysql_error());

print '<b>'.number_format(mysql_num_rows($result),0).' Newsletters available' . '</b><br>';

while ($row = mysql_fetch_array($result))
{ print '<a href="'.$BaseURL.$row['Location'].$row['Filename'].'" target="_blank">'.$row['Filename'].'</a><br>';
}



I use this on a site to create links for uploaded newsletters that users can click on to download and read. Could be modified to display thumbnails and images.

Dave, you probably can tell us of a way to emulate populating his image directory contents using Flex/PHP code and populating/updating the core-Files table. Then he could query the table for the images like I did for my PDF newsletters. Smile

 
welby


Posts: 34
 
Random photo album image in a blockOctober 13, 2011 @ 6:48pm
Thanks David and Wolf.

David no worries, I'll just install the photo gallery module and use the original code. Might be a cool idea for a future code snippet.

Thanks.

Welby
 

Post Message 




Try & Buy FeedForAll - Easy to use RSS Feed Creator - great for iTunes users!

MEMBERS




All Contents, Code, Scripts and Technologies Copyright 2003-2009 FlexCMS.
All Rights Reserved. Software License Agreement

Processing Time: 0.07051 seconds.
 
Management Login

Powered By FlexCMS
Powered By FlexCMS