Skip to content

Instantly share code, notes, and snippets.

@nevkontakte

nevkontakte/fc.php

Created Jul 13, 2020
Embed
What would you like to do?
<?php
/**
* FakeBurner
* FeedBurner counter imitator / Имитатор счетчика FeedBurner
* Originally written for Zebrum Bloglike (http://zebrum.net.ru)
*
* @author Alek$ <aleks@aradmin.org.ru> http://nevkontakte.org.ru
* @copyright Alek$, 2009
* @license GNU General Public License v 2
*/
/**
* Feel free to use and spread this script, but please keep my copyrights
* Вы можете свободно пользоваться этим скриптом и распространять его, но, пожалуйста, сохраняйте мои копирайты
*/
$bgcolor = '#99CCFF'; // Основной цвет фона
$fgcolor = '#000000'; // Цвет текста
$readers = 50; // Среднее количество читателей
$fluctuation = 10; // Максимальное отклонение от среднего количества читателей
class FakeBurner
{
private $readers = 0;
private $fluct = 0;
private $fgcolor = '000000';
private $bgcolor = '99cccc';
private $img;
function FakeBurner($options = null)
{
if(is_array($options))
{
$this->SetOptions($options);
}
$this->img = imagecreatetruecolor(88, 26);
}
function SetOptions($options)
{
if(is_array($options))
{
foreach($options as $key => $val)
{
$this->SetOption($key, $val);
}
}
}
function SetOption($key, $value)
{
switch($key)
{
case 'readers':
return $this->readers = intval($value);
case 'fluct':
case 'fluctuations':
return $this->fluct = intval($value);
case 'bgcolor':
case 'bg':
return $this->bgcolor = ltrim($value, '#');
case 'fgcolor':
case 'fg':
return $this->fgcolor = ltrim($value, '#');
}
}
function Rgb2hsv($r, $g, $b)
{
$r /= 255;
$g /= 255;
$b /= 255;
$V = max($r, max($g, $b));
$v = min($r, min($g, $b));
$S = ($V == 0) ? 0 : ($V - $v)/$V;
if($V - $v == 0)
{
$cr = $cg = $cb = 0;
}
else
{
$cr = ($V - $r)/($V - $v);
$cg = ($V - $g)/($V - $v);
$cb = ($V - $b)/($V - $v);
}
if($r == $V)
{
$H = $cb - $cg;
}
else if($g == $V)
{
$H = $cr - $cb + 2;
}
else if($b == $V)
{
$H = $cg - $cr + 4;
}
if($H < 0)
{
$H += 6;
}
return array('h' => round($H*60), 's' => round($S*100), 'v' => round($V*100));
}
function Hsv2rgb($H, $S, $V)
{
$H /= 60;
$S /= 100;
$V /= 100;
$f = $H - intval($H);
$m = $V * (1 - $S);
$i = intval($H);
$n = $V * (1 - $S * $f);
$k = $V * (1 - $S * (1 - $f));
switch($i)
{
case 0:
return array('r' => round($V*255), 'g' => round($k*255), 'b' => round($m*255));
case 1:
return array('r' => round($n*255), 'g' => round($V*255), 'b' => round($m*255));
case 2:
return array('r' => round($m*255), 'g' => round($V*255), 'b' => round($k*255));
case 3:
return array('r' => round($m*255), 'g' => round($n*255), 'b' => round($V*255));
case 4:
return array('r' => round($k*255), 'g' => round($m*255), 'b' => round($V*255));
case 5:
case 6:
return array('r' => round($V*255), 'g' => round($m*255), 'b' => round($n*255));
}
}
function ModifyColor($r, $g, $b, $h = null, $s = null, $v = null)
{
$hsv = $this->Rgb2hsv($r, $g, $b);
if($h !== NULL)
{
$val = intval(substr($h, 1));
switch($h[0])
{
case ' ':
$hsv['h'] = $val;
break;
case '+':
$hsv['h'] += $val;
break;
case '-':
$hsv['h'] -= $val;
break;
}
$hsv['h'] = min(360, max(0, $hsv['h']));
}
if($v !== NULL)
{
$val = intval(substr($v, 1));
switch($v[0])
{
case ' ':
$hsv['v'] = $val;
break;
case '+':
$hsv['v'] += $val;
break;
case '-':
$hsv['v'] -= $val;
break;
}
$hsv['v'] = min(100, max(0, $hsv['v']));
}
if($s !== NULL)
{
$val = intval(substr($s, 1));
switch($s[0])
{
case ' ':
$hsv['s'] = $val;
break;
case '+':
$hsv['s'] += $val;
break;
case '-':
$hsv['s'] -= $val ;
break;
}
$hsv['s'] = min(100, max(0, $hsv['s']));
}
return $this->Hsv2rgb($hsv['h'], $hsv['s'], $hsv['v']);
}
function GetReaders()
{
return max(0, $this->readers + ((date('d')+date('m')+date('y')+date('H')) % ($this->fluct*2)) - $this->fluct);
}
function Render($fakeburner = false)
{
// Calculate colors
$bgrgb= array(
'r' => hexdec(substr($this->bgcolor, 0, 2)),
'g' => hexdec(substr($this->bgcolor, 2, 2)),
'b' => hexdec(substr($this->bgcolor, 4, 2)),
);
$fgrgb= array(
'r' => hexdec(substr($this->fgcolor, 0, 2)),
'g' => hexdec(substr($this->fgcolor, 2, 2)),
'b' => hexdec(substr($this->fgcolor, 4, 2)),
);
//var_dump($fgrgb);
$bgboxrgb = $this->ModifyColor($bgrgb['r'], $bgrgb['g'], $bgrgb['b'], null, '-25', '+5');
$bgboxdarkrgb = $this->ModifyColor($bgrgb['r'], $bgrgb['g'], $bgrgb['b'], null, '-25', '-20');
$borderlightrgb = $this->ModifyColor($bgrgb['r'], $bgrgb['g'], $bgrgb['b'], null, null, ' 100');
$borderdarkrgb = $this->ModifyColor($bgrgb['r'], $bgrgb['g'], $bgrgb['b'], null, '-25', '-40');
// Init color resources
$transparent = imagecolorallocate($this->img, 255, 1, 254);
$bg = imagecolorallocate($this->img, $bgrgb['r'], $bgrgb['g'], $bgrgb['b']);
$bgbox = imagecolorallocate($this->img, $bgboxrgb['r'], $bgboxrgb['g'], $bgboxrgb['b']);
$bgboxdark = imagecolorallocate($this->img, $bgboxdarkrgb['r'], $bgboxdarkrgb['g'], $bgboxdarkrgb['b']);
$bgboxlight = imagecolorallocate($this->img, 255, 255, 255);
$borderlight = imagecolorallocate($this->img, $borderlightrgb['r'], $borderlightrgb['g'], $borderlightrgb['b']);
$borderdark = imagecolorallocate($this->img, $borderdarkrgb['r'], $borderdarkrgb['g'], $borderdarkrgb['b']);
$textcolor = imagecolorallocate($this->img, $fgrgb['r'], $fgrgb['g'], $fgrgb['b']);
$byfeedburner = imagecolorallocate($this->img, 108, 108, 108);
// Enable transparency
imagecolortransparent($this->img, $transparent);
// Draw background
imagefilledrectangle($this->img, 0, 0, 88, 26, $transparent);
// Main bar
imagefilledrectangle($this->img, 0, 0, 88, 18, $bg);
imageline($this->img, 0, 0, 88, 0, $borderlight);
imageline($this->img, 0, 0, 0, 18, $borderlight);
imageline($this->img, 1, 18, 87, 18, $borderdark);
imageline($this->img, 87, 1, 87, 18, $borderdark);
// Number bar
imagefilledrectangle($this->img, 3, 2, 38, 15, $bgbox);
imageline($this->img, 3, 2, 3, 15, $bgboxdark);
imageline($this->img, 3, 2, 38, 2, $bgboxdark);
imageline($this->img, 4, 15, 38, 15, $bgboxlight);
imageline($this->img, 38, 3, 38, 15, $bgboxlight);
//Draw text
$x = 36 - imagefontwidth(2)*strlen($this->readers);
imagestring($this->img, 2, $x, 2, $this->GetReaders(), $textcolor);
imagestring($this->img, 2, 43, 2, 'readers', $textcolor);
if($fakeburner)
{
$str = 'BY FAKEBURNER';
}
else
{
$str = 'BY FEEDBURNER';
}
$width = imagesx($this->img) - 2;
$step = $width/strlen($str);
//var_dump($width, $step);
for($i = 0; $i < strlen($str); $i ++)
{
imagestring($this->img, 1, 2 + $i*$step, 19, $str[$i], $byfeedburner);
}
}
function Output($fakeburner = false)
{
$this->Render($fakeburner);
header('Content-Type: image/gif');
imagegif($this->img);
}
}
$fc = new FakeBurner();
$fc->SetOption('bg', $bgcolor);
$fc->SetOption('fg', $fgcolor);
$fc->SetOption('fluct', $fluctuation);
$fc->SetOption('readers', $readers);
$fc->Output();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment