PrivateBin\Vizhash16x16::generate PHP Метод

generate() публичный Метод

The given text should to be 128 to 150 characters long
public generate ( string $text ) : string
$text string
Результат string PNG data. Or empty string if GD is not available.
    public function generate($text)
    {
        if (!function_exists('gd_info')) {
            return '';
        }
        $textlen = strlen($text);
        // We convert the hash into an array of integers.
        $this->VALUES = array();
        for ($i = 0; $i < $textlen; $i = $i + 2) {
            array_push($this->VALUES, hexdec(substr($text, $i, 2)));
        }
        $this->VALUES_INDEX = 0;
        // to walk the array.
        // Then use these integers to drive the creation of an image.
        $image = imagecreatetruecolor($this->width, $this->height);
        $r = $r0 = $this->getInt();
        $g = $g0 = $this->getInt();
        $b = $b0 = $this->getInt();
        // First, create an image with a specific gradient background.
        $op = 'v';
        if ($this->getInt() % 2 == 0) {
            $op = 'h';
        }
        $image = $this->degrade($image, $op, array($r0, $g0, $b0), array(0, 0, 0));
        for ($i = 0; $i < 7; ++$i) {
            $action = $this->getInt();
            $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;
            $this->drawshape($image, $action, $color);
        }
        $color = imagecolorallocate($image, $this->getInt(), $this->getInt(), $this->getInt());
        $this->drawshape($image, $this->getInt(), $color);
        ob_start();
        imagepng($image);
        $imagedata = ob_get_contents();
        ob_end_clean();
        imagedestroy($image);
        return $imagedata;
    }

Usage Example

Пример #1
0
 /**
  * Set nickname.
  *
  * @access public
  * @param string $nickname
  * @throws Exception
  * @return void
  */
 public function setNickname($nickname)
 {
     if (!Sjcl::isValid($nickname)) {
         throw new Exception('Invalid data.', 66);
     }
     $this->_data->meta->nickname = $nickname;
     // If a nickname is provided, we generate an icon based on a SHA512 HMAC
     // of the users IP. (We assume that if the user did not enter a nickname,
     // the user wants to be anonymous and we will not generate an icon.)
     $icon = $this->_conf->getKey('icon');
     if ($icon != 'none') {
         $pngdata = '';
         $hmac = TrafficLimiter::getHash();
         if ($icon == 'identicon') {
             $identicon = new Identicon();
             $pngdata = $identicon->getImageDataUri($hmac, 16);
         } elseif ($icon == 'vizhash') {
             $vh = new Vizhash16x16();
             $pngdata = 'data:image/png;base64,' . base64_encode($vh->generate($hmac));
         }
         if ($pngdata != '') {
             $this->_data->meta->vizhash = $pngdata;
         }
     }
     // Once the icon is generated, we do not keep the IP address hash.
 }