Tag Cloud

24 Январь 2007

Написал вот класс для генирации облака тегов



/**
 * @author CTAPbIu_MABP <CTAPbIuMABP@gmail.com>
 * @version 1.0
 * @license Free for non-commercial use
 */
class CloudMaker
{
	public $color = array(0,0,0); // cloud color is black (#000000)
	public $size = array(15,30); // min - max font size
	public $grad = 'px'; // css size (em,px,pt)
	public $link = 'http://localhost/'; // url prefix
	
	private $_cloud = array(); // tmp cloud params
	
	protected $_count = 0; // count of cloud elements
	protected $_map = array(); // sort map
	
	/**
	 * Set default params
	 *
	 * @param str $color
	 * @param int|arr $size
	 * @param str $link
	 * @param str $grad
	 */
	public function __construct($color, $size=30, $link='', $grad='') 
	{
		$this->_makeColor($color);
		$this->_makeSize($size);
		$this->link = $link ? $link : $this->link;
		$this->grad = $grad ? $grad : $this->grad;
	}
	
	/**
	 * Public method to add cloud
	 *
	 * @param arr $cloud
	 */
	public function addCloud($cloud) // array
	{
		if (!is_array($cloud) || empty($cloud)) return; // empty value

		$this->_count += count($cloud); // if 2 or more cloud added
	
		if (!is_array(current($cloud)))
		{
			foreach ($cloud as $key => $value)
			{
				$this->_cloud[] = array(
					'weight' => $key,
					'tag' => $value,
				 );
			}
		}else{
			foreach ($cloud as $key => $value)
			{
				$this->_cloud[] = array(
					'weight' => $value[0],
					'tag' => $value[1],
					'link' => $value[2],
				 );
			}
		}
		
		$this->_makeLinksFromTags();
		usort($this->_cloud, array($this, '_sortWeight'));
		
		$this->_makeTrueWeight();
		$this->_makeTrueColor();
	}
	
	/**
	 * Reset color
	 *
	 * @param str $color
	 */
	public function changeColor($color)
	{
		$this->_makeColor($color);
		$this->_makeTrueColor();
	}
	
	/**
	 * Reset size
	 *
	 * @param int|arr $size
	 * @param str $grad
	 */
	public function changeSize($size,$grad='')
	{
		$this->_makeSize($size);
		$this->_makeTrueWeight();
		$this->grad = $grad ? $grad : $this->grad;
	}
	
	/**
	 * Create HTML code
	 *
	 * @return str
	 */
	public function makeCloud()
	{
		
		$this->_map = array_reverse($this->_map);
		usort($this->_cloud, array($this, '_sortMap')); // randomize
		
		// The format of html is not programmer trouble. Anyone can change it as (s)he like
		foreach($this->_cloud as $item)
			$cloud .= "<a href='".$this->link.$item['link']."'><span style='color:#".$item['color'].";font-size:".$item['factor'].$this->grad.";'>".$item['tag']."</span></a>\r\n";

		return $cloud;
	}
	
	/**
	 * Callback function for usort
	 *
	 * @param any $a
	 * @param any $b
	 * @return int
	 */
	protected function _sortWeight($a, $b)
	{
		return $this->_map[] = ($a['weight'] == $b['weight'])? 0 : ($a['weight'] > $b['weight']) ? -1 : 1;
	}
	
	/**
	 * Callback function for usort
	 *
	 * @param any $a
	 * @param any $b
	 * @return int
	 */
	protected function _sortMap($a, $b) // :(
	{
		$tmp = current($this->_map);
		next($this->_map);
		return $tmp;
	}

	/**
	 * Create links for russian lang tag
	 *
	 */
	/*
	private function _makeLinksFromTags()
	{
		for($i=0;$i<=$this->_count-1;$i++)
		{
			if (empty($this->_cloud[$i]['link']))
				if (preg_match('#[a-y?????]#i',$this->_cloud[$i]['tag']))
					$this->_cloud[$i]['link'] = str_replace(
						array('a','a','a','a','a','a','?','?','c','e','e','e','e','i','i','i','i','?','n','o','o','o','o','o','?','o','u','u','u','y','?','y','?','?','?','?',' '),
						array('a','b','v','g','d','e','yo','j','z','i','y','k','l','m','n','o','p','r','s','t','u','f','h','tz','ch','sh','sch','','','e','yu','ya','e','y','i','g','+'),
						preg_replace('#[^a-za-y?????0-9_ --]#i','',strtolower($this->_cloud[$i]['tag'])));
				else
					$this->_cloud[$i]['link'] = preg_replace('#[^a-z0-9_ --]#i','',strtolower($this->_cloud[$i]['tag']));
		}
	}
	*/
	
	/**
	 * Create links for tags
	 *
	 */
	private function _makeLinksFromTags()
	{
		for($i=0;$i<=$this->_count-1;$i++)
			if (empty($this->_cloud[$i]['link']))
				$this->_cloud[$i]['link'] = preg_replace('#[^a-z0-9_ --]#i','',strtolower($this->_cloud[$i]['tag']));
	}
	
	/**
	 * Set color strength
	 *
	 * @param string $color
	 */
	private function _makeColor($color)
	{
		preg_match('/#?([0-9A-F]{1,2})([0-9A-F]{1,2})([0-9A-F]{1,2})/i',$color,$c); // HEX to color
		$this->color = empty($c) ? $this->color : array_map('hexdec',array_slice($c,1));
	}

	/**
	 * Set size
	 *
	 * @param int|arr $size
	 */
	protected function _makeSize($size)
	{
		$this->size = is_array($size) ? $size : array($size/2,$size);
	}
	
	/**
	 * Set factor foreach tag
	 *
	 */
	private function _makeTrueWeight()
	{
		$delta = ($this->size[1] - $this->size[0]) / ($this->_cloud[0]['weight'] - $this->_cloud[$this->_count-1]['weight']);

		for($i=0;$i<=$this->_count-1;$i++)
			$this->_cloud[$i]['factor'] = round($this->_cloud[$i]['weight']*$delta+$this->size[0],2);
	}
	
	/**
	 * Set color foreach tag
	 *
	 */
	private function _makeTrueColor()
	{
		$step['r'] = @round((255-$this->color[0]) / $this->_cloud[0]['factor'], 2); // @ - Division by zero
		$step['g'] = @round((255-$this->color[1]) / $this->_cloud[0]['factor'], 2);
		$step['b'] = @round((255-$this->color[2]) / $this->_cloud[0]['factor'], 2);
		
		for($i=0,$j=$this->_count-1;$i<=$this->_count-1;$i++,$j--)
		{
			$r = dechex(255-$step['r']*$this->_cloud[$j]['factor']);
			$g = dechex(255-$step['g']*$this->_cloud[$j]['factor']);
			$b = dechex(255-$step['b']*$this->_cloud[$j]['factor']);
			
			$this->_cloud[$j]['color'] = str_pad($r, 2, "0", STR_PAD_LEFT).str_pad($g, 2, "0", STR_PAD_LEFT).str_pad($b, 2, "0", STR_PAD_LEFT);
		}
	}

} // end of class

Испоьзовать так:



// Sample 1
$cloud = array(
1=>"January",
2=>"February",
3=>"March",
4=>"April",
5=>"May",
6=>"June",
7=>"July",
8=>"August",
9=>"September",
10=>"October",
11=>"November",
12=>"December", 
);

$cm = new CloudMaker('#00ff00',30,'http://localhost/','px');
$cm->addCloud($cloud);
echo $cm->makeCloud();

echo '<hr>';

// Sample 2
$cm = new CloudMaker('#00ff00',array(10,20),'http://localhost/index.php?tag=','px');
$cm->addCloud($cloud);
$cm->changeColor('#AC00BD');
$cm->changeSize(array(1,2),'em');
echo $cm->makeCloud();

echo '<hr>';

// Sample 3
$cloud2 = array(
array(1,"January","01"),
array(2,"February","02"),
array(3,"March","03"),
array(4,"April","04"),
array(5,"May","05"),
array(6,"June","06"),
array(7,"July","07"),
array(8,"August","08"),
array(9,"September","09"),
array(10,"October","10"),
array(11,"November","11"),
array(12,"December", "12"),
);

$cm = new CloudMaker('#107020',array(8,30),'http://localhost/index.php?year=2007&month=','px');
$cm->addCloud($cloud2);
echo "<div style='width:200px;border:1px solid red;'>".$cm->makeCloud()."</div>";

echo highlight_file(__FILE__);
Комментирование отключено.