2012-04-21 19:59:45 +00:00
|
|
|
<?php
|
2013-11-01 00:15:14 +00:00
|
|
|
/**
|
|
|
|
* VizHash_GD
|
|
|
|
*
|
|
|
|
* Visual Hash implementation in php4+GD,
|
2016-07-11 09:58:15 +00:00
|
|
|
* stripped down and modified version for PrivateBin
|
2013-11-01 00:15:14 +00:00
|
|
|
*
|
|
|
|
* @link http://sebsauvage.net/wiki/doku.php?id=php:vizhash_gd
|
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
2016-07-19 11:56:52 +00:00
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2019-09-22 17:42:04 +00:00
|
|
|
* @version 0.0.5 beta PrivateBin 1.3.1
|
2013-11-01 00:15:14 +00:00
|
|
|
*/
|
2016-12-12 17:43:23 +00:00
|
|
|
|
2016-12-12 17:50:00 +00:00
|
|
|
namespace PrivateBin;
|
2016-07-21 15:09:48 +00:00
|
|
|
|
2013-11-01 00:15:14 +00:00
|
|
|
/**
|
2016-08-09 09:54:42 +00:00
|
|
|
* Vizhash16x16
|
2013-11-01 00:15:14 +00:00
|
|
|
*
|
|
|
|
* Example:
|
2016-08-09 09:54:42 +00:00
|
|
|
* $vz = new Vizhash16x16();
|
2016-08-25 07:53:31 +00:00
|
|
|
* $data = $vz->generate(sha512('hello'));
|
2013-11-01 00:15:14 +00:00
|
|
|
* header('Content-type: image/png');
|
|
|
|
* echo $data;
|
|
|
|
* exit;
|
|
|
|
*/
|
2016-08-09 09:54:42 +00:00
|
|
|
class Vizhash16x16
|
2012-04-21 19:59:45 +00:00
|
|
|
{
|
2015-08-16 13:55:31 +00:00
|
|
|
/**
|
|
|
|
* hash values
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var array
|
|
|
|
*/
|
2012-04-21 19:59:45 +00:00
|
|
|
private $VALUES;
|
2015-08-16 13:55:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* index of current value
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var int
|
|
|
|
*/
|
2012-04-21 19:59:45 +00:00
|
|
|
private $VALUES_INDEX;
|
2015-08-16 13:55:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* image width
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var int
|
|
|
|
*/
|
2012-04-21 19:59:45 +00:00
|
|
|
private $width;
|
2015-08-16 13:55:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* image height
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var int
|
|
|
|
*/
|
2012-04-21 19:59:45 +00:00
|
|
|
private $height;
|
2015-08-16 13:55:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* constructor
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public function __construct()
|
2012-04-21 19:59:45 +00:00
|
|
|
{
|
2015-08-16 13:55:31 +00:00
|
|
|
$this->width = 16;
|
|
|
|
$this->height = 16;
|
2012-04-29 17:15:06 +00:00
|
|
|
}
|
|
|
|
|
2015-08-16 13:55:31 +00:00
|
|
|
/**
|
|
|
|
* Generate a 16x16 png corresponding to $text.
|
|
|
|
*
|
2016-08-10 15:41:46 +00:00
|
|
|
* The given text should to be 128 to 150 characters long
|
|
|
|
*
|
2015-08-16 13:55:31 +00:00
|
|
|
* @access public
|
|
|
|
* @param string $text
|
|
|
|
* @return string PNG data. Or empty string if GD is not available.
|
|
|
|
*/
|
|
|
|
public function generate($text)
|
2012-04-21 19:59:45 +00:00
|
|
|
{
|
2016-07-26 06:19:35 +00:00
|
|
|
if (!function_exists('gd_info')) {
|
|
|
|
return '';
|
|
|
|
}
|
2012-04-21 19:59:45 +00:00
|
|
|
|
2016-08-10 16:22:28 +00:00
|
|
|
$textlen = strlen($text);
|
2012-04-21 19:59:45 +00:00
|
|
|
|
|
|
|
// We convert the hash into an array of integers.
|
2016-08-10 15:41:46 +00:00
|
|
|
$this->VALUES = array();
|
|
|
|
for ($i = 0; $i < $textlen; $i = $i + 2) {
|
|
|
|
array_push($this->VALUES, hexdec(substr($text, $i, 2)));
|
2016-07-26 06:19:35 +00:00
|
|
|
}
|
2016-08-10 15:41:46 +00:00
|
|
|
$this->VALUES_INDEX = 0; // to walk the array.
|
2012-04-21 19:59:45 +00:00
|
|
|
|
|
|
|
// Then use these integers to drive the creation of an image.
|
2016-07-26 06:19:35 +00:00
|
|
|
$image = imagecreatetruecolor($this->width, $this->height);
|
2012-04-21 19:59:45 +00:00
|
|
|
|
2016-08-10 15:41:46 +00:00
|
|
|
$r = $r0 = $this->getInt();
|
|
|
|
$g = $g0 = $this->getInt();
|
|
|
|
$b = $b0 = $this->getInt();
|
2012-04-21 19:59:45 +00:00
|
|
|
|
|
|
|
// First, create an image with a specific gradient background.
|
2016-08-10 15:41:46 +00:00
|
|
|
$op = 'v';
|
|
|
|
if (($this->getInt() % 2) == 0) {
|
|
|
|
$op = 'h';
|
2016-10-29 08:24:08 +00:00
|
|
|
}
|
2016-07-26 06:19:35 +00:00
|
|
|
$image = $this->degrade($image, $op, array($r0, $g0, $b0), array(0, 0, 0));
|
2012-04-21 19:59:45 +00:00
|
|
|
|
2016-08-10 15:41:46 +00:00
|
|
|
for ($i = 0; $i < 7; ++$i) {
|
|
|
|
$action = $this->getInt();
|
2016-08-15 14:45:47 +00:00
|
|
|
$color = imagecolorallocate($image, $r, $g, $b);
|
|
|
|
$r = $r0 = ($r0 + $this->getInt() / 25) % 256;
|
|
|
|
$g = $g0 = ($g0 + $this->getInt() / 25) % 256;
|
|
|
|
$b = $b0 = ($b0 + $this->getInt() / 25) % 256;
|
2016-07-26 06:19:35 +00:00
|
|
|
$this->drawshape($image, $action, $color);
|
2012-04-21 19:59:45 +00:00
|
|
|
}
|
|
|
|
|
2016-07-26 06:19:35 +00:00
|
|
|
$color = imagecolorallocate($image, $this->getInt(), $this->getInt(), $this->getInt());
|
|
|
|
$this->drawshape($image, $this->getInt(), $color);
|
2012-04-21 19:59:45 +00:00
|
|
|
ob_start();
|
|
|
|
imagepng($image);
|
|
|
|
$imagedata = ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
imagedestroy($image);
|
2012-04-29 17:15:06 +00:00
|
|
|
|
2012-04-21 19:59:45 +00:00
|
|
|
return $imagedata;
|
2012-04-29 17:15:06 +00:00
|
|
|
}
|
|
|
|
|
2015-08-16 13:55:31 +00:00
|
|
|
/**
|
|
|
|
* Returns a single integer from the $VALUES array (0...255)
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private function getInt()
|
2012-04-21 19:59:45 +00:00
|
|
|
{
|
2016-08-10 15:41:46 +00:00
|
|
|
$v = $this->VALUES[$this->VALUES_INDEX];
|
|
|
|
++$this->VALUES_INDEX;
|
2012-04-21 19:59:45 +00:00
|
|
|
$this->VALUES_INDEX %= count($this->VALUES); // Warp around the array
|
|
|
|
return $v;
|
|
|
|
}
|
2015-08-15 14:37:44 +00:00
|
|
|
|
2015-08-16 13:55:31 +00:00
|
|
|
/**
|
|
|
|
* Returns a single integer from the array (roughly mapped to image width)
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private function getX()
|
2012-04-21 19:59:45 +00:00
|
|
|
{
|
2016-08-10 15:41:46 +00:00
|
|
|
return $this->width * $this->getInt() / 256;
|
2012-04-21 19:59:45 +00:00
|
|
|
}
|
|
|
|
|
2015-08-16 13:55:31 +00:00
|
|
|
/**
|
|
|
|
* Returns a single integer from the array (roughly mapped to image height)
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private function getY()
|
2012-04-29 17:15:06 +00:00
|
|
|
{
|
2016-08-10 15:41:46 +00:00
|
|
|
return $this->height * $this->getInt() / 256;
|
2012-04-29 17:15:06 +00:00
|
|
|
}
|
|
|
|
|
2015-08-16 13:55:31 +00:00
|
|
|
/**
|
|
|
|
* Gradient function
|
|
|
|
*
|
|
|
|
* taken from:
|
|
|
|
* http://www.supportduweb.com/scripts_tutoriaux-code-source-41-gd-faire-un-degrade-en-php-gd-fonction-degrade-imagerie.html
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param resource $img
|
|
|
|
* @param string $direction
|
|
|
|
* @param array $color1
|
|
|
|
* @param array $color2
|
|
|
|
* @return resource
|
|
|
|
*/
|
2016-07-26 06:19:35 +00:00
|
|
|
private function degrade($img, $direction, $color1, $color2)
|
2012-04-21 19:59:45 +00:00
|
|
|
{
|
2016-08-10 15:41:46 +00:00
|
|
|
if ($direction == 'h') {
|
2016-08-15 14:45:47 +00:00
|
|
|
$size = imagesx($img);
|
2016-07-26 06:19:35 +00:00
|
|
|
$sizeinv = imagesy($img);
|
|
|
|
} else {
|
2016-08-15 14:45:47 +00:00
|
|
|
$size = imagesy($img);
|
2016-07-26 06:19:35 +00:00
|
|
|
$sizeinv = imagesx($img);
|
|
|
|
}
|
|
|
|
$diffs = array(
|
2016-08-10 15:41:46 +00:00
|
|
|
(($color2[0] - $color1[0]) / $size),
|
|
|
|
(($color2[1] - $color1[1]) / $size),
|
2016-10-29 08:24:08 +00:00
|
|
|
(($color2[2] - $color1[2]) / $size),
|
2016-08-10 15:41:46 +00:00
|
|
|
);
|
|
|
|
for ($i = 0; $i < $size; ++$i) {
|
|
|
|
$r = $color1[0] + ($diffs[0] * $i);
|
|
|
|
$g = $color1[1] + ($diffs[1] * $i);
|
|
|
|
$b = $color1[2] + ($diffs[2] * $i);
|
|
|
|
if ($direction == 'h') {
|
2016-07-26 06:19:35 +00:00
|
|
|
imageline($img, $i, 0, $i, $sizeinv, imagecolorallocate($img, $r, $g, $b));
|
|
|
|
} else {
|
|
|
|
imageline($img, 0, $i, $sizeinv, $i, imagecolorallocate($img, $r, $g, $b));
|
2012-04-21 19:59:45 +00:00
|
|
|
}
|
2016-07-26 06:19:35 +00:00
|
|
|
}
|
|
|
|
return $img;
|
2012-04-21 19:59:45 +00:00
|
|
|
}
|
2012-04-29 17:15:06 +00:00
|
|
|
|
2015-08-16 13:55:31 +00:00
|
|
|
/**
|
|
|
|
* Draw a shape
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param resource $image
|
|
|
|
* @param int $action
|
|
|
|
* @param int $color
|
|
|
|
*/
|
2016-07-26 06:19:35 +00:00
|
|
|
private function drawshape($image, $action, $color)
|
2012-04-21 19:59:45 +00:00
|
|
|
{
|
2016-08-10 15:41:46 +00:00
|
|
|
switch ($action % 7) {
|
2012-04-21 19:59:45 +00:00
|
|
|
case 0:
|
2016-10-29 08:24:08 +00:00
|
|
|
imagefilledrectangle($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $color);
|
2012-04-21 19:59:45 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
case 2:
|
2016-10-29 08:24:08 +00:00
|
|
|
imagefilledellipse($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $color);
|
2012-04-21 19:59:45 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
2016-08-10 16:22:28 +00:00
|
|
|
$points = array($this->getX(), $this->getY(), $this->getX(), $this->getY(), $this->getX(), $this->getY(), $this->getX(), $this->getY());
|
2016-10-29 08:24:08 +00:00
|
|
|
imagefilledpolygon($image, $points, 4, $color);
|
2012-04-21 19:59:45 +00:00
|
|
|
break;
|
2015-08-15 14:37:44 +00:00
|
|
|
default:
|
2016-08-15 14:45:47 +00:00
|
|
|
$start = $this->getInt() * 360 / 256;
|
|
|
|
$end = $start + $this->getInt() * 180 / 256;
|
2016-10-29 08:24:08 +00:00
|
|
|
imagefilledarc($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $start, $end, $color, IMG_ARC_PIE);
|
2012-04-21 19:59:45 +00:00
|
|
|
}
|
2012-04-29 17:15:06 +00:00
|
|
|
}
|
|
|
|
}
|