Here is a rough patch that I worked up for our site that allows me to maintain the slideshow just by adding/removing images from the images/slideshows directory. That is, the PHP just does an opendir() and readdir() to find all the image files in the directory, and them emits an HTML for each one. That way, if we ever want to add/remove an image from the slideshow, we just add or remove a file from the slideshows directory -- there's no need to edit the theme page template.
The code is fairly rough, and leaves a lot of room for improvement, e.g.:
- Add a text file for captions
- Letting the user customize the directory where the slideshow files are located
- Having specific timeframes where specific images can appear in the slideshow (e.g., image foo.jpg will only appear in the slideshow on June 8th, etc.)
Here's what I did -- hopefully it's of use to someone:
--- /home/bsfnhoa/drupal/danland/page.tpl.php 2010-11-19 08:42:44.000000000 -0600
+++ page.tpl.php 2011-06-01 05:12:55.000000000 -0500
@@ -80,10 +80,30 @@
<?php if ($page['highlighted']) : ?><div id="slideshow-bottom">
<div id="mission"><?php print render ($page['highlighted']); ?></div></div><?php endif; ?>
<div class="slideshow">
-<img src="<?php print $base_path . $directory; ?>/images/slideshows/sea.jpg" width="950" height="355" alt="slideshow 1"/>
-<img src="<?php print $base_path . $directory; ?>/images/slideshows/noon.jpg" width="950" height="355" alt="slideshow 2"/>
-<img src="<?php print $base_path . $directory; ?>/images/slideshows/snow.jpg" width="950" height="355" alt="slideshow 3"/>
-</div>
+<?php
+$dir = drupal_get_path("theme", "danland") . "/images/slideshows";
+if (is_dir($dir)) {
+ $d = opendir($dir);
+ while (($f = readdir($d)) != false) {
+ if (!empty($f) && $f != "." && $f != "..") {
+ $files[] = $f;
+ }
+ continue;
+ }
+ closedir($d);
+
+ sort($files);
+ $i = 1;
+ foreach ($files as $file) {
+ # Best sizes are 950x355
+ $info = getimagesize("$dir/$file");
+ if ($info[3] != "") {
+ print("<img src=\"$base_path$directory/images/slideshows/$file\" $info[3] alt=\"slideshow $i\"/>\n");
+ ++$i;
+ }
+ }
+}
+?></div>
</div>
</div>
<?php endif; ?>
--- /home/bsfnhoa/drupal/danland/maintenance-page.tpl.php 2011-01-06 01:45:39.000000000 -0600
+++ maintenance-page.tpl.php 2011-06-01 05:15:34.000000000 -0500
@@ -69,10 +69,30 @@
<?php if ($content): ?><div id="slideshow-bottom">
<div id="content-maintenance"><?php print $content; ?></div></div><?php endif; ?>
<div class="slideshow">
-<img src="<?php print $base_path . $directory; ?>/images/slideshows/sea.jpg" width="950" height="355" alt="slideshow 1"/>
-<img src="<?php print $base_path . $directory; ?>/images/slideshows/noon.jpg" width="950" height="355" alt="slideshow 2"/>
-<img src="<?php print $base_path . $directory; ?>/images/slideshows/snow.jpg" width="950" height="355" alt="slideshow 3"/>
-</div>
+<?php
+$dir = drupal_get_path("theme", "danland") . "/images/slideshows";
+if (is_dir($dir)) {
+ $d = opendir($dir);
+ while (($f = readdir($d)) != false) {
+ if (!empty($f) && $f != "." && $f != "..") {
+ $files[] = $f;
+ }
+ continue;
+ }
+ closedir($d);
+
+ sort($files);
+ $i = 1;
+ foreach ($files as $file) {
+ # Best sizes are 950x355
+ $info = getimagesize("$dir/$file");
+ if ($info[3] != "") {
+ print("<img src=\"$base_path$directory/images/slideshows/$file\" $info[3] alt=\"slideshow $i\"/>\n");
+ ++$i;
+ }
+ }
+}
+?></div>
</div>
</div>
Comment | File | Size | Author |
---|---|---|---|
#3 | header-rotation-1175194-3.patch | 1.72 KB | doka |
Comments
Comment #1
danpros CreditAttribution: danpros commentedHi,
Thank you for the code. Change it to need review.
Dan
Comment #2
rthuey CreditAttribution: rthuey commentedexperience level - first drupal site.
at first i was using bluehost.com's online code edit function. thinking the below was a bug with their editor. i have since tried this using Coda on my mac. the result is the same as stated below
i can add the second half of the patch and see no changes nor get any errors
whether i make the second half changes first or make only the changes from the first half of the patch. when i load it to the site and refresh, i get an error at line 86. if i then open the page that was just uploaded there is no code beyond line 85. which before the refresh there was more than 200 lines of code. weird to me. the last line that shows is 85.
this is what i am starting with, that works before adding the patch.
Comment #3
doka CreditAttribution: doka commentedplease find my patch: it takes all the pictures from /images/slideshows, and rotates them.
Comment #5
jcnventura CreditAttribution: jcnventura commented