Issues (12)

lib/Vizhash16x16.php (1 issue)

1
<?php
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
13
namespace 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
 */
25
class 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 2
    public function __construct()
65
    {
66 2
        $this->width  = 16;
67 2
        $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 2
    public function generate($text)
80
    {
81 2
        if (!function_exists('gd_info')) {
82
            return '';
83
        }
84
85 2
        $textlen = strlen($text);
86
87
        // We convert the hash into an array of integers.
88 2
        $this->VALUES = array();
89 2
        for ($i = 0; $i < $textlen; $i = $i + 2) {
90 2
            array_push($this->VALUES, hexdec(substr($text, $i, 2)));
91
        }
92 2
        $this->VALUES_INDEX = 0; // to walk the array.
93
94
        // Then use these integers to drive the creation of an image.
95 2
        $image = imagecreatetruecolor($this->width, $this->height);
96
97 2
        $r = $r0 = $this->getInt();
98 2
        $g = $g0 = $this->getInt();
99 2
        $b = $b0 = $this->getInt();
100
101
        // First, create an image with a specific gradient background.
102 2
        $op = 'v';
103 2
        if (($this->getInt() % 2) == 0) {
104 1
            $op = 'h';
105
        }
106 2
        $image = $this->degrade($image, $op, array($r0, $g0, $b0), array(0, 0, 0));
0 ignored issues
show
It seems like $image can also be of type GdImage; however, parameter $img of PrivateBin\Vizhash16x16::degrade() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

106
        $image = $this->degrade(/** @scrutinizer ignore-type */ $image, $op, array($r0, $g0, $b0), array(0, 0, 0));
Loading history...
107
108 2
        for ($i = 0; $i < 7; ++$i) {
109 2
            $action = $this->getInt();
110 2
            $color  = imagecolorallocate($image, $r, $g, $b);
111 2
            $r      = $r0      = (int) ($r0 + $this->getInt() / 25) % 256;
112 2
            $g      = $g0      = (int) ($g0 + $this->getInt() / 25) % 256;
113 2
            $b      = $b0      = (int) ($b0 + $this->getInt() / 25) % 256;
114 2
            $this->drawshape($image, $action, $color);
115
        }
116
117 2
        $color = imagecolorallocate($image, $this->getInt(), $this->getInt(), $this->getInt());
118 2
        $this->drawshape($image, $this->getInt(), $color);
119 2
        ob_start();
120 2
        imagepng($image);
121 2
        $imagedata = ob_get_contents();
122 2
        ob_end_clean();
123 2
        imagedestroy($image);
124
125 2
        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 2
    private function getInt()
135
    {
136 2
        $v = $this->VALUES[$this->VALUES_INDEX];
137 2
        ++$this->VALUES_INDEX;
138 2
        $this->VALUES_INDEX %= count($this->VALUES); // Wrap around the array
139 2
        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 2
    private function getX()
149
    {
150 2
        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 2
    private function getY()
160
    {
161 2
        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 2
    private function degrade($img, $direction, $color1, $color2)
178
    {
179 2
        if ($direction == 'h') {
180 1
            $size    = imagesx($img);
181 1
            $sizeinv = imagesy($img);
182
        } else {
183 2
            $size    = imagesy($img);
184 2
            $sizeinv = imagesx($img);
185
        }
186 2
        $diffs = array(
187 2
            ($color2[0] - $color1[0]) / $size,
188 2
            ($color2[1] - $color1[1]) / $size,
189 2
            ($color2[2] - $color1[2]) / $size,
190 2
        );
191 2
        for ($i = 0; $i < $size; ++$i) {
192 2
            $r = $color1[0] + ((int) $diffs[0] * $i);
193 2
            $g = $color1[1] + ((int) $diffs[1] * $i);
194 2
            $b = $color1[2] + ((int) $diffs[2] * $i);
195 2
            if ($direction == 'h') {
196 1
                imageline($img, $i, 0, $i, $sizeinv, imagecolorallocate($img, $r, $g, $b));
197
            } else {
198 2
                imageline($img, 0, $i, $sizeinv, $i, imagecolorallocate($img, $r, $g, $b));
199
            }
200
        }
201 2
        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 2
    private function drawshape($image, $action, $color)
213
    {
214 2
        switch ($action % 7) {
215 2
            case 0:
216 1
                imagefilledrectangle($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $color);
217 1
                break;
218 2
            case 1:
219 2
            case 2:
220 2
                imagefilledellipse($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $color);
221 2
                break;
222 2
            case 3:
223 1
                $points = array($this->getX(), $this->getY(), $this->getX(), $this->getY(), $this->getX(), $this->getY(), $this->getX(), $this->getY());
224 1
                version_compare(PHP_VERSION, '8.1', '<') ? imagefilledpolygon($image, $points, 4, $color) : imagefilledpolygon($image, $points, $color);
225 1
                break;
226
            default:
227 2
                $start = (int) ($this->getInt() * 360 / 256);
228 2
                $end   = (int) ($start + $this->getInt() * 180 / 256);
229 2
                imagefilledarc($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $start, $end, $color, IMG_ARC_PIE);
230
        }
231
    }
232
}
233