Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Ograniczenie wyświetlania ilości miniatur w Simple Image Gallery
Forum PHP.pl > Forum > Przedszkole
szpuntoo
Nie potrafię sobie poradzić z problemem, który polega na ograniczeniu wyświetlania ilości miniatur (thumbs) do pierwszych 3 np.
Kod pluginu:
  1. <?php
  2. /**
  3.  * @version 3.0.1
  4.  * @package Simple Image Gallery (plugin)
  5.  * @author JoomlaWorks - <a href="http://www.joomlaworks.net" target="_blank">http://www.joomlaworks.net</a>
  6.  * @copyright Copyright (c) 2006 - 2014 JoomlaWorks Ltd. All rights reserved.
  7.  * @license GNU/GPL license: <a href="http://www.gnu.org/copyleft/gpl.html" target="_blank">http://www.gnu.org/copyleft/gpl.html</a>
  8.  */
  9.  
  10. // no direct access
  11. defined('_JEXEC') or die('Restricted access');
  12.  
  13. class SimpleImageGalleryHelper {
  14.  
  15. public static function renderGallery($srcimgfolder, $thb_width, $thb_height, $smartResize, $jpg_quality, $cache_expire_time, $gal_id)
  16. {
  17.  
  18. // API
  19. jimport('joomla.filesystem.folder');
  20.  
  21. // Path assignment
  22. $sitePath = JPATH_SITE.'/';
  23. if(JRequest::getCmd('format')=='feed')
  24. {
  25. $siteUrl = JURI::root(true).'';
  26. }
  27. else
  28. {
  29. $siteUrl = JURI::root(true).'/';
  30. }
  31.  
  32. // Internal parameters
  33. $prefix = "jw_sig_cache_";
  34.  
  35. // Set the cache folder
  36. $cacheFolderPath = JPATH_SITE.DS.'cache'.DS.'jw_sig';
  37. if (file_exists($cacheFolderPath) && is_dir($cacheFolderPath))
  38. {
  39. // all OK
  40. }
  41. else
  42. {
  43. mkdir($cacheFolderPath);
  44. }
  45.  
  46. // Check if the source folder exists and read it
  47. $srcFolder = JFolder::files($sitePath.$srcimgfolder);
  48.  
  49. // Proceed if the folder is OK or fail silently
  50. if (!$srcFolder)
  51. return;
  52.  
  53. // Loop through the source folder for images
  54. $fileTypes = array('jpg', 'jpeg', 'gif', 'png');
  55. // Create an array of file types
  56. $found = array();
  57. // Create an array for matching files
  58. foreach ($srcFolder as $srcImage)
  59. {
  60. $fileInfo = pathinfo($srcImage);
  61. if (array_key_exists('extension', $fileInfo) && in_array(strtolower($fileInfo['extension']), $fileTypes))
  62. {
  63. $found[] = $srcImage;
  64. }
  65. }
  66.  
  67. // Bail out if there are no images found
  68. if (count($found) == 0)
  69. return;
  70.  
  71. // Sort array
  72. sort($found);
  73.  
  74. // Initiate array to hold gallery
  75. $gallery = array();
  76.  
  77. // Loop through the image file list
  78. foreach ($found as $key => $filename)
  79. {
  80.  
  81. // Determine thumb image filename
  82. if (strtolower(substr($filename, -4, 4)) == 'jpeg')
  83. {
  84. $thumbfilename = substr($filename, 0, -4).'jpg';
  85. }
  86. elseif (strtolower(substr($filename, -3, 3)) == 'gif' || strtolower(substr($filename, -3, 3)) == 'png' || strtolower(substr($filename, -3, 3)) == 'jpg')
  87. {
  88. $thumbfilename = substr($filename, 0, -3).'jpg';
  89. }
  90.  
  91. // Object to hold each image elements
  92. $gallery[$key] = new JObject;
  93.  
  94. // Assign source image and path to a variable
  95. $original = $sitePath.str_replace('/', DS, $srcimgfolder).DS.$filename;
  96.  
  97. // Check if thumb image exists already
  98. $thumbimage = $cacheFolderPath.DS.$prefix.$gal_id.'_'.strtolower(self::cleanThumbName($thumbfilename));
  99.  
  100. if (file_exists($thumbimage) && is_readable($thumbimage) && (filemtime($thumbimage) + $cache_expire_time) > time())
  101. {
  102. // do nothing
  103. }
  104. else
  105. {
  106. // Otherwise create the thumb image
  107.  
  108. // begin by getting the details of the original
  109. list($width, $height, $type) = getimagesize($original);
  110.  
  111. // create an image resource for the original
  112. switch($type)
  113. {
  114. case 1 :
  115. $source = @ imagecreatefromgif($original);
  116. if (!$source)
  117. {
  118. JError::raiseNotice('', JText::_('JW_PLG_SIG_ERROR_GIFS'));
  119. return;
  120. }
  121. break;
  122. case 2 :
  123. $source = imagecreatefromjpeg($original);
  124. break;
  125. case 3 :
  126. $source = imagecreatefrompng($original);
  127. break;
  128. default :
  129. $source = NULL;
  130. }
  131.  
  132. // Bail out if the image resource is not OK
  133. if (!$source)
  134. {
  135. JError::raiseNotice('', JText::_('JW_PLG_SIG_ERROR_SRC_IMGS'));
  136. return;
  137. }
  138.  
  139. // calculate thumbnails
  140. $thumbnail = self::thumbDimCalc($width, $height, $thb_width, $thb_height, $smartResize);
  141.  
  142. $thumb_width = $thumbnail['width'];
  143. $thumb_height = $thumbnail['height'];
  144.  
  145. // create an image resource for the thumbnail
  146. $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
  147.  
  148. // create the resized copy
  149. imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
  150.  
  151. // convert and save all thumbs to .jpg
  152. $success = imagejpeg($thumb, $thumbimage, $jpg_quality);
  153.  
  154. // Bail out if there is a problem in the GD conversion
  155. if (!$success)
  156. return;
  157.  
  158. // remove the image resources from memory
  159. imagedestroy($source);
  160. imagedestroy($thumb);
  161.  
  162. }
  163.  
  164. // Assemble the image elements
  165. $gallery[$key]->filename = $filename;
  166. $gallery[$key]->sourceImageFilePath = $siteUrl.$srcimgfolder.'/'.self::replaceWhiteSpace($filename);
  167. $gallery[$key]->thumbImageFilePath = $siteUrl.'cache/jw_sig/'.$prefix.$gal_id.'_'.strtolower(self::cleanThumbName($thumbfilename));
  168. $gallery[$key]->width = $thb_width;
  169. $gallery[$key]->height = $thb_height;
  170.  
  171. }// foreach loop
  172.  
  173. // OUTPUT
  174. return $gallery;
  175.  
  176. }
  177.  
  178.  
  179.  
  180. /* ------------------ Helper Functions ------------------ */
  181.  
  182. // Calculate thumbnail dimensions
  183. public static function thumbDimCalc($width, $height, $thb_width, $thb_height, $smartResize)
  184. {
  185.  
  186. if ($smartResize)
  187. {
  188.  
  189. // thumb ratio bigger that container ratio
  190. if ($width / $height > $thb_width / $thb_height)
  191. {
  192. // wide containers
  193. if ($thb_width >= $thb_height)
  194. {
  195. // wide thumbs
  196. if ($width > $height)
  197. {
  198. $thumb_width = $thb_height * $width / $height;
  199. $thumb_height = $thb_height;
  200. }
  201. // high thumbs
  202. else
  203. {
  204. $thumb_width = $thb_height * $width / $height;
  205. $thumb_height = $thb_height;
  206. }
  207. // high containers
  208. }
  209. else
  210. {
  211. // wide thumbs
  212. if ($width > $height)
  213. {
  214. $thumb_width = $thb_height * $width / $height;
  215. $thumb_height = $thb_height;
  216. }
  217. // high thumbs
  218. else
  219. {
  220. $thumb_width = $thb_height * $width / $height;
  221. $thumb_height = $thb_height;
  222. }
  223. }
  224. }
  225. else
  226. {
  227. // wide containers
  228. if ($thb_width >= $thb_height)
  229. {
  230. // wide thumbs
  231. if ($width > $height)
  232. {
  233. $thumb_width = $thb_width;
  234. $thumb_height = $thb_width * $height / $width;
  235. }
  236. // high thumbs
  237. else
  238. {
  239. $thumb_width = $thb_width;
  240. $thumb_height = $thb_width * $height / $width;
  241. }
  242. // high containers
  243. }
  244. else
  245. {
  246. // wide thumbs
  247. if ($width > $height)
  248. {
  249. $thumb_width = $thb_height * $width / $height;
  250. $thumb_height = $thb_height;
  251. }
  252. // high thumbs
  253. else
  254. {
  255. $thumb_width = $thb_width;
  256. $thumb_height = $thb_width * $height / $width;
  257. }
  258. }
  259. }
  260.  
  261. }
  262. else
  263. {
  264.  
  265. if ($width > $height)
  266. {
  267. $thumb_width = $thb_width;
  268. $thumb_height = $thb_width * $height / $width;
  269. }
  270. elseif ($width < $height)
  271. {
  272. $thumb_width = $thb_height * $width / $height;
  273. $thumb_height = $thb_height;
  274. }
  275. else
  276. {
  277. $thumb_width = $thb_width;
  278. $thumb_height = $thb_height;
  279. }
  280.  
  281. }
  282.  
  283. $thumbnail = array();
  284. $thumbnail['width'] = round($thumb_width);
  285. $thumbnail['height'] = round($thumb_height);
  286.  
  287. return $thumbnail;
  288.  
  289. }
  290.  
  291. // Path overrides
  292. public static function getTemplatePath($pluginName, $file, $tmpl)
  293. {
  294.  
  295. $mainframe = JFactory::getApplication();
  296. $p = new JObject;
  297. $pluginGroup = 'content';
  298.  
  299. $jTemplate = $mainframe->getTemplate();
  300.  
  301. if($mainframe->isAdmin()){
  302. $db = JFactory::getDBO();
  303. if (version_compare(JVERSION, '1.6', 'ge'))
  304. {
  305. $query = "SELECT template FROM #__template_styles WHERE client_id = 0 AND home = 1";
  306. }
  307. else
  308. {
  309. $query = "SELECT template FROM #__templates_menu WHERE client_id = 0 AND menuid = 0";
  310. }
  311. $db->setQuery($query);
  312. $jTemplate = $db->loadResult();
  313. }
  314.  
  315. if (file_exists(JPATH_SITE.DS.'templates'.DS.$jTemplate.DS.'html'.DS.$pluginName.DS.$tmpl.DS.str_replace('/', DS, $file)))
  316. {
  317. $p->file = JPATH_SITE.DS.'templates'.DS.$jTemplate.DS.'html'.DS.$pluginName.DS.$tmpl.DS.$file;
  318. $p->http = JURI::root(true)."/templates/".$jTemplate."/html/{$pluginName}/{$tmpl}/{$file}";
  319. }
  320. else
  321. {
  322. if (version_compare(JVERSION, '1.6.0', 'ge'))
  323. {
  324. // Joomla! 1.6+
  325. $p->file = JPATH_SITE.DS.'plugins'.DS.$pluginGroup.DS.$pluginName.DS.$pluginName.DS.'tmpl'.DS.$tmpl.DS.$file;
  326. $p->http = JURI::root(true)."/plugins/{$pluginGroup}/{$pluginName}/{$pluginName}/tmpl/{$tmpl}/{$file}";
  327. }
  328. else
  329. {
  330. // Joomla! 1.5
  331. $p->file = JPATH_SITE.DS.'plugins'.DS.$pluginGroup.DS.$pluginName.DS.'tmpl'.DS.$tmpl.DS.$file;
  332. $p->http = JURI::root(true)."/plugins/{$pluginGroup}/{$pluginName}/tmpl/{$tmpl}/{$file}";
  333. }
  334. }
  335. return $p;
  336. }
  337.  
  338. // Replace white space
  339. public static function replaceWhiteSpace($text_to_parse)
  340. {
  341. $source_html = array(" ");
  342. $replacement_html = array("%20");
  343. return str_replace($source_html, $replacement_html, $text_to_parse);
  344. }
  345.  
  346. // Cleanup thumbnail filenames
  347. public static function cleanThumbName($text_to_parse)
  348. {
  349. $source_html = array(' ', ',');
  350. $replacement_html = array('_', '_');
  351. return str_replace($source_html, $replacement_html, $text_to_parse);
  352. }
  353.  
  354. } // End class
aras785
Może taka zmiana pomoże (nie mam jak sprawdzić):

Po linii 57 dodać:
  1. $limit =3;

Po linii 64 (po found[]) dodać:
  1. if(count($found)>=$limit) break;


I jeszcze link całym kodem + te linijki które dodałem są podświetlone na zielono: https://www.diffchecker.com/0l1x5snp

Pozdrawiam
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.