Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.59% covered (success)
98.59%
70 / 71
85.71% covered (success)
85.71%
6 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Vizhash16x16
98.59% covered (success)
98.59%
70 / 71
85.71% covered (success)
85.71%
6 / 7
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 generate
96.67% covered (success)
96.67%
29 / 30
0.00% covered (danger)
0.00%
0 / 1
5
 getInt
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getX
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getY
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 degrade
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
4
 drawshape
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
7
1<?php declare(strict_types=1);
2/**
3 * VizHash_GD
4 *
5 * Visual Hash implementation in php4+GD,
6 * stripped down from version 0.0.5 beta, modified for PrivateBin
7 *
8 * @link      https://sebsauvage.net/wiki/doku.php?id=php:vizhash_gd
9 * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
10 * @license   https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
11 */
12
13namespace PrivateBin;
14
15/**
16 * Vizhash16x16
17 *
18 * Example:
19 * $vz = new Vizhash16x16();
20 * $data = $vz->generate(sha512('hello'));
21 * header('Content-type: image/png');
22 * echo $data;
23 * exit;
24 */
25class Vizhash16x16
26{
27    /**
28     * hash values
29     *
30     * @access private
31     * @var    array
32     */
33    private $VALUES;
34
35    /**
36     * index of current value
37     *
38     * @access private
39     * @var    int
40     */
41    private $VALUES_INDEX;
42
43    /**
44     * image width
45     *
46     * @access private
47     * @var    int
48     */
49    private $width;
50
51    /**
52     * image height
53     *
54     * @access private
55     * @var    int
56     */
57    private $height;
58
59    /**
60     * constructor
61     *
62     * @access public
63     */
64    public function __construct()
65    {
66        $this->width  = 16;
67        $this->height = 16;
68    }
69
70    /**
71     * Generate a 16x16 png corresponding to $text.
72     *
73     * The given text should to be 128 to 150 characters long
74     *
75     * @access public
76     * @param  string $text
77     * @return string PNG data. Or empty string if GD is not available.
78     */
79    public function generate($text)
80    {
81        if (!function_exists('gd_info')) {
82            return '';
83        }
84
85        $textlen = strlen($text);
86
87        // We convert the hash into an array of integers.
88        $this->VALUES = array();
89        for ($i = 0; $i < $textlen; $i = $i + 2) {
90            array_push($this->VALUES, hexdec(substr($text, $i, 2)));
91        }
92        $this->VALUES_INDEX = 0; // to walk the array.
93
94        // Then use these integers to drive the creation of an image.
95        $image = imagecreatetruecolor($this->width, $this->height);
96
97        $r = $r0 = $this->getInt();
98        $g = $g0 = $this->getInt();
99        $b = $b0 = $this->getInt();
100
101        // First, create an image with a specific gradient background.
102        $op = 'v';
103        if (($this->getInt() % 2) == 0) {
104            $op = 'h';
105        }
106        $image = $this->degrade($image, $op, array($r0, $g0, $b0), array(0, 0, 0));
107
108        for ($i = 0; $i < 7; ++$i) {
109            $action = $this->getInt();
110            $color  = imagecolorallocate($image, $r, $g, $b);
111            $r      = $r0      = (int) ($r0 + $this->getInt() / 25) % 256;
112            $g      = $g0      = (int) ($g0 + $this->getInt() / 25) % 256;
113            $b      = $b0      = (int) ($b0 + $this->getInt() / 25) % 256;
114            $this->drawshape($image, $action, $color);
115        }
116
117        $color = imagecolorallocate($image, $this->getInt(), $this->getInt(), $this->getInt());
118        $this->drawshape($image, $this->getInt(), $color);
119        ob_start();
120        imagepng($image);
121        $imagedata = ob_get_contents();
122        ob_end_clean();
123        imagedestroy($image);
124
125        return $imagedata;
126    }
127
128    /**
129     * Returns a single integer from the $VALUES array (0...255)
130     *
131     * @access private
132     * @return int
133     */
134    private function getInt()
135    {
136        $v = $this->VALUES[$this->VALUES_INDEX];
137        ++$this->VALUES_INDEX;
138        $this->VALUES_INDEX %= count($this->VALUES); // Wrap around the array
139        return $v;
140    }
141
142    /**
143     * Returns a single integer from the array (roughly mapped to image width)
144     *
145     * @access private
146     * @return int
147     */
148    private function getX()
149    {
150        return (int) ($this->width * $this->getInt() / 256);
151    }
152
153    /**
154     * Returns a single integer from the array (roughly mapped to image height)
155     *
156     * @access private
157     * @return int
158     */
159    private function getY()
160    {
161        return (int) ($this->height * $this->getInt() / 256);
162    }
163
164    /**
165     * Gradient function
166     *
167     * taken from:
168     * @link   https://www.supportduweb.com/scripts_tutoriaux-code-source-41-gd-faire-un-degrade-en-php-gd-fonction-degrade-imagerie.html
169     *
170     * @access private
171     * @param  resource $img
172     * @param  string $direction
173     * @param  array $color1
174     * @param  array $color2
175     * @return resource
176     */
177    private function degrade($img, $direction, $color1, $color2)
178    {
179        if ($direction == 'h') {
180            $size    = imagesx($img);
181            $sizeinv = imagesy($img);
182        } else {
183            $size    = imagesy($img);
184            $sizeinv = imagesx($img);
185        }
186        $diffs = array(
187            ($color2[0] - $color1[0]) / $size,
188            ($color2[1] - $color1[1]) / $size,
189            ($color2[2] - $color1[2]) / $size,
190        );
191        for ($i = 0; $i < $size; ++$i) {
192            $r = $color1[0] + ((int) $diffs[0] * $i);
193            $g = $color1[1] + ((int) $diffs[1] * $i);
194            $b = $color1[2] + ((int) $diffs[2] * $i);
195            if ($direction == 'h') {
196                imageline($img, $i, 0, $i, $sizeinv, imagecolorallocate($img, $r, $g, $b));
197            } else {
198                imageline($img, 0, $i, $sizeinv, $i, imagecolorallocate($img, $r, $g, $b));
199            }
200        }
201        return $img;
202    }
203
204    /**
205     * Draw a shape
206     *
207     * @access private
208     * @param  resource $image
209     * @param  int $action
210     * @param  int $color
211     */
212    private function drawshape($image, $action, $color)
213    {
214        switch ($action % 7) {
215            case 0:
216                imagefilledrectangle($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $color);
217                break;
218            case 1:
219            case 2:
220                imagefilledellipse($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $color);
221                break;
222            case 3:
223                $points = array($this->getX(), $this->getY(), $this->getX(), $this->getY(), $this->getX(), $this->getY(), $this->getX(), $this->getY());
224                version_compare(PHP_VERSION, '8.1', '<') ? imagefilledpolygon($image, $points, 4, $color) : imagefilledpolygon($image, $points, $color);
225                break;
226            default:
227                $start = (int) ($this->getInt() * 360 / 256);
228                $end   = (int) ($start + $this->getInt() * 180 / 256);
229                imagefilledarc($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $start, $end, $color, IMG_ARC_PIE);
230        }
231    }
232}