A new Random Photo hack... very different!
zman
Joined: 2003-07-24
Posts: 196 |
Posted: Sat, 2003-09-06 18:22 |
Hello! In my quest to get my Gallery modified for my uses, I've made a random photo mod which some of you may find useful. I haven't done a lot of testing of this but it seems to work good for my needs. It is quite different than the other Gallery random photo hacks... in some ways better and in some ways not My Gallery is still "under construction" but you can get an idea of what this random photo hack looks like and does by visiting this link... http://www.extreme-z.com/appearance/gallery/fspoilers You'll notice on the right hand side of the page there are three thumbs. The first two are random photos generated from my Gallery images. The last thumb is a random ad photo that uses the same code but doesn't use images in the Gallery. I have it setup so when either one of the top two thumbs is clicked, it opens that Gallery image in the same page. When the bottom thumb is clicked it opens a new browser window and goes to the page I specify. This might sound like it is going to be difficult to implement but it is really very easy! First off, I got this idea when looking for a simple banner ad type script. I found one and after looking at it a bit, I got the idea to modify it and use it for random Gallery images instead of for rotating banner ads and the results are better than I could have hoped for... at least I'm pretty pleased Here is the code I started with... What this hack does is this... each time an image is added to the Gallery, a text file is updated with information about that image so it can be used as a random photo. This is really the only place the Gallery code itself is modified. What this means is that only images you add to your Gallery AFTER you do the hack will be used as random pictures. No images that were already in your Gallery will be used... you'll have to add these manually if you wish for them to be included. What else this means is that if you delete an image from your Gallery it goes away from your Gallery but NOT from the text file. So, there will now be a "broken link" random image and you'll need to manually edit the text file and remove the line associated with the deleted image. Like I said, this is a simple/easy mod I'm not a programmer and I really don't even know PHP so keep that in mind. LOL! What is really cool about this is that you can have as many random photos on a page as you'd like! All you need to do is "echo" the filename. Also, by creating different random text files (you could add if/then statements to the code to make a different random file for each album if you'd like) you can easily control which files are displayed where and to a degree, even how often they show up. What I mean by this is that in my example the first two random photos are from my Gallery. I could have 1000 photos in my Gallery. I could easily manually add my ad photo information to that text file but what that would mean is my 10 or so ads would have to "compete" with the other 1000 files for random viewing and would rarely show up. I didn't want this so instead just created another text file and echo'd that file instead. That file now has in it only my ad photos and they now don't have to "compete" with the Gallery photos. You can also echo the text files on non Gallery pages. This allows you to put a random photo (or two or three, etc.) on like your home page. I tried briefly to use the IFRAME command so I could keep my .htm extension but couldn't get this to work with IFRAME. I'm not sure why? If somebody has any ideas, please let me know! In the meantime, it looks like the pages which you want random photos on will have to be php pages. Now that I've described this in probably too much detail, here is the meat :D This code needs to be added to the Album.php file in your gallery/classes folder: list($w, $h) = $item->getThumbDimensions($i); if ($w <= 150 && $h <= 125) { $random_file = fopen("/home/www/extreme-z.com/htdocs/appearance/my_photos.txt","a"); $newline = $gallery->app->photoAlbumURL . "/" . $gallery->session->albumName . "/$name" . "|0|1|" . $gallery->app->albumDirURL . "/" . $gallery->session->albumName . "/$name.thumb.$tag" . "|$caption\r\n"; fwrite($random_file,"$newline"); fclose($random_file); } In the above code you'll of course need to change the path to the path of your txt file (which needs to be CHMOD 777). Also take note of the $w and $h variables (thanks beckett ;) ). These are width and height variables of the thumb. You can have several different thumb sizes and rotated pictures and all different kinds of combinations. Well, often times when you want to do a random photo on a page you want to be at least somewhat consistent with the size of the random photo so as to not mess up your page layout. So, for my case, I didn't want the photo to be added to my random photo file (my_photos.txt) if it was wider than 150 pixels or taller than 125 pixels. By putting on these restrictions I do miss out on some images added to the Gallery BUT my page layouts stay looking good and that is more important to me. You can modifiy these values to whatever you'd like. Find this portion of code and add the above where indicated... $err = $item->setPhoto($dir, $name, $tag, $this->fields["thumb_size"], $pathToThumb); if ($err) { if (fs_file_exists("$dir/$name.$tag")) { fs_unlink("$dir/$name.$tag"); } return $err; } else { $item->setCaption("$caption"); $originalItemCaptureDate = getItemCaptureDate($file); $now = time(); $item->setItemCaptureDate($originalItemCaptureDate); $item->setUploadDate($now); foreach ($extraFields as $field => $value) { $item->setExtraField($field, $value); } if (!strcmp($owner, "")) { $nobody = $gallery->userDB->getNobody(); $owner = $nobody->getUid(); } $item->setOwner($owner); // ***RANDOM PHOTO CODE LISTED ABOVE GOES HERE } $this->photos[] = $item; /* If this is the only photo, make it the highlight */ if ($this->numPhotos(1) == 1 && !$item->isMovie()) { $this->setHighlight(1); } Now, all you need to do is insert the code in your page where you want the random photo. This is even easier! For the first photo on your page use something like this but replace the path with your own. You'll notice that I've named the php file which does the random photo stuff random_picture.php...
If you want to have a second random photo on the same page it would look like this (with the name of your file)...
and for a third random photo but from a DIFFERENT random photo file, it might look something like this...
The above is actually what I use for the three random photos on my page. As you can see, you still only need to do the php include function ONCE per page. Now, for my random_picture.php file. You'll need to have this file somewhere on your server and just be sure to point to the correct path when you do the php include function. <?php /* * This file has been changed by Brent Franker for better use in Gallery * * Banner Simple, Version 1.0 * by Dan Kaplan <dan@abledesign.com> * Last Modified: January 10, 2003 * -------------------------------------------------------------------- * * USE THIS LIBRARY AT YOUR OWN RISK; no warranties are expressed or * implied. You may modify the file however you see fit, so long as * you retain this header information and any credits to other sources * throughout the file. If you make any modifications or improvements, * please send them via email to Dan Kaplan <dan@abledesign.com>. * -------------------------------------------------------------------- * * */ global $base_dir1, $img_url1, $all_photo_files, $srand_called, $last_file1, $array1; // Path to php file $base_dir1 = '/home/www/extreme-z.com/htdocs/appearance'; // URL to images which are referenced as being on-site. // Place images not in your Gallery in this folder. // Gallery images are referenced as "off-site" images. $img_url1 = 'http://www.extreme-z.com/appearance/gallery/images'; // List the file names (no extension) in the array which you will be using // for your random pictures. // In the array below, // "my_photos" is the file which is auto updated each time a picture is added // to the gallery. "my_ads" is the file which contains other pictures and ads // which are not in my Gallery. You can have as many different random picture // files as you'd like... just add the file names to the array below. $all_photo_files = array ("my_photos", "my_ads"); $srand_called = FALSE; $last_file1 = ''; $array1 = ''; function get_banner($photo_file='') { global $base_dir1, $img_url1, $all_photo_files, $srand_called, $last_file1, $array1; if (empty($last_file1) || ($photo_file != $last_file1) || !is_array($array1)) { // if calling muliple banners on one page, no need to read in the same file repeatedly. so, // if the last banner file read in is the next one called, pull the $array1 array from memory. $file1 = $base_dir1."/".$photo_file.".txt"; if (!file_exists($file1)) { // could not load the banner file, so create a default blank image and exit $banner = "<img src=\"\" border=\"0\" alt=\"broken image\">"; return $banner; } $array1 = file($file1); // read $file1 into an array $last_file1 = $photo_file; // save the last read-in file into memory } if (!$srand_called) { // slightly less random without this; srand() should only be called once for multiple banners mt_srand ((double) microtime() * 1000000); $srand_called = TRUE; } $rand1 = mt_rand(1, count($array1)-1); // start at 1 to ignore comment line $line1 = chop($array1[$rand1]); # URL | target="_blank" (1/0) | off-site image (1/0) | image | Alt text $banner_split = explode("|", $line1); $URL1 = $banner_split[0]; $blank1 = $banner_split[1]; $off_site = $banner_split[2]; $image1 = $banner_split[3]; $alt1 = $banner_split[4]; if ($blank1 == 1) { $target1 = "target=\"_blank\""; } else { $target1 = ""; } if ($off_site == 1) { $image1 = $image1; } else { $image1 = "$img_url1/$image1"; } $banner = "<a href=\"$URL1\" $target1><img src=\"$image1\" border=\"1\" alt=\"$alt1\"></a>"; return $banner; } ?> I've tried to add some extra comments to the above to give you a better idea. If you compare this file to the one original (as listed on the above referenced website) you'll see that I have done away with the "size" stuff that was important to the guy who wrote it for his banner ads. The important thing to notice is that I have two files listed in the array... "my_photos" and "my_ads". Leave off the ".txt" extension because it gets added by the code. If you have ten of these text files you'd add them all here. This should be enough to get you started :D Here are some important things to keep in mind... * As much as I'd like to be, I'm not a programmer. I don't know the terminolgy, I don't know proper formatting, I really don't know much of anything I hack my around to get what I want and what I did works good for me. If I screwed something up, please let me know! One example that I can think of that I don't understand but works is I had to define some of the variables as "global" in the random_picture.php file. This wasn't necessary so long as my page wasn't part of a Gallery page but as soon as I used this code on page within Gallery, it didn't work. I "fixed" it by making the remaining variables global. So, I have two global statements... not sure really what that does or why it didn't work before but doing this made it work so I was happy * I have not tested this to a great degree. If you have like 10,000 lines in your text file for the random photos I don't know how that's going to work. I don't know if it will slow down or what. I've tested it with like 50 images but not yet with a ton so if you do this and have a ton of images, let me know how it works please All comments are welcome and fee free to provide pointers and suggestions on how to make this better (or fix it. LOL!). Thanks to all for the help you've provided me in getting it this far along. I think I have a good start on Gallery given my extremely limited knowledge about this stuff. Thanks again! |
|
Posts: 196
Well, I've already modified it. LOL!
My Gallery has to do with two car models... Camaros and Firebirds. I thought it would be cool if the top random photo was only Camaros and the next random photo down be only Firebirds. My Album names for my Camaros all start with "c" and for the Firebirds all start with "f" so I just did some string functions and if/then statements and now have the code writing to a different text file depending on which album the picture comes from. VERY COOL! I can't believe I'm actually getting this stuff to work! LOL!
This has a lot of possibilities! One thing I'm going to use this for is my own personal Gallery for my own car pictures. I was originally going to install another Gallery for this (because I didn't want all the tons of my pics showing up in the public random photos) but now all I have to do is name my hidden album with a starting character something other than "c" or "f". Now I don't have to install another instance of Gallery... again pretty cool
Here is the new code. You'll see I changed the text file names. I did have "my_photos" for both the top and middle random photo but I now have "camaros" and "firebirds" being echo'd for these instead.
Posts: 196
I'm just getting crazy with this stuff Just made a hack which will show the 5 most recently added images in each of random text files!
I added to the above where I talked about breaking out the file into two different files... Camaros and Firebirds.
I went on the web and read a little about file handling ops and for loops and stuff like that and just added to the bottom of the Gallery another little mod... The most recent Camaro additions and then under that the most recent Firebird additions! Well, I'm lying a little because it is only the most recently added which went into my random photo file (remember above I exclude images if they are too tall or not in albums that start with "c" or "f") but that is good enough for me since 99% of pictures added will meet the size criteria!
I've stuck this at the bottom of the page in the footer. Not sure if I like that or not but it is easy enough to change the layout so I'll play with it some. I'm just pretty stoked that I got this much working so far. LOL!
Here is an example...
http://www.extreme-z.com/appearance/gallery/fwheels
I'm thinking of making each page show only 6 pictures? Hmm? If I do that it will make the foot visible all the time for people who run at a decent resolution. Good idea?
Well, if nobody gets anything out of this tread at least I'm having fun. :lol:
Posts: 33
my random image on http://www.webseth.com doesnt rely on gallery at all and can be used on any images.
i think the code is pretty easy to read, restrictoins are that its a jpg, not a thumbnail, not a highlight, and is <60kbyte (since its on my front page)
the randomdirs.txt is a plain text list of directories it will scan for pictures -- i prefer this because i can exclude albums from the pool this way.
very easy short and customizable.
- Seth
Posts: 974
Excellent work! Keep it up!
Posts: 13451
jmullan, remember to email your tits.
Posts: 974
My tits? h0bbel, you have managed to completely confuse me.
Posts: 13451
hah! I did good! Have a look at webseths site
Posts: 974
Whoa, Not Quite Safe For Work.
Posts: 196
Damn, webseth, are you trying to get me fired!?!?! I clicked on your link earlier while I was at work... not a good idea. If our Info Tech department happens to noticed where I went I hope they had on the tracking software which showed I only had the page up for about .52 seconds. We get fired for viewing stuff like that on company computers... a little warning would have been nice ;)
Posts: 33
HAH! i'm sorry i usually dont have nudity there on the front page it just happened that it was my last update on that quasiblog thing.
it could've been worse though!
- Seth
Posts: 25
Hey, zman, you're onto something! A hack that can show on another page the most recently added photos in a gallery install is quite often requested, and not yet perfected. I almost got there using a RSS feed generator from this thread, hacking it to show images rather than links. But, that script doesn't "prune," and so I'd end up with an output file with hundreds of images!
You mentioned your code can now do the 'latest x' photos in each of two separate galleries. Any chance you could post your full hack in its latest revision? And, if it's not too much trouble, do so assuming the need for only one file (rather than the two separate car files you're working with)?
Good work!
Posts: 130
I did a hack last week that lists the most recently added photos - see it working at http://www.tearoom.me.uk/gallery/stats.php?type=date The same script can also show images with the most clicks and votes. The code can be found here http://www.tearoom.me.uk/stats.tar.gz Also see http://gallery.menalto.com/index.php?name=PNphpBB2&file=viewtopic&t=13139 for more details.
Posts: 25
What a wide-ranging and elegant hack! That definitely has 'latest images' covered, as well as the voting features.
I see the stats.php file renders a full gallery page compatible with the layout of the gallery, which is especially cool. However, what changes could be made so that the output is a basic HTML file that could be called via SSI/PHP from another page on the same site, say, a weblog sidebar? The one I made from the the RSS hack puts out only the image thumbnail linked to the full image.
Posts: 25
Never mind, I did it myself! See my post here.
Posts: 98
This is great random photo mod. But it doesn't seem to resize displayed random photos, so you either have to screw up the layout of your page ---you want the sidebar narrow while most thumb photos are still larger--or, you have to resize your thumb photos to a smaller size to fit layout...
The other, original random block resizes photos so you wont screw your layout..
The good thing about this mod and the other mod doesnt have is that this mod can pull different random photos from designated albums...It's a great function if you categorize photos like what was done here by different car makes...
So wondering how to combine these nice two features in a single mod? Is it possible..any ideas? thanks...