?
Path : /home/admin/domains/happytokorea.com/public_html_bk/promice/plugins/system/ |
Current File : /home/admin/domains/happytokorea.com/public_html_bk/promice/plugins/system/jvgallery.php |
<?php /** # plg_system_jvgallery - JV Gallery # @version 1.5.x # ------------------------------------------------------------------------ # author Open Source Code Solutions Co # copyright Copyright (C) 2011 joomlavi.com. All Rights Reserved. # @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL or later. # Websites: http://www.joomlavi.com # Technical Support: http://www.joomlavi.com/my-tickets.html -------------------------------------------------------------------------*/ // No direct access to this file defined('_JEXEC') or die('Restricted access'); jimport('joomla.application.menu'); jimport( 'joomla.plugin.plugin' ); jimport('joomla.filesystem.file'); class plgSystemJVGallery extends JPlugin { protected $abort = array(); /** * Constructor * * For php4 compatability we must not use the __constructor as a constructor for plugins * because func_get_args ( void ) returns a copy of all passed arguments NOT references. * This causes problems with cross-referencing necessary for the observer design pattern. * * @access protected * @param object $subject The object to observe * @param array $config An array that holds the plugin configuration * @since 1.0 */ public function plgSystemJVGallery (& $subject, $config){ parent :: __construct($subject, $config); } function onAfterDispatch(){ $app = JFactory::getApplication(); if ($app->isAdmin ()) return; $document =& JFactory::getDocument(); $document->addStyleSheet(JURI::root().'plugins/system/jvgallery/css/template.css'); $document->addStyleSheet(JURI::root().'plugins/system/jvgallery/css/slimbox.css'); $document->addScript(JURI::root() . 'plugins/system/jvgallery/js/jvgallery.js'); $document->addScript(JURI::root() . 'plugins/system/jvgallery/js/slimbox.js'); if(JRequest::getVar('option') == 'com_content' && JRequest::getVar('task') == 'edit'){ $buffer = $document->getBuffer(); $buffer = $buffer['component']['']; $regex = '/{jvgallery([^}]*)}([^}]*){\/jvgallery}/i'; preg_match_all( $regex, $buffer, $matches ); if(count($matches[0])){ foreach($matches[0] as $matche){ $this->abort[] = $matche; } } } return; } public function onAfterRender(){ $app = JFactory::getApplication(); if ($app->isAdmin ()) return; $body = JResponse::getBody (); $tabs = array(); // expression to search for $regex = '/{jvgallery([^}]*)}([^}]*){\/jvgallery}/i'; preg_match_all( $regex, $body, $matche ); if(count($matche[0])){ foreach($matche[0] as $index => $matc){ $replace = preg_quote($matc, '/'); if(!in_array($matc,$this->abort)){ $data = $this->parseAttributes(trim($matche[1][$index])); $data['caption'] = $this->parseAttributes(trim($matche[2][$index])); $body = preg_replace("/$replace/", $this->process($data), $body); } } } JResponse::setBody ( $body); } protected function parseAttributes( $string ) { //Initialize variables $attr = array(); $retarray = array(); $regex = '/([\S]+)[\s]?=[\s]?"([^"]*)"/i'; // Lets grab all the key/value pairs using a regular expression preg_match_all( $regex, $string, $attr ); if (is_array($attr)) { $numPairs = count($attr[1]); for($i = 0; $i < $numPairs; $i++ ) { if(stripos($attr[2][$i],'|')){ $attr[2][$i] = @explode('|',$attr[2][$i]); } $retarray[$attr[1][$i]] = $attr[2][$i]; } } return $retarray; } public function process ( $data ){ $folder = isset($data['folder']) ? $data['folder']:'images/stories'; $filter = '\.png$|\.gif$|\.jpg$|\.bmp$|\.ico$'; $images = array(); $width = isset($data['width']) ? $data['width'] : $this->params->get('width', 150); $height = isset($data['height']) ? $data['height'] : $this->params->get('height',150); $thumbsize = array($width,$height); if(JFolder::exists(JPATH_ROOT.DS.$folder)){ $images = JFolder::files(JPATH_ROOT.DS.$folder, $filter); if(!count($images)) return; if(!$this->createThumbnails($images, $folder, $width, $height)){ return; } $html = $this->render($images, $folder, $thumbsize, $data); return $html; } return; } public function render($images, $folder, $thumbsize, $data){ $cols = isset($data['cols']) ? $data['cols']: $this->params->get('cols', ''); $id = jvgallery.rand(100,300); $theme = isset($data['theme']) ? $data['theme']: $this->params->get('theme', ''); $path = JPATH_ROOT.'/plugins/system/jvgallery/themes/theme'.$theme.'/gallery.php'; ob_start(); require $path; $html = ob_get_contents(); ob_clean(); return $html; } protected function createThumbnails($images, $folder, $width = 150, $height = 150){ $path = JPATH_ROOT.DS.$folder.DS; $resizePath = JPATH_ROOT.DS.$folder.DS."images_{$width}_{$height}"; if(!JFolder::exists($resizePath)){ if(!JFolder::create($resizePath)) return; if(!JFile::write($resizePath.DS.'index.html',"<html><body></body></html>")) return; } if(count($images)){ foreach($images as $image){ if(!JFile::exists($resizePath.DS.$image)){ $fileNameParts = explode('.',$image); $fileExtension = array_pop($fileNameParts); $thumb = $this->buildThumbnail($path.$image,$fileExtension, $width, $height); if(!JFile::write($resizePath.DS.$image,$thumb)) return; } } } return true; } protected function buildThumbnail($filepath, $ext, $img_width = 150, $img_height = 150){ $img_width = $img_width != null ? $img_width: 150; $img_height = $img_height != null ? $img_height: 150; $ext = $ext == 'jpg' ? 'jpeg' : $ext; $function = 'imagecreatefrom'.$ext; // Get the image and create a thumbnail $img = @$function($filepath); if (!$img) { $img = imagecreate(150, 30); /* Create a black image */ $bgc = imagecolorallocate($img, 255, 255, 255); $tc = imagecolorallocate($img, 0, 0, 0); imagefilledrectangle($im, 0, 0, 150, 30, $bgc); /* Output an errmsg */ imagestring($img, 1, 5, 5, "Error loading !", $tc); } $width = imageSX($img); $height = imageSY($img); if (!$width || !$height) { header("HTTP/1.1 500 Internal Server Error"); echo "Invalid width or height"; exit(0); } // Build the thumbnail $target_width = $img_width; $target_height = $img_height; $target_ratio = $target_width / $target_height; $img_ratio = $width / $height; if ($target_ratio > $img_ratio) { $new_height = $target_height; $new_width = $img_ratio * $target_height; } else { $new_height = $target_width / $img_ratio; $new_width = $target_width; } if ($new_height > $target_height) { $new_height = $target_height; } if ($new_width > $target_width) { $new_height = $target_width; } $new_img = ImageCreateTrueColor($img_width, $img_height); $color = imagecolorallocate($new_img, 250,250,250); if (!@imagefilledrectangle($new_img, 0, 0, $target_width-1, $target_height-1, $color)) { // Fill the image black header("HTTP/1.1 500 Internal Server Error"); echo "Could not fill new image"; exit(0); } if (!@imagecopyresampled($new_img, $img, ($target_width-$new_width)/2, ($target_height-$new_height)/2, 0, 0, $new_width, $new_height, $width, $height)) { header("HTTP/1.0 500 Internal Server Error"); echo "Could not resize image"; exit(0); } // Use a output buffering to load the image into a variable ob_start(); imagejpeg($new_img); $imagevariable = ob_get_contents(); ob_end_clean(); return $imagevariable; } }