RAND_N_IMG_FROM_FOLDER: mehrere Zufallsbilder aus Ordner

Hier bekommst Du deutschsprachigen Support. Keine Fehlermeldungen oder Erweiterungswünsche bitte!
Post Reply
thoblerone
Posts: 110
Joined: Fri 27. Jan 2006, 23:34
Location: Essen, Germany
Contact:

RAND_N_IMG_FROM_FOLDER: mehrere Zufallsbilder aus Ordner

Post by thoblerone »

Hallo zusammen,

der Thread von top zu den Zufallsbildern (http://forum.phpwcms.org/viewtopic.php?f=16&t=22785) ist schon ein bisserl her, aber gerade habe ich in anderem Kontext eine Lösung als RT Render Script erstellt. Dazu habe das Breitsch'sche RAND_IMG_FROM_FOLDER aufgepeppt und nun gibt es ein RAND_N_IMG_FROM_FOLDER.

Benutztung: in CP oder Template das RT verwenden: {RAND_N_IMG_FROM_FOLDER:<<folder id>>:<<number of images>>:<<lightbox size params>>:<<WxHxCROPxQUALITY ALT-Text>>}

Dabei ist
  • <<folder id>> = ID des Dateiordners aus der Dateizentrale.
  • <<number of images>> = Anzahl der ausgewählten Zufallsbilder (Falls weniger Bilder im Ordner sind, werden weniger Bilder ausgegeben)
  • <<lightbox size params>> = WxHxCROPxQUALITY für die Lightbox. Falls leer, wird keine Lightbox erzeugt
  • <<WxHxCROPxQUALITY ALT-Text>> = Größe und Alt-Text der Bilder
Beispiel: {RAND_N_IMG_FROM_FOLDER:5:3:800x800x0x100:x150 lesen} liefert 3 zufällige Bilder mit alt-Text "lesen" aus Ordner ID 5 mit einer einheitlichen Höhe von 150px und einer Lightbox von max. 800x800 Pixeln.

Bedingung 1: /config/phpwcms/conf.inc.php

Code: Select all

$phpwcms['allow_ext_render'] = 1;
Bedingung 2 (für Lightbox): irgendwo im Template oder auch im Zufallsbild - CP

Code: Select all

<!-- CSS: template/lib/slimbox/slimbox.css -->  
<!-- JS: template/lib/slimbox/slimbox.mootools-1.3.js -->
Code /template/inc_script/frontend_render/random_multi_images_from_folder.php - entwickelt und getestet auf phpwcms 1.5.3 (2012/04/26, r471)

Code: Select all

<?php
/* ===========================================================================================
   get a number of random images from a folder V1.0  01.06.2012 thoblerone
   ===========================================================================================
   basiert auf 
   random image from folder V1.0  22.03.10  breitsch http://www.webrealisierung.ch
   -> {RAND_IMG_FROM_FOLDER:ID of PHPWCMS Folder:CSS_CLASS or HTML tag:WxHxCROPxQUALITY ALT-Text}
   The original script in /include/inc_front/ext.func.inc.php  programmed by (c) Oliver Georgi
   22.03.10  patched and swaped out to /frontend_render/* by breitsch partially copied from
   imgx V1.0  03.01.09 by K.Heermann (flip-flop)
 
   Tag:
   {RAND_N_IMG_FROM_FOLDER:<<folder id>>:<<number of images>>:<<lightbox size params>>:<<WxHxCROPxQUALITY ALT-Text>>}
 
   folder ID                 = the ID number of the Folder in the phpwcms file browser - required
   number of images          = the number of images to create. Will create max. the number of files in specified folder
   lightbox size params      = Dimensions, cropping and render quality for lightbox image style is WxHxCROPxQUALITY
                               lightbox popup relies on lightbox script and css include somewhere on your webpage.
								<!-- CSS: template/lib/slimbox/slimbox.css --> 
								<!-- JS: template/lib/slimbox/slimbox.mootools-1.3.js -->
                               leave lightbox size params empty for no lightbox   
   WxHxCROPxQUALITY ALT-Text = Dimensions and alt text of the image (alt text only when not used as background image)
                               if empty and css background image it takes the original dimensions of the image
                               if empty and not css background image it takes the preview dimensions for the images
 
============================================================================================= */
 
// ----------------------------------------------------------------
// obligate check for phpwcms constants
if (!defined('PHPWCMS_ROOT')) {
   die("You Cannot Access This Script Directly, Have a Nice Day."); }
// ----------------------------------------------------------------
 
if( strpos($content['all'], '{RAND_N_IMG_FROM_FOLDER:') !== FALSE ) {
 
function parse_nimagesfromfolder($matches) {
// $matches[0] = complete RT
// $matches[1] = ID of Folder
// $matches[2] = number of images (leave empty for 1 image)
// $matches[3] = leave empty = no lightbox , else Lightbox cmsimage.php Parameters (WxHxCROPxQUALITY e.g. 800x800x0x100)
// $matches[4] = WxHxCROPxQUALITY ALT-Text


	$nImages = 1;
	 if (isset ($matches[2]) && $matches[2] !== "") {
		$nImages = $matches[2];
	}


	 
   if(isset($matches[1])) {
 
      // Folder ID
      $folder_id    = intval($matches[1]);
 
      // check for Alt-Text
      $alt      = explode(' ', $matches[4], 2);       // WxHxCROPxQUALITY ALT-Text
      // $alt[0] = WxHxCROPxQUALITY
      // $alt[1] = ALT-Text

	
      $value    = explode('x', trim(strtolower($alt[0])));
      $alttext  = isset($alt[1])    ? trim($alt[1]) : '';
 
      $alt      = isset($alt[0]) ? html_specialchars(trim($alt[0])) : ''; // WxHxCROPxQUALITY
 
      $width    = isset($value[ 0 ])  ? intval($value[ 0 ]) : 0;
      $height   = isset($value[ 1 ])  ? intval($value[ 1 ]) : 0;
      $crop     = isset($value[ 2 ]) && intval($value[ 2 ]) === 1 ? 1 : 0;
      $quality  = isset($value[ 3 ])  ? intval($value[ 3 ]) : 0;
 
      $sql = 'SELECT * FROM '.DB_PREPEND.'phpwcms_file WHERE f_aktiv=1 AND f_public=1 AND f_trash=0 AND f_pid='.$folder_id.' AND f_ext IN ("jpg", "png", "gif")';
        $result = mysql_query($sql);
        $i=0;
        while($folderfile = mysql_fetch_array($result)) {
          $folderfiles[$i] = $folderfile;
          $i++;
        }
 		
		/* use max. as many images as there are in the folder */
		if ($i < $nImages)
			$nImages = $i;
		
        $random = array_rand($folderfiles, $nImages);

		if ($nImages == 1) /* special case 1 file: array_rand returns single value instead of array of indices */
			$random = array( $random );
		
		shuffle($random);
		
        $folderfile_output  = '';		
		
		for ($idxImage = 0; $idxImage<$nImages; $idxImage++)
		{
			$folderfile = $folderfiles[$random[$idxImage]];
 
			if(isset($folderfile['f_id']) ) {
				// $thumb_info = @getimagesize(PHPWCMS_URL.PHPWCMS_FILES.$folderfile["f_hash"] . '.' . $folderfile["f_ext"]);
				$thumb_info = @getimagesize(PHPWCMS_FILES.$folderfile["f_hash"] . '.' . $folderfile["f_ext"]);

				(isset ($value[ 0 ])) ? $thumb_info[0] = $value[0] : $thumb_info[0] = $thumb_info[0];
				(isset ($value[ 1 ])) ? $thumb_info[1] = $value[1] : $thumb_info[1] = $thumb_info[1];

				$zoominfo    = get_cached_image(
				  array(   "target_ext"   =>   $folderfile["f_ext"],
							"image_name"  =>   $folderfile["f_hash"] . '.' . $folderfile["f_ext"],
							"max_width"   =>   $thumb_info[0],
							"max_height"  =>   $thumb_info[1],
							"thumb_name"  =>   md5(   $folderfile["f_hash"].$thumb_info[0].
												   $thumb_info[1].$GLOBALS['phpwcms']["sharpen_level"].$crop),
							'crop_image'  =>   $crop
				  )
				);
				$folderfile_path  = PHPWCMS_IMAGES.$zoominfo[0];
			} else {
			  $folderfile_path  = '';
			}
 
			$image      = '<img src="'.PHPWCMS_URL.'img/cmsimage.php/'.$width.'x'.$height.'x'.$crop;
			if($quality <= 100 && $quality >= 10) {
			$image .= 'x'.$quality;
			}
			$image     .= '/'.$folderfile['f_id'].'.'.$folderfile["f_ext"].'" alt="'.$alttext.'" border="0" />';

			/* TB lightbox */
			if (isset ($matches[3]) && $matches[3] !== "")
			{
				$lightbox = '<a title="'.$folderfile['f_longinfo'].'" rel="lightbox" target="_blank" href="'
								. PHPWCMS_URL.'img/cmsimage.php/' . $matches[3];
				$lightbox     .= '/'.$folderfile['f_id'].'.'.$folderfile["f_ext"].'">';
				
				$image = $lightbox . $image . '</a>';
			}
			/* TB lightbox ende*/

			// append current image to output 
			$folderfile_output  .= $image;

	   }
       return $folderfile_output;
   }
 
   return '<img src="'.PHPWCMS_URL.'img/leer.gif" alt="" border="0" />';
 
}
 
$content["all"] = preg_replace_callback( '/\{RAND_N_IMG_FROM_FOLDER:(\d+):(\d+):(.*?):(.*?)\}/i', 'parse_nimagesfromfolder', $content["all"]);

} // end if
 
?>
Post Reply