Файловый менеджер - Редактировать - /home/digilove/public_html/41423/core.tar
Назад
helper.php 0000644 00000012415 15232574456 0006554 0 ustar 00 <?php /** * @package Related News * @version 2.5 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL * @copyright (c) 2012 YouTech Company. All Rights Reserved. * @author YouTech Company http://www.smartaddons.com * */ defined('_JEXEC') or die; $com_path = JPATH_SITE.'/components/com_content/'; require_once $com_path.'router.php'; require_once $com_path.'helpers/route.php'; JModelLegacy::addIncludePath($com_path . '/models', 'ContentModel'); include_once dirname(__FILE__).'/helper_base.php'; class RelatedNews extends RelatedBaseHelper{ public static function getList(&$params) { // Get an instance of the generic articles model $model = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true)); // Set application parameters in model $app = JFactory::getApplication(); $appParams = $app->getParams(); $model->setState('params', $appParams); // Set the filters based on the module params $model->setState('list.start', 0); $model->setState('list.limit', (int) $params->get('count', 5)); $model->setState('filter.published', 1); // Access filter $access = !JComponentHelper::getParams('com_content')->get('show_noauth'); $authorised = JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id')); $model->setState('filter.access', $access); // Category filter $model->setState('filter.category_id', $params->get('catid', array())); // Filter by language $model->setState('filter.language', $app->getLanguageFilter()); // Set ordering $ordering = $params->get('ordering', 'a.publish_up'); $model->setState('list.ordering', $ordering); if (trim($ordering) == 'rand()') { $model->setState('list.ordering', JFactory::getDbo()->getQuery(true)->Rand()); } else { $direction = $params->get('direction', 1) ? 'DESC' : 'ASC'; $model->setState('list.direction', $direction); $model->setState('list.ordering', $ordering); } // Retrieve Content $items = $model->getItems(); foreach ($items as &$item) { $item->readmore = strlen(trim($item->fulltext)); $item->slug = $item->id . ':' . $item->alias; $item->catslug = $item->catid . ':' . $item->category_alias; if ($access || in_array($item->access, $authorised)) { // We know that user has the privilege to view the article $item->link = JRoute::_(ContentHelperRoute::getArticleRoute($item->slug, $item->catid, $item->language)); $item->linkText = JText::_('MOD_ARTICLES_NEWS_READMORE'); } else { $item->link = new JUri(JRoute::_('index.php?option=com_users&view=login', false)); $item->link->setVar('return', base64_encode(ContentHelperRoute::getArticleRoute($item->slug, $item->catid, $item->language))); $item->linkText = JText::_('MOD_ARTICLES_NEWS_READMORE_REGISTER'); } $item->introtext = JHtml::_('content.prepare', $item->introtext, '', 'mod_articles_news.content'); if (!$params->get('image')) { $item->introtext = preg_replace('/<img[^>]*>/', '', $item->introtext); } $results = $app->triggerEvent('onContentAfterDisplay', array('com_content.article', &$item, &$params, 1)); $item->afterDisplayTitle = trim(implode("\n", $results)); $results = $app->triggerEvent('onContentBeforeDisplay', array('com_content.article', &$item, &$params, 1)); $item->beforeDisplayContent = trim(implode("\n", $results)); } return $items; } public static function groupBy($list, $fieldName, $article_grouping_direction, $fieldNameToKeep = null) { $grouped = array(); if (!is_array($list)) { if ($list == '') { return $grouped; } $list = array($list); } foreach($list as $key => $item) { if (!isset($grouped[$item->$fieldName])) { $grouped[$item->$fieldName] = array(); } if (is_null($fieldNameToKeep)) { $grouped[$item->$fieldName][$key] = $item; } else { $grouped[$item->$fieldName][$key] = $item->$fieldNameToKeep; } unset($list[$key]); } $article_grouping_direction($grouped); return $grouped; } public static function groupByDate($list, $type = 'year', $article_grouping_direction, $month_year_format = 'F Y') { $grouped = array(); if (!is_array($list)) { if ($list == '') { return $grouped; } $list = array($list); } foreach($list as $key => $item) { switch($type) { case 'month_year': $month_year = JString::substr($item->created, 0, 7); if (!isset($grouped[$month_year])) { $grouped[$month_year] = array(); } $grouped[$month_year][$key] = $item; break; case 'year': default: $year = JString::substr($item->created, 0, 4); if (!isset($grouped[$year])) { $grouped[$year] = array(); } $grouped[$year][$key] = $item; break; } unset($list[$key]); } $article_grouping_direction($grouped); if ($type === 'month_year') { foreach($grouped as $group => $items) { $date = new JDate($group); $formatted_group = $date->format($month_year_format); $grouped[$formatted_group] = $items; unset($grouped[$group]); } } return $grouped; } } helper_base.php 0000644 00000036066 15232574456 0007556 0 ustar 00 <?php /** * @package Related News * @version 2.5 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL * @copyright (c) 2012 YouTech Company. All Rights Reserved. * @author YouTech Company http://www.smartaddons.com * */ defined('_JEXEC') or die; require_once JPATH_SITE.'/libraries/joomla/filesystem/folder.php'; JLoader::register('ImageHelper', dirname(__FILE__).'/helper_image.php'); if (!class_exists('RelatedBaseHelper')){ abstract class RelatedBaseHelper{ /** * * @param string $introtext * @return string */ public function _cleanText($text){ $text = str_replace('<p>', ' ', $text); $text = str_replace('</p>', ' ', $text); $text = strip_tags($text, '<a><em><strong>'); $text = trim($text); return $text; } /** * Parse and build target attribute for links. * @param string $value (_self, _blank, _windowopen, _modal) */ public static function parseTarget($value='_self'){ $target = ''; switch($value){ default: case '0': case '_self': break; case '1': case '_blank': $target = "target=\"_blank\""; break; case '2': case '_windowopen': $target = "onclick=\"window.open(this.href,'targetWindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,false');return false;\""; break; case '3': case '_modal': $target = ""; break; } return $target; } /** * Truncate string by $length * @param string $string * @param int $length * @param string $etc * @return string */ public static function truncate($string, $length, $etc='...'){ return defined('MB_OVERLOAD_STRING') ? self::_mb_truncate($string, $length, $etc) : self::_truncate($string, $length, $etc); } /** * Truncate string if it's size over $length * @param string $string * @param int $length * @param string $etc * @return string */ private static function _truncate($string, $length, $etc='...'){ if ($length>0 && $length<strlen($string)){ $buffer = ''; $buffer_length = 0; $parts = preg_split('/(<[^>]*>)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE); $self_closing_tag = split(',', 'area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed'); $open = array(); foreach($parts as $i => $s){ if( false===strpos($s, '<') ){ $s_length = strlen($s); if ($buffer_length + $s_length < $length){ $buffer .= $s; $buffer_length += $s_length; } else if ($buffer_length + $s_length == $length) { if ( !empty($etc) ){ $buffer .= ($s[$s_length - 1]==' ') ? $etc : " $etc"; } break; } else { $words = preg_split('/([^\s]*)/', $s, - 1, PREG_SPLIT_DELIM_CAPTURE); $space_end = false; foreach ($words as $w){ if ($w_length = strlen($w)){ if ($buffer_length + $w_length < $length){ $buffer .= $w; $buffer_length += $w_length; $space_end = (trim($w) == ''); } else { if ( !empty($etc) ){ $more = $space_end ? $etc : " $etc"; $buffer .= $more; $buffer_length += strlen($more); } break; } } } break; } } else { preg_match('/^<([\/]?\s?)([a-zA-Z0-9]+)\s?[^>]*>$/', $s, $m); //$tagclose = isset($m[1]) && trim($m[1])=='/'; if (empty($m[1]) && isset($m[2]) && !in_array($m[2], $self_closing_tag)){ array_push($open, $m[2]); } else if (trim($m[1])=='/') { $tag = array_pop($open); if ($tag != $m[2]){ // uncomment to to check invalid html string. // die('invalid close tag: '. $s); } } $buffer .= $s; } } // close tag openned. while(count($open)>0){ $tag = array_pop($open); $buffer .= "</$tag>"; } return $buffer; } return $string; } /** * Truncate mutibyte string if it's size over $length * @param string $string * @param int $length * @param string $etc * @return string */ private static function _mb_truncate($string, $length, $etc='...'){ $encoding = mb_detect_encoding($string); if ($length>0 && $length<mb_strlen($string, $encoding)){ $buffer = ''; $buffer_length = 0; $parts = preg_split('/(<[^>]*>)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE); $self_closing_tag = explode(',', 'area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed'); $open = array(); foreach($parts as $i => $s){ if (false === mb_strpos($s, '<')){ $s_length = mb_strlen($s, $encoding); if ($buffer_length + $s_length < $length){ $buffer .= $s; $buffer_length += $s_length; } else if ($buffer_length + $s_length == $length) { if ( !empty($etc) ){ $buffer .= ($s[$s_length - 1]==' ') ? $etc : " $etc"; } break; } else { $words = preg_split('/([^\s]*)/', $s, -1, PREG_SPLIT_DELIM_CAPTURE); $space_end = false; foreach ($words as $w){ if ($w_length = mb_strlen($w, $encoding)){ if ($buffer_length + $w_length < $length){ $buffer .= $w; $buffer_length += $w_length; $space_end = (trim($w) == ''); } else { if ( !empty($etc) ){ $more = $space_end ? $etc : " $etc"; $buffer .= $more; $buffer_length += mb_strlen($more); } break; } } } break; } } else { preg_match('/^<([\/]?\s?)([a-zA-Z0-9]+)\s?[^>]*>$/', $s, $m); //$tagclose = isset($m[1]) && trim($m[1])=='/'; if (empty($m[1]) && isset($m[2]) && !in_array($m[2], $self_closing_tag)){ array_push($open, $m[2]); } else if (trim($m[1])=='/') { $tag = array_pop($open); if ($tag != $m[2]){ // uncomment to to check invalid html string. // die('invalid close tag: '. $s); } } $buffer .= $s; } } // close tag openned. while(count($open)>0){ $tag = array_pop($open); $buffer .= "</$tag>"; } return $buffer; } return $string; } public static $image_article_cache = array(); public static function getArticleImage($item, $_params, $ctype='article'){ $images = self::getArticleImages($item, $_params, $ctype); return is_array($images) && count($images) ? $images[0] : null; } public static function getArticleImages($item, $_params, $ctype='article'){ $hash = md5( serialize(array($_params, $ctype)) ); if ( !isset(self::$image_article_cache[$hash][$item->id]) ){ $defaults = array( 'external' => 1, 'image_intro' => 1, 'inline_introtext' => 1, 'image_fulltext' => 1, 'inline_fulltext' => 1 ); $images_path = array(); $priority = preg_split('/[\s|,|;]/', $_params->get('imgcfg_order', 'external, imagE_intro,inline_introtext,image_fulltext,inline_fulltext'), -1, PREG_SPLIT_NO_EMPTY); if ( count($priority) > 0 ){ $priority = array_map('strtolower', $priority); $mark = array(); for($i=0; $i<count($priority); $i++){ $type = $priority[$i]; if ( array_key_exists($type, $defaults) ) unset($defaults[ $type ]); if ( $_params->get('imgcfg_from_'.$type, 1) ) $mark[ $type ] = 1; } } foreach($defaults as $type => $val){ if ( $_params->get('imgcfg_from_'.$type, 1) ) $mark[ $type ] = 1; } if ( count($mark) > 0 ){ // prepare data. $images_data = null; if (array_key_exists('image_intro', $mark) || array_key_exists('image_fulltext', $mark)){ $images_data = json_decode($item->images, true); } foreach($mark as $type => $true){ switch ($type){ case 'image_intro': case 'image_fulltext': if ( isset($images_data) && isset($images_data[$type]) && !empty($images_data[$type])){ $image = array( 'src' => $images_data[$type] ); if (array_key_exists($type.'_alt', $images_data)){ $image['alt'] = $images_data[$type.'_alt']; } if (array_key_exists($type.'_caption', $images_data)){ /* $image['class'] = 'caption'; */ $image['title'] = $images_data[$type.'_caption']; } array_push($images_path, $image); } break; case 'inline_introtext': $text = $item->introtext; case 'inline_fulltext': if ($type == 'inline_fulltext'){ $text = $item->fulltext; } $inline_images = self::getInlineImages($text); for ($i=0; $i<count($inline_images); $i++){ array_push($images_path, $inline_images[$i]); } break; case 'external': $exf = $_params->get('imgcfg_external_url', '/images'); preg_match_all('/{([a-zA-Z0-9_]+)}/', $exf, $m); if ( count($m)==2 && count($m[0])>0 ){ $compat = 1; foreach ($m[1] as $property){ !property_exists($item, $property) && ($compat=0); } if ($compat){ $replace = array(); foreach ($m[1] as $property){ $replace[] = is_null($item->$property) ? '' : $item->$property; } $exf = str_replace($m[0], $replace, $exf); } } $files = self::getExternalImages($exf); for ($i=0; $i<count($files); $i++){ array_push($images_path, array('src'=>$files[$i])); } break; default: break; } } } if ( count($images_path) == 0 && $_params->get('imgcfg_placeholder', 1)==1){ $images_path[] = array('src'=> $_params->get('imgcfg_placeholder_path', null), 'class'=>'placeholder'); } self::$image_article_cache[$hash][$item->id] = $images_path; } return self::$image_article_cache[$hash][$item->id]; } public static $image_category_cache = array(); public static function getCategoryImage($item, $_params, $ctype='category'){ $images = &self::getCategoryImages($item, $_params, $ctype); return is_array($images) && count($images) ? $images[0] : null; } public static function getCategoryImages($item, $_params, $ctype='category'){ $hash = md5( serialize(array($_params, $ctype)) ); if ( !isset(self::$image_category_cache[$hash][$item->id]) ){ $defaults = array( 'external' => 1, 'params' => 1, 'description' => 1 ); $images_path = array(); $priority = preg_split('/[\s|,|;]/', $_params->get('imgcfg_order', 'external, params, description'), -1, PREG_SPLIT_NO_EMPTY); if ( count($priority) > 0 ){ $priority = array_map('strtolower', $priority); $mark = array(); for($i=0; $i<count($priority); $i++){ $type = $priority[$i]; if ( array_key_exists($type, $defaults) ) unset($defaults[ $type ]); if ( $_params->get('imgcfg_from_'.$type, 1) ) $mark[ $type ] = 1; } } foreach($defaults as $type => $val){ if ( $_params->get('imgcfg_from_'.$type, 1) ) $mark[ $type ] = 1; } if ( count($mark) > 0 ){ $cparams = null; if (array_key_exists('params', $mark)){ $cparams = new JRegistry; $cparams->loadString($item->params); } foreach($mark as $type => $true){ switch ($type){ case 'params': if ( $cparams instanceof JRegistry && $cparams->get('image') ){ $image = array( 'src' => $cparams->get('image') ); array_push($images_path, $image); } break; case 'description': $inline_images = self::getInlineImages($item->description); for ($i=0; $i<count($inline_images); $i++){ array_push($images_path, $inline_images[$i]); } break; case 'external': $exf = $_params->get('imgcfg_external_url', '/images'); preg_match_all('/{([a-zA-Z0-9_]+)}/', $exf, $m); if ( count($m)==2 && count($m[0])>0 ){ $compat = 1; foreach ($m[1] as $property){ !property_exists($item, $property) && ($compat=0); } if ($compat){ $replace = array(); foreach ($m[1] as $property){ $replace[] = is_null($item->$property) ? '' : $item->$property; } $exf = str_replace($m[0], $replace, $exf); } } $files = self::getExternalImages($exf); for ($i=0; $i<count($files); $i++){ array_push($images_path, array('src'=>$files[$i])); } break; default: break; } } } if ( count($images_path) == 0 && $_params->get('imgcfg_placeholder', 1)==1){ $images_path[] = array('src'=> $_params->get('imgcfg_placeholder_path', null), 'class'=>'placeholder'); } self::$image_category_cache[$hash][$item->id] = $images_path; } return self::$image_category_cache[$hash][$item->id]; } /** * * @param string $text * @return string: */ public static function getInlineImages($text){ $images = array(); $searchTags = array( 'img' => '/<img[^>]+>/i', 'input' => '/<input[^>]+type\s?=\s?"image"[^>]+>/i' ); foreach ($searchTags as $tag => $regex){ preg_match_all($regex, $text, $m); if ( is_array($m) && isset($m[0]) && count($m[0])){ foreach ($m[0] as $htmltag){ $tmp = JUtility::parseAttributes($htmltag); if ( isset($tmp['src']) ){ if ($tag == 'input'){ array_push( $images, array('src' => $tmp['src']) ); } else { array_push( $images, $tmp ); } } } } } return $images; } /** * * @param string $path * @return multitype:multitype:unknown |Ambigous <multitype:, boolean, multitype:unknown multitype:unknown > */ public static function getExternalImages($path){ $files = array(); $ps = JString::parse_url($path); if ( array_key_exists('path', $ps) && !empty($ps['path']) ){ $isHttp = isset($ps['scheme']) && in_array($ps['scheme'], array('http', 'https')); if (!$isHttp || JURI::isInternal($path)){ // image on server $path = $ps['path']; } else { $files[] = array( 'src' => $path ); return $files; } } if (is_file($path)){ $files[] = $path; } else if (is_dir($path)){ $files = JFolder::files($path, '.jpg|.png|.gif', false, true); } else { $ext = substr($path, -4); $search = substr($path, 0, -4); $lext = strtolower($ext); if ( is_dir($search) && in_array($lext, array('.jpg', '.png', '.gif')) ){ $files = JFolder::files($search, $ext, false, true); } } return $files; } public static function imageTag($image, $options=array()){ return ImageHelper::init($image, $options)->tag(); } public static function getImageHelper($image, $options=array()){ return ImageHelper::init($image, $options); } } } helper_image.php 0000644 00000046706 15232574456 0007730 0 ustar 00 <?php /** * @package Related News * @version 2.5 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL * @copyright (c) 2012 YouTech Company. All Rights Reserved. * @author YouTech Company http://www.smartaddons.com * */ defined('_JEXEC') or die; class ImageHelper{ protected static $instances = array(); protected static $defaults = array( 'type' => null, 'width' => null, 'height' => null, 'quality' => 90, 'function' => null, 'function_mode' => null, 'transparency' => true, 'background' => array('r'=>255, 'g'=>255, 'b'=>255), 'cache' => null, 'cache_url' => null ); public static $supports = array( IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_JPEG ); public static $functions = array( 'resize', 'crop', 'rotate', 'flip_horizontal', 'flip_vertical' ); public static $modes = array( 'center', 'fill', 'fit', 'stretch' ); protected $image = null; protected $in = null; protected $out = null; protected $errors = array(); protected $options = null; protected $_process = 1; protected $debug = false; /** * * @param mixed $image * @param array $options * @return ImageHelper */ public static function init($image, $options = array()){ $cacheid = count(self::$instances); if (!isset(self::$instances[$cacheid])){ self::$instances[$cacheid] = new ImageHelper($image, $options); } return self::$instances[$cacheid]; } /** * ImageHelper constructor * @param unknown_type $image * @param unknown_type $options */ public function __construct($image = null, $options = array()){ !is_null($image) && $this->image = $image; // defaults foreach (ImageHelper::$defaults as $name => $value){ $this->options[$name] = array_key_exists($name, $options) ? $options[$name] : $value; } $this->in = array( 'type'=>null, 'src' =>null ); $this->out = array(); $this->debug && JFactory::getApplication()->enqueueMessage('ImageHelper::__construct'); } public static function setDefault($_params = null){ self::$defaults = array( 'type' => null, 'width' => null, 'height' => null, 'quality' => 90, 'function' => null, 'function_mode' => null, 'transparency' => true, 'background' => array('r'=>255, 'g'=>255, 'b'=>255), 'cache' => null, 'cache_url' => null ); if ($_params instanceof JRegistry){ $opts = array(); // output type ? $imgcfg_type = $_params->get('imgcfg_type', 0); if (in_array($imgcfg_type, array(IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF))){ $opts['type'] = $imgcfg_type; } // function ? $imgcfg_fn = $_params->get('imgcfg_function', 'none_resize'); $imgcfg_fn = explode('_', $imgcfg_fn); if ( in_array($imgcfg_fn[0], self::$functions) ){ $opts['function'] = $imgcfg_fn[0]; if ($imgcfg_fn[0] == 'resize'){ if ( isset($imgcfg_fn[1]) && in_array($imgcfg_fn[1], self::$modes) ){ $opts['function_mode'] = $imgcfg_fn[1]; } else { $opts['function_mode'] = self::$modes[2]; } } } // background $imgcfg_background = $_params->get('imgcfg_background', null); if (!is_null($imgcfg_background)){ $opts['background'] = $imgcfg_background; } // quality $imgcfg_quality = $_params->get('imgcfg_quality', null); if ( $imgcfg_quality != null && intval($imgcfg_quality) > 0 && intval($imgcfg_quality) < 100 ){ $opts['quality'] = intval($imgcfg_quality); } // transparency $imgcfg_transparency = $_params->get('imgcfg_transparency', 1); if ( $imgcfg_transparency != null){ $opts['transparency'] = $imgcfg_transparency ? true : false; } // dimension ? $imgcfg_w = $_params->get('imgcfg_width', 0); $imgcfg_h = $_params->get('imgcfg_height', 0); if ( intval($imgcfg_w) > 0 ){ $opts['width'] = $imgcfg_w; } if ( intval($imgcfg_h) > 0 ){ $opts['height'] = $imgcfg_h; } // cache ? if ( defined('JPATH_CACHE') ){ $cache_path_default = JPATH_CACHE.'/resized'; $cache_path_url_default = 'cache/resized/'; } else { $cache_path_default = null; $cache_path_url_default = null; } $cache_path = $_params->get('imgcfg_cache', $cache_path_default); $cache_url = $_params->get('imgcfg_cache_url', $cache_path_url_default); if ($cache_path) $opts['cache'] = $cache_path; if ($cache_url) $opts['cache_url'] = $cache_url; } self::$defaults = array_merge(self::$defaults, $opts); } public static function testDefault(){ echo 'ImageHelper::$defaults'.'<br>'; var_dump(self::$defaults); } /** * Get image type for input url or path of file. * @return mixed */ protected function getInputType(){ if ( !isset($this->in['type']) ){ $this->debug && JFactory::getApplication()->enqueueMessage('ImageHelper::getInputType'); if (is_string($this->image)){ $path = $this->image; } else { $path = @$this->image['src']; } if (is_string($path)){ $ps = JString::parse_url($path); if ( array_key_exists('path', $ps) && !empty($ps['path']) ){ $isHttp = isset($ps['scheme']) && in_array($ps['scheme'], array('http', 'https')); if (!$isHttp || JURI::isInternal($path)){ $path = $ps['path']; } else { // is extenal url. $this->in['type'] = 'url'; $this->in['src'] = $path; return $this->in['type']; } } } else if (!$path){ $this->errors[] = 'Image path must be string or stored in array_argument[src]!'; } //var_dump('Path: ', $path); if ( !file_exists($path) ){ $this->errors[] = 'Image path is not exists!'; } else { //var_dump('EXISTS'); $this->in['src'] = realpath($path); $infor = array(); if ( function_exists('exif_imagetype') ){ $intype = exif_imagetype($path); } else { $infor = getimagesize($path); $intype= @$infor[2]; } switch ($intype){ case IMAGETYPE_PNG: $this->in['type'] = IMAGETYPE_PNG; break; case IMAGETYPE_GIF: $this->in['type'] = IMAGETYPE_GIF; break; case IMAGETYPE_JPEG: $this->in['type'] = IMAGETYPE_JPEG; break; default: } } } return $this->in['type']; } protected function getInputResource(){ $this->debug && JFactory::getApplication()->enqueueMessage('ImageHelper::getInputResource'); //var_dump('getInputResource: '.$this->in['src']); if ( !isset($this->in['image']) ){ $intype = $this->getInputType(); switch ($intype){ case IMAGETYPE_PNG: $this->in['image'] = imagecreatefrompng($this->in['src']); break; case IMAGETYPE_GIF: $this->in['image'] = imagecreatefromgif($this->in['src']); break; case IMAGETYPE_JPEG: $this->in['image'] = imagecreatefromjpeg($this->in['src']); break; default: $this->in['image'] = null; return false; } } return is_resource($this->in['image']) ? $this->in['image'] : null; } protected function validOptions(){ $this->debug && JFactory::getApplication()->enqueueMessage('ImageHelper::validOptions'); // TODO: validate input $type = $this->getInputType(); $input_type_ok = ($type=='url') || in_array($type, ImageHelper::$supports); if ( !$input_type_ok ){ $this->errors[] = 'Input type is not supported!'; $this->_process = 0; } else if ($type=='url'){ $this->_process = 0; } // required options $output_type_ok = !is_null($this->options['type']) && in_array($this->options['type'], ImageHelper::$supports); if ( !$output_type_ok ){ if ( $input_type_ok ){ $this->options['type'] = $type; } } if ( !($this->options['width']>0 || $this->options['height']>0) ){ $this->errors[] = 'Output width or height must be greater than zero (>0)!'; $this->_process = 0; } if ( in_array($this->options['function'], ImageHelper::$functions) ){ if ($this->options['function'] == 'resize' && !in_array($this->options['function_mode'], ImageHelper::$modes)){ $this->errors[] = 'Resize mode is not valid! should be in ['.implode(', ', ImageHelper::$modes).']'; $this->_process = 0; } } else { $this->errors[] = 'Output function is not valid! should be in ['.implode(', ', ImageHelper::$functions).']'; $this->_process = 0; } // auto parse options if ( !isset($this->options['quality']) || !is_numeric($this->options['quality']) || $this->options['quality']==0 ){ $this->options['quality'] = 90; } else { if ($this->options['quality'] < 10){ $this->options['quality'] = 10; } if ($this->options['quality'] > 100){ $this->options['quality'] = 100; } } $c = &$this->options['background']; if ( is_string($this->options['background']) ){ if ( preg_match('/^#?([a-fA-F0-9]{3}|[a-fA-F0-9]{6})$/i', $this->options['background'], $m) ){ $hex = isset($m[1]) ? $m[1] : null; if (strlen($hex)==3){ $hex = $hex[0].$hex[0].$hex[1].$hex[1].$hex[2].$hex[2]; } if ($hex){ $dec = hexdec($hex); $this->options['background'] = array( 'r' => 0xFF & ($dec >> 0x10), 'g' => 0xFF & ($dec >> 0x8), 'b' => 0xFF & $dec ); } else { $this->options['background'] = null; } } else { $this->options['background'] = null; } } if ( !is_array($this->options['background']) ){ $this->options['background'] = null; } else { $valid = 1; $bg = $this->options['background']; foreach (array('r', 'g', 'b') as $e){ if ( !array_key_exists($e, $bg) || ($bg[$e]<0) || ($bg[$e]>255) ){ $valid = 0; break; } } if (!$valid){ $this->options['background'] = null; } } if ( !is_null($this->options['cache']) ){ if ( !@is_dir($this->options['cache']) && !@mkdir($this->options['cache']) ){ $this->options['cache'] = __DIR__.'/resized'; } } else { $this->options['cache'] = __DIR__.'/resized'; } if ( !file_exists($this->options['cache']) && !@mkdir($this->options['cache'], 0755, true) ){ $this->errors[] = 'Cache is not created!'; $this->_process = 0; } else { $this->options['cache'] = realpath($this->options['cache']); } return count($this->errors) == 0; } protected function getOutputFile(){ if ( !array_key_exists('src', $this->out) || is_null($this->out['src']) ){ $this->debug && JFactory::getApplication()->enqueueMessage('ImageHelper::getOutputFile'); $hash = md5( serialize( array($this->options, $this->in['src'], $this->in['type']) ) ); if (defined('DEBUG') && 0){ $hash = basename($this->in['src'], '.jpg'); $w = isset($this->options['width']) ? $this->options['width'] : 0; $h = isset($this->options['height']) ? $this->options['height'] : 0; $hash .= '-'.$w.'x'.$h; $hash .= '-'.$this->options['function']; $hash .= '-'.$this->options['function_mode']; $hash .= '-'.$this->options['quality']; } switch ($this->options['type']){ case IMAGETYPE_PNG: $ext = '.png'; break; case IMAGETYPE_GIF: $ext = '.gif'; break; case IMAGETYPE_JPEG: $ext = '.jpg'; break; default: $ext = image_type_to_extension($this->options['type'], true); } $this->out['src'] = $this->options['cache'].'/'.$hash.$ext; $this->debug && JFactory::getApplication()->enqueueMessage('ImageHelper::getOutputFile::::'.$this->out['src']); } return $this->out['src']; } protected function getOutputUrl(){ if (!isset($this->out['url'])){ $this->debug && JFactory::getApplication()->enqueueMessage('ImageHelper::getOutputUrl'); if ( $this->validOptions() && in_array($this->options['type'], ImageHelper::$supports) ){ $this->out['url'] = $this->options['cache_url'].basename( $this->getOutputFile() ); } else { $this->out['url'] = is_string($this->image) ? $this->image : @$this->image['src']; $this->debug && JFactory::getApplication()->enqueueMessage('Not validOptions::'.$this->out['url']); } } return $this->out['url']; } /** * @return mixed */ public function output(){ if ( $this->validOptions() && $this->getInputResource()){ $this->debug && JFactory::getApplication()->enqueueMessage('ImageHelper::output'); $this->process(); if (is_resource($this->out['image'])){ $oImage = &$this->out['image']; switch( $this->options['type'] ){ case IMAGETYPE_JPEG: imagejpeg($oImage, $this->getOutputFile(), $this->options['quality']); break; case IMAGETYPE_GIF: imagegif($oImage, $this->getOutputFile()); break; case IMAGETYPE_PNG: imagepng($oImage, $this->getOutputFile(), 10 - round($this->options['quality']/10.0)); break; } } } } /** *@return string */ public function tag(){ $this->debug && JFactory::getApplication()->enqueueMessage('ImageHelper::tag'); if (!$this->getOutputUrl()){ return ''; } $tag = array(); if (is_array($this->image)){ foreach ($this->image as $att => $val){ if ($att == 'src'){ $tag[] = 'src="'.$this->getOutputUrl().'"'; } else { $tag[] = $att.'="'.$val.'"'; } } if (!array_key_exists('alt', $this->image)){ $tag[] = 'alt="'.$this->getOutputUrl().'"'; } } else { $tag[] = 'src="'.$this->getOutputUrl().'"'; $tag[] = 'alt="'.$this->getOutputUrl().'"'; } return count($tag) ? '<img '.implode(' ', $tag).'/>' : ''; } /** * @return string */ public function path(){ $this->debug && JFactory::getApplication()->enqueueMessage('ImageHelper::path'); return $this->__toString(); } public function src(){ $this->debug && JFactory::getApplication()->enqueueMessage('ImageHelper::src'); return $this->getOutputUrl(); } /** * @return string */ public function error(){ $this->debug && JFactory::getApplication()->enqueueMessage('ImageHelper::error'); return $this->errors; } /** * Resize input image by size * @param int $w * @param int $h * @return ImageHelper */ public function resize($w = 0, $h = 0){ $this->debug && JFactory::getApplication()->enqueueMessage('ImageHelper::resize'); if ($w > 0) $this->options['width'] = $w; if ($h > 0) $this->options['height'] = $h; return $this; } protected function actionPerformed(){ $this->debug && JFactory::getApplication()->enqueueMessage('ImageHelper::actionPerformed'); $sImage = $this->getInputResource(); if ( !is_resource($sImage) ) return; $sw = imagesx($sImage); $sh = imagesy($sImage); if ( $this->options['function']=='resize' ){ $ow = is_null($this->options['width']) ? 0 : intval($this->options['width']); $oh = is_null($this->options['height']) ? 0 : intval($this->options['height']); if ( $ow==0 && $oh==0 ){ //die('thoi khoi resize'); } else if ($ow == 0){ // resize by height. $this->options['function_mode'] = ImageHelper::$modes[3]; $ow = $this->options['width'] = round($oh * $sw/$sh); } else if ($oh == 0){ $this->options['function_mode'] = ImageHelper::$modes[3]; $oh = $this->options['height'] = (int)round($ow * $sh/$sw); // resize by width } else if ( abs($oh-round($ow*$sh/$sw)) <= 1 ){ // resize by ca hai $this->options['function_mode'] = ImageHelper::$modes[3]; $oh = $this->options['height'] = round($ow*$sh/$sw); } $oImage = $this->getCanvas(); // build agrs $sx = $sy = 0; $dx = $dy = 0; $dw = $sw; $dh = $sh; switch($this->options['function_mode']){ case ImageHelper::$modes[0]: if ($ow > $dw) { $dx = ($ow-$dw)/2; } if ($oh > $dh) { $dy = ($oh-$dh)/2; } if ($sw > $ow){ $sx = ($sw-$ow)/2; } if ($sh > $oh){ $sy = ($sh-$oh)/2; } break; case ImageHelper::$modes[1]: $or = 1.0*$sw/$sh; if ($ow < $oh*$or){ $dw = round($oh*$or); $dh = $oh; $dx = ($ow-$dw)/2; $dy = 0; } else { $dw = $ow; $dh = round($ow/$or); $dx = 0; $dy = ($oh-$dh)/2; } break; case ImageHelper::$modes[2]: $or = 1.0*$sw/$sh; if ($ow > $oh*$or){ $dw = round($oh*$or); $dh = $oh; $dx = ($ow-$dw)/2; $dy = 0; } else { $dw = $ow; $dh = round($ow/$or); $dx = 0; $dy = ($oh-$dh)/2; } break; default: case ImageHelper::$modes[3]: $dw = $ow; $dh = $oh; break; } imagecopyresampled( $oImage, $sImage, $dx, $dy, $sx, $sy, $dw, $dh, $sw, $sh ); $this->out['image'] = $oImage; } return 1; } public function __destruct(){ $this->save2f(); $this->debug && JFactory::getApplication()->enqueueMessage('ImageHelper::__destruct'); } protected function save2f(){ $this->debug && JFactory::getApplication()->enqueueMessage('ImageHelper::save2f'); if ($this->_process && ($of = $this->getOutputFile()) && !file_exists($of)){ $this->actionPerformed(); if ( array_key_exists('image', $this->out) && is_resource($this->out['image'])){ $oImage = &$this->out['image']; switch( $this->options['type'] ){ case IMAGETYPE_JPEG: imagejpeg($oImage, $of, $this->options['quality']); break; case IMAGETYPE_GIF: imagegif($oImage, $of); break; case IMAGETYPE_PNG: imagepng($oImage, $of, 10 - round($this->options['quality']/10.0)); break; } } } } protected function getCanvas(){ if ( !isset($this->out['image']) ){ $this->debug && JFactory::getApplication()->enqueueMessage('ImageHelper::getCanvas'); $oImage = imagecreatetruecolor($this->options['width'], $this->options['height']); if ($this->options['transparency'] && in_array($this->options['type'], array(IMAGETYPE_GIF, IMAGETYPE_PNG))){ if (IMAGETYPE_GIF == $this->in['type']){ $trans_index = imagecolortransparent($this->in['image']); if ($trans_index >= 0) { $trans_color = imagecolorsforindex($this->in['image'], $trans_index); $trans_index = imagecolorallocate($oImage, $trans_color['red'], $trans_color['green'], $trans_color['blue']); imagefill($oImage, 0, 0, $trans_index); imagecolortransparent($oImage, $trans_index); } } else if (IMAGETYPE_PNG == $this->in['type']){ $png_alpha = ((ord(file_get_contents($this->in['src'], false, null, 25, 1)) & 6) & 4) == 4; if ($png_alpha){ imagealphablending($oImage, true); $transparent = imagecolorallocatealpha($oImage, 0, 0, 0, 127); imagefilledrectangle($oImage, 0, 0, $this->options['width'], $this->options['height'], $transparent); imagealphablending($oImage, false); imagesavealpha($oImage, true); } } } else if ( !is_null($this->options['background']) && $this->options['function']==ImageHelper::$functions[0] && in_array($this->options['function_mode'], array(ImageHelper::$modes[0], ImageHelper::$modes[2]))){ $bgc = imagecolorallocate( $oImage, $this->options['background']['r'], $this->options['background']['g'], $this->options['background']['b'] ); imagefilledrectangle($oImage, 0, 0, $this->options['width'], $this->options['height'], $bgc); } $this->out['image'] = $oImage; } return $this->out['image']; } public function __toString(){ $this->debug && JFactory::getApplication()->enqueueMessage('ImageHelper::__toString'); $f = $this->getOutputFile(); return is_null($f) ? '' : $f; } } index.html 0000644 00000000054 15232574456 0006555 0 ustar 00 <html><body bgcolor="#FFFFFF"></body></html>