[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/modules/mod_random_image/src/Helper/ -> RandomImageHelper.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  mod_random_image
   6   *
   7   * @copyright   (C) 2006 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Module\RandomImage\Site\Helper;
  12  
  13  use Joomla\CMS\Uri\Uri;
  14  use Joomla\String\StringHelper;
  15  
  16  // phpcs:disable PSR1.Files.SideEffects
  17  \defined('_JEXEC') or die;
  18  // phpcs:enable PSR1.Files.SideEffects
  19  
  20  /**
  21   * Helper for mod_random_image
  22   *
  23   * @since  1.5
  24   */
  25  class RandomImageHelper
  26  {
  27      /**
  28       * Retrieves a random image
  29       *
  30       * @param   \Joomla\Registry\Registry  &$params  module parameters object
  31       * @param   array                      $images   list of images
  32       *
  33       * @return  mixed
  34       */
  35      public static function getRandomImage(&$params, $images)
  36      {
  37          $width  = $params->get('width', 100);
  38          $height = $params->get('height', null);
  39  
  40          $i = \count($images);
  41  
  42          if ($i === 0) {
  43              return null;
  44          }
  45  
  46          $random = mt_rand(0, $i - 1);
  47          $image  = $images[$random];
  48          $size   = getimagesize(JPATH_BASE . '/' . $image->folder . '/' . $image->name);
  49  
  50          if ($size[0] < $width) {
  51              $width = $size[0];
  52          }
  53  
  54          $coeff = $size[0] / $size[1];
  55  
  56          if ($height === null) {
  57              $height = (int) ($width / $coeff);
  58          } else {
  59              $newheight = min($height, (int) ($width / $coeff));
  60  
  61              if ($newheight < $height) {
  62                  $height = $newheight;
  63              } else {
  64                  $width = $height * $coeff;
  65              }
  66          }
  67  
  68          $image->width  = $width;
  69          $image->height = $height;
  70          $image->folder = str_replace('\\', '/', $image->folder);
  71  
  72          return $image;
  73      }
  74  
  75      /**
  76       * Retrieves images from a specific folder
  77       *
  78       * @param   \Joomla\Registry\Registry  &$params  module params
  79       * @param   string                     $folder   folder to get the images from
  80       *
  81       * @return  array
  82       */
  83      public static function getImages(&$params, $folder)
  84      {
  85          $type   = $params->get('type', 'jpg');
  86          $files  = [];
  87          $images = [];
  88  
  89          $dir = JPATH_BASE . '/' . $folder;
  90  
  91          // Check if directory exists
  92          if (is_dir($dir)) {
  93              if ($handle = opendir($dir)) {
  94                  while (false !== ($file = readdir($handle))) {
  95                      if ($file !== '.' && $file !== '..' && $file !== 'CVS' && $file !== 'index.html') {
  96                          $files[] = $file;
  97                      }
  98                  }
  99              }
 100  
 101              closedir($handle);
 102  
 103              $i = 0;
 104  
 105              foreach ($files as $img) {
 106                  if (!is_dir($dir . '/' . $img) && preg_match('/' . $type . '/', $img)) {
 107                      $images[$i] = new \stdClass();
 108  
 109                      $images[$i]->name   = $img;
 110                      $images[$i]->folder = $folder;
 111                      $i++;
 112                  }
 113              }
 114          }
 115  
 116          return $images;
 117      }
 118  
 119      /**
 120       * Get sanitized folder
 121       *
 122       * @param   \Joomla\Registry\Registry  &$params  module params objects
 123       *
 124       * @return  mixed
 125       */
 126      public static function getFolder(&$params)
 127      {
 128          $folder   = $params->get('folder');
 129          $LiveSite = Uri::base();
 130  
 131          // If folder includes livesite info, remove
 132          if (StringHelper::strpos($folder, $LiveSite) === 0) {
 133              $folder = str_replace($LiveSite, '', $folder);
 134          }
 135  
 136          // If folder includes absolute path, remove
 137          if (StringHelper::strpos($folder, JPATH_SITE) === 0) {
 138              $folder = str_replace(JPATH_BASE, '', $folder);
 139          }
 140  
 141          return str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $folder);
 142      }
 143  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer