Source for file helper.php

Documentation is available at helper.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Site
  4.  * @subpackage  mod_random_image
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC') or die;
  11.  
  12. /**
  13.  * Helper for mod_random_image
  14.  *
  15.  * @package     Joomla.Site
  16.  * @subpackage  mod_random_image
  17.  * @since       1.5
  18.  */
  19. {
  20.     public static function getRandomImage(&$params, $images)
  21.     {
  22.         $width    = $params->get('width');
  23.         $height    = $params->get('height');
  24.  
  25.         $i      = count($images);
  26.         $random = mt_rand(0, $i - 1);
  27.         $image  = $images[$random];
  28.         $size   = getimagesize(JPATH_BASE . '/' . $image->folder . '/' . $image->name);
  29.  
  30.         if ($width == '')
  31.         {
  32.             $width = 100;
  33.         }
  34.  
  35.         if ($size[0] < $width)
  36.         {
  37.             $width = $size[0];
  38.         }
  39.  
  40.         $coeff = $size[0] / $size[1];
  41.         if ($height == '')
  42.         {
  43.             $height = (int) ($width / $coeff);
  44.         }
  45.         else
  46.         {
  47.             $newheight = min($height, (int) ($width / $coeff));
  48.             if ($newheight < $height)
  49.             {
  50.                 $height = $newheight;
  51.             } else {
  52.                 $width = $height * $coeff;
  53.             }
  54.         }
  55.  
  56.         $image->width    = $width;
  57.         $image->height    = $height;
  58.         $image->folder    = str_replace('\\', '/', $image->folder);
  59.  
  60.         return $image;
  61.     }
  62.  
  63.     public static function getImages(&$params, $folder)
  64.     {
  65.         $type        = $params->get('type', 'jpg');
  66.  
  67.         $files    = array();
  68.         $images    = array();
  69.  
  70.         $dir = JPATH_BASE . '/' . $folder;
  71.  
  72.         // check if directory exists
  73.         if (is_dir($dir))
  74.         {
  75.             if ($handle = opendir($dir))
  76.             {
  77.                 while (false !== ($file = readdir($handle)))
  78.                 {
  79.                     if ($file != '.' && $file != '..' && $file != 'CVS' && $file != 'index.html')
  80.                     {
  81.                         $files[] = $file;
  82.                     }
  83.                 }
  84.             }
  85.             closedir($handle);
  86.  
  87.             $i = 0;
  88.             foreach ($files as $img)
  89.             {
  90.                 if (!is_dir($dir . '/' . $img))
  91.                 {
  92.                     if (preg_match('/'.$type.'/', $img))
  93.                     {
  94.                         $images[$i] = new stdClass;
  95.  
  96.                         $images[$i]->name    = $img;
  97.                         $images[$i]->folder    = $folder;
  98.                         $i++;
  99.                     }
  100.                 }
  101.             }
  102.         }
  103.  
  104.         return $images;
  105.     }
  106.  
  107.     public static function getFolder(&$params)
  108.     {
  109.         $folder    = $params->get('folder');
  110.  
  111.         $LiveSite    = JUri::base();
  112.  
  113.         // if folder includes livesite info, remove
  114.         if (JString::strpos($folder, $LiveSite) === 0)
  115.         {
  116.             $folder = str_replace($LiveSite, '', $folder);
  117.         }
  118.         // if folder includes absolute path, remove
  119.         if (JString::strpos($folder, JPATH_SITE) === 0)
  120.         {
  121.             $folder = str_replace(JPATH_BASE, '', $folder);
  122.         }
  123.         $folder = str_replace('\\', DIRECTORY_SEPARATOR, $folder);
  124.         $folder = str_replace('/', DIRECTORY_SEPARATOR, $folder);
  125.  
  126.         return $folder;
  127.     }
  128. }

Documentation generated on Tue, 19 Nov 2013 15:04:19 +0100 by phpDocumentor 1.4.3