Untitled diff

Created Diff never expires
0 removals
Lines
Total
Removed
Words
Total
Removed
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
354 lines
3 additions
Lines
Total
Added
Words
Total
Added
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
357 lines
<?php
<?php
/**
/**
* @version 3.0.1
* @version 3.0.1
* @package Simple Image Gallery (plugin)
* @package Simple Image Gallery (plugin)
* @author JoomlaWorks - http://www.joomlaworks.net
* @author JoomlaWorks - http://www.joomlaworks.net
* @copyright Copyright (c) 2006 - 2014 JoomlaWorks Ltd. All rights reserved.
* @copyright Copyright (c) 2006 - 2014 JoomlaWorks Ltd. All rights reserved.
* @license GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
* @license GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
*/
*/
// no direct access
// no direct access
defined('_JEXEC') or die('Restricted access');
defined('_JEXEC') or die('Restricted access');
class SimpleImageGalleryHelper {
class SimpleImageGalleryHelper {
public static function renderGallery($srcimgfolder, $thb_width, $thb_height, $smartResize, $jpg_quality, $cache_expire_time, $gal_id)
public static function renderGallery($srcimgfolder, $thb_width, $thb_height, $smartResize, $jpg_quality, $cache_expire_time, $gal_id)
{
{
// API
// API
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.folder');
// Path assignment
// Path assignment
$sitePath = JPATH_SITE.'/';
$sitePath = JPATH_SITE.'/';
if(JRequest::getCmd('format')=='feed')
if(JRequest::getCmd('format')=='feed')
{
{
$siteUrl = JURI::root(true).'';
$siteUrl = JURI::root(true).'';
}
}
else
else
{
{
$siteUrl = JURI::root(true).'/';
$siteUrl = JURI::root(true).'/';
}
}
// Internal parameters
// Internal parameters
$prefix = "jw_sig_cache_";
$prefix = "jw_sig_cache_";
// Set the cache folder
// Set the cache folder
$cacheFolderPath = JPATH_SITE.DS.'cache'.DS.'jw_sig';
$cacheFolderPath = JPATH_SITE.DS.'cache'.DS.'jw_sig';
if (file_exists($cacheFolderPath) && is_dir($cacheFolderPath))
if (file_exists($cacheFolderPath) && is_dir($cacheFolderPath))
{
{
// all OK
// all OK
}
}
else
else
{
{
mkdir($cacheFolderPath);
mkdir($cacheFolderPath);
}
}
// Check if the source folder exists and read it
// Check if the source folder exists and read it
$srcFolder = JFolder::files($sitePath.$srcimgfolder);
$srcFolder = JFolder::files($sitePath.$srcimgfolder);
// Proceed if the folder is OK or fail silently
// Proceed if the folder is OK or fail silently
if (!$srcFolder)
if (!$srcFolder)
return;
return;
// Loop through the source folder for images
// Loop through the source folder for images
$fileTypes = array('jpg', 'jpeg', 'gif', 'png');
$fileTypes = array('jpg', 'jpeg', 'gif', 'png');
// Create an array of file types
// Create an array of file types
$found = array();
$found = array();
// Create an array for matching files
// Create an array for matching files
$limit = 3;
foreach ($srcFolder as $srcImage)
foreach ($srcFolder as $srcImage)
{
{
$fileInfo = pathinfo($srcImage);
$fileInfo = pathinfo($srcImage);
if (array_key_exists('extension', $fileInfo) && in_array(strtolower($fileInfo['extension']), $fileTypes))
if (array_key_exists('extension', $fileInfo) && in_array(strtolower($fileInfo['extension']), $fileTypes))
{
{
$found[] = $srcImage;
$found[] = $srcImage;
if(count($found)>=3) break;
}
}
}
}
// Bail out if there are no images found
// Bail out if there are no images found
if (count($found) == 0)
if (count($found) == 0)
return;
return;
// Sort array
// Sort array
sort($found);
sort($found);
// Initiate array to hold gallery
// Initiate array to hold gallery
$gallery = array();
$gallery = array();
// Loop through the image file list
// Loop through the image file list
foreach ($found as $key => $filename)
foreach ($found as $key => $filename)
{
{
// Determine thumb image filename
// Determine thumb image filename
if (strtolower(substr($filename, -4, 4)) == 'jpeg')
if (strtolower(substr($filename, -4, 4)) == 'jpeg')
{
{
$thumbfilename = substr($filename, 0, -4).'jpg';
$thumbfilename = substr($filename, 0, -4).'jpg';
}
}
elseif (strtolower(substr($filename, -3, 3)) == 'gif' || strtolower(substr($filename, -3, 3)) == 'png' || strtolower(substr($filename, -3, 3)) == 'jpg')
elseif (strtolower(substr($filename, -3, 3)) == 'gif' || strtolower(substr($filename, -3, 3)) == 'png' || strtolower(substr($filename, -3, 3)) == 'jpg')
{
{
$thumbfilename = substr($filename, 0, -3).'jpg';
$thumbfilename = substr($filename, 0, -3).'jpg';
}
}
// Object to hold each image elements
// Object to hold each image elements
$gallery[$key] = new JObject;
$gallery[$key] = new JObject;
// Assign source image and path to a variable
// Assign source image and path to a variable
$original = $sitePath.str_replace('/', DS, $srcimgfolder).DS.$filename;
$original = $sitePath.str_replace('/', DS, $srcimgfolder).DS.$filename;
// Check if thumb image exists already
// Check if thumb image exists already
$thumbimage = $cacheFolderPath.DS.$prefix.$gal_id.'_'.strtolower(self::cleanThumbName($thumbfilename));
$thumbimage = $cacheFolderPath.DS.$prefix.$gal_id.'_'.strtolower(self::cleanThumbName($thumbfilename));
if (file_exists($thumbimage) && is_readable($thumbimage) && (filemtime($thumbimage) + $cache_expire_time) > time())
if (file_exists($thumbimage) && is_readable($thumbimage) && (filemtime($thumbimage) + $cache_expire_time) > time())
{
{
// do nothing
// do nothing
}
}
else
else
{
{
// Otherwise create the thumb image
// Otherwise create the thumb image
// begin by getting the details of the original
// begin by getting the details of the original
list($width, $height, $type) = getimagesize($original);
list($width, $height, $type) = getimagesize($original);
// create an image resource for the original
// create an image resource for the original
switch($type)
switch($type)
{
{
case 1 :
case 1 :
$source = @ imagecreatefromgif($original);
$source = @ imagecreatefromgif($original);
if (!$source)
if (!$source)
{
{
JError::raiseNotice('', JText::_('JW_PLG_SIG_ERROR_GIFS'));
JError::raiseNotice('', JText::_('JW_PLG_SIG_ERROR_GIFS'));
return;
return;
}
}
break;
break;
case 2 :
case 2 :
$source = imagecreatefromjpeg($original);
$source = imagecreatefromjpeg($original);
break;
break;
case 3 :
case 3 :
$source = imagecreatefrompng($original);
$source = imagecreatefrompng($original);
break;
break;
default :
default :
$source = NULL;
$source = NULL;
}
}
// Bail out if the image resource is not OK
// Bail out if the image resource is not OK
if (!$source)
if (!$source)
{
{
JError::raiseNotice('', JText::_('JW_PLG_SIG_ERROR_SRC_IMGS'));
JError::raiseNotice('', JText::_('JW_PLG_SIG_ERROR_SRC_IMGS'));
return;
return;
}
}
// calculate thumbnails
// calculate thumbnails
$thumbnail = self::thumbDimCalc($width, $height, $thb_width, $thb_height, $smartResize);
$thumbnail = self::thumbDimCalc($width, $height, $thb_width, $thb_height, $smartResize);
$thumb_width = $thumbnail['width'];
$thumb_width = $thumbnail['width'];
$thumb_height = $thumbnail['height'];
$thumb_height = $thumbnail['height'];
// create an image resource for the thumbnail
// create an image resource for the thumbnail
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
// create the resized copy
// create the resized copy
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
// convert and save all thumbs to .jpg
// convert and save all thumbs to .jpg
$success = imagejpeg($thumb, $thumbimage, $jpg_quality);
$success = imagejpeg($thumb, $thumbimage, $jpg_quality);
// Bail out if there is a problem in the GD conversion
// Bail out if there is a problem in the GD conversion
if (!$success)
if (!$success)
return;
return;
// remove the image resources from memory
// remove the image resources from memory
imagedestroy($source);
imagedestroy($source);
imagedestroy($thumb);
imagedestroy($thumb);
}
}
// Assemble the image elements
// Assemble the image elements
$gallery[$key]->filename = $filename;
$gallery[$key]->filename = $filename;
$gallery[$key]->sourceImageFilePath = $siteUrl.$srcimgfolder.'/'.self::replaceWhiteSpace($filename);
$gallery[$key]->sourceImageFilePath = $siteUrl.$srcimgfolder.'/'.self::replaceWhiteSpace($filename);
$gallery[$key]->thumbImageFilePath = $siteUrl.'cache/jw_sig/'.$prefix.$gal_id.'_'.strtolower(self::cleanThumbName($thumbfilename));
$gallery[$key]->thumbImageFilePath = $siteUrl.'cache/jw_sig/'.$prefix.$gal_id.'_'.strtolower(self::cleanThumbName($thumbfilename));
$gallery[$key]->width = $thb_width;
$gallery[$key]->width = $thb_width;
$gallery[$key]->height = $thb_height;
$gallery[$key]->height = $thb_height;
}// foreach loop
}// foreach loop
// OUTPUT
// OUTPUT
return $gallery;
return $gallery;
}
}
/* ------------------ Helper Functions ------------------ */
/* ------------------ Helper Functions ------------------ */
// Calculate thumbnail dimensions
// Calculate thumbnail dimensions
public static function thumbDimCalc($width, $height, $thb_width, $thb_height, $smartResize)
public static function thumbDimCalc($width, $height, $thb_width, $thb_height, $smartResize)
{
{
if ($smartResize)
if ($smartResize)
{
{
// thumb ratio bigger that container ratio
// thumb ratio bigger that container ratio
if ($width / $height > $thb_width / $thb_height)
if ($width / $height > $thb_width / $thb_height)
{
{
// wide containers
// wide containers
if ($thb_width >= $thb_height)
if ($thb_width >= $thb_height)
{
{
// wide thumbs
// wide thumbs
if ($width > $height)
if ($width > $height)
{
{
$thumb_width = $thb_height * $width / $height;
$thumb_width = $thb_height * $width / $height;
$thumb_height = $thb_height;
$thumb_height = $thb_height;
}
}
// high thumbs
// high thumbs
else
else
{
{
$thumb_width = $thb_height * $width / $height;
$thumb_width = $thb_height * $width / $height;
$thumb_height = $thb_height;
$thumb_height = $thb_height;
}
}
// high containers
// high containers
}
}
else
else
{
{
// wide thumbs
// wide thumbs
if ($width > $height)
if ($width > $height)
{
{
$thumb_width = $thb_height * $width / $height;
$thumb_width = $thb_height * $width / $height;
$thumb_height = $thb_height;
$thumb_height = $thb_height;
}
}
// high thumbs
// high thumbs
else
else
{
{
$thumb_width = $thb_height * $width / $height;
$thumb_width = $thb_height * $width / $height;
$thumb_height = $thb_height;
$thumb_height = $thb_height;
}
}
}
}
}
}
else
else
{
{
// wide containers
// wide containers
if ($thb_width >= $thb_height)
if ($thb_width >= $thb_height)
{
{
// wide thumbs
// wide thumbs
if ($width > $height)
if ($width > $height)
{
{
$thumb_width = $thb_width;
$thumb_width = $thb_width;
$thumb_height = $thb_width * $height / $width;
$thumb_height = $thb_width * $height / $width;
}
}
// high thumbs
// high thumbs
else
else
{
{
$thumb_width = $thb_width;
$thumb_width = $thb_width;
$thumb_height = $thb_width * $height / $width;
$thumb_height = $thb_width * $height / $width;
}
}
// high containers
// high containers
}
}
else
else
{
{
// wide thumbs
// wide thumbs
if ($width > $height)
if ($width > $height)
{
{
$thumb_width = $thb_height * $width / $height;
$thumb_width = $thb_height * $width / $height;
$thumb_height = $thb_height;
$thumb_height = $thb_height;
}
}
// high thumbs
// high thumbs
else
else
{
{
$thumb_width = $thb_width;
$thumb_width = $thb_width;
$thumb_height = $thb_width * $height / $width;
$thumb_height = $thb_width * $height / $width;
}
}
}
}
}
}
}
}
else
else
{
{
if ($width > $height)
if ($width > $height)
{
{
$thumb_width = $thb_width;
$thumb_width = $thb_width;
$thumb_height = $thb_width * $height / $width;
$thumb_height = $thb_width * $height / $width;
}
}
elseif ($width < $height)
elseif ($width < $height)
{
{
$thumb_width = $thb_height * $width / $height;
$thumb_width = $thb_height * $width / $height;
$thumb_height = $thb_height;
$thumb_height = $thb_height;
}
}
else
else
{
{
$thumb_width = $thb_width;
$thumb_width = $thb_width;
$thumb_height = $thb_height;
$thumb_height = $thb_height;
}
}
}
}
$thumbnail = array();
$thumbnail = array();
$thumbnail['width'] = round($thumb_width);
$thumbnail['width'] = round($thumb_width);
$thumbnail['height'] = round($thumb_height);
$thumbnail['height'] = round($thumb_height);
return $thumbnail;
return $thumbnail;
}
}
// Path overrides
// Path overrides
public static function getTemplatePath($pluginName, $file, $tmpl)
public static function getTemplatePath($pluginName, $file, $tmpl)
{
{
$mainframe = JFactory::getApplication();
$mainframe = JFactory::getApplication();
$p = new JObject;
$p = new JObject;
$pluginGroup = 'content';
$pluginGroup = 'content';
$jTemplate = $mainframe->getTemplate();
$jTemplate = $mainframe->getTemplate();
if($mainframe->isAdmin()){
if($mainframe->isAdmin()){
$db = JFactory::getDBO();
$db = JFactory::getDBO();
if (version_compare(JVERSION, '1.6', 'ge'))
if (version_compare(JVERSION, '1.6', 'ge'))
{
{
$query = "SELECT template FROM #__template_styles WHERE client_id = 0 AND home = 1";
$query = "SELECT template FROM #__template_styles WHERE client_id = 0 AND home = 1";
}
}
else
else
{
{
$query = "SELECT template FROM #__templates_menu WHERE client_id = 0 AND menuid = 0";
$query = "SELECT template FROM #__templates_menu WHERE client_id = 0 AND menuid = 0";
}
}
$db->setQuery($query);
$db->setQuery($query);
$jTemplate = $db->loadResult();
$jTemplate = $db->loadResult();
}
}
if (file_exists(JPATH_SITE.DS.'templates'.DS.$jTemplate.DS.'html'.DS.$pluginName.DS.$tmpl.DS.str_replace('/', DS, $file)))
if (file_exists(JPATH_SITE.DS.'templates'.DS.$jTemplate.DS.'html'.DS.$pluginName.DS.$tmpl.DS.str_replace('/', DS, $file)))
{
{
$p->file = JPATH_SITE.DS.'templates'.DS.$jTemplate.DS.'html'.DS.$pluginName.DS.$tmpl.DS.$file;
$p->file = JPATH_SITE.DS.'templates'.DS.$jTemplate.DS.'html'.DS.$pluginName.DS.$tmpl.DS.$file;
$p->http = JURI::root(true)."/templates/".$jTemplate."/html/{$pluginName}/{$tmpl}/{$file}";
$p->http = JURI::root(true)."/templates/".$jTemplate."/html/{$pluginName}/{$tmpl}/{$file}";
}
}
else
else
{
{
if (version_compare(JVERSION, '1.6.0', 'ge'))
if (version_compare(JVERSION, '1.6.0', 'ge'))
{
{
// Joomla! 1.6+
// Joomla! 1.6+
$p->file = JPATH_SITE.DS.'plugins'.DS.$pluginGroup.DS.$pluginName.DS.$pluginName.DS.'tmpl'.DS.$tmpl.DS.$file;
$p->file = JPATH_SITE.DS.'plugins'.DS.$pluginGroup.DS.$pluginName.DS.$pluginName.DS.'tmpl'.DS.$tmpl.DS.$file;
$p->http = JURI::root(true)."/plugins/{$pluginGroup}/{$pluginName}/{$pluginName}/tmpl/{$tmpl}/{$file}";
$p->http = JURI::root(true)."/plugins/{$pluginGroup}/{$pluginName}/{$pluginName}/tmpl/{$tmpl}/{$file}";
}
}
else
else
{
{
// Joomla! 1.5
// Joomla! 1.5
$p->file = JPATH_SITE.DS.'plugins'.DS.$pluginGroup.DS.$pluginName.DS.'tmpl'.DS.$tmpl.DS.$file;
$p->file = JPATH_SITE.DS.'plugins'.DS.$pluginGroup.DS.$pluginName.DS.'tmpl'.DS.$tmpl.DS.$file;
$p->http = JURI::root(true)."/plugins/{$pluginGroup}/{$pluginName}/tmpl/{$tmpl}/{$file}";
$p->http = JURI::root(true)."/plugins/{$pluginGroup}/{$pluginName}/tmpl/{$tmpl}/{$file}";
}
}
}
}
return $p;
return $p;
}
}
// Replace white space
// Replace white space
public static function replaceWhiteSpace($text_to_parse)
public static function replaceWhiteSpace($text_to_parse)
{
{
$source_html = array(" ");
$source_html = array(" ");
$replacement_html = array("%20");
$replacement_html = array("%20");
return str_replace($source_html, $replacement_html, $text_to_parse);
return str_replace($source_html, $replacement_html, $text_to_parse);
}
}
// Cleanup thumbnail filenames
// Cleanup thumbnail filenames
public static function cleanThumbName($text_to_parse)
public static function cleanThumbName($text_to_parse)
{
{
$source_html = array(' ', ',');
$source_html = array(' ', ',');
$replacement_html = array('_', '_');
$replacement_html = array('_', '_');
return str_replace($source_html, $replacement_html, $text_to_parse);
return str_replace($source_html, $replacement_html, $text_to_parse);
}
}
} // End class
} // End class