Last active
August 29, 2015 14:18
-
-
Save mistic100/0a07b36522dc10d3b58a to your computer and use it in GitHub Desktop.
PHP image hash generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Squaricon | |
* | |
* @description: PHP image hash generator | |
* | |
* @author: Damien "Mistic" Sorel - http://strangeplanet.fr | |
* @license: MIT | |
* @version: 2.0.0 | |
* @date: 21/05/2015 | |
* | |
* @usage: Init Identicon\Squareicon with an hash and an optional array of options | |
* - size : image size (between 8 and 512, default 128) | |
* - colors : number of colors (1 or 2, default 2) | |
* - symmetry : symmetry type (default 0) | |
* 0 for no symmetry | |
* 1 for vertical symmetry | |
* 2 for horizontal symmetry | |
* 3 for central symmetry | |
* | |
* @example: direct render | |
* > (new Squareicon($_GET['hash'], $_GET))->display(); | |
* | |
* @example: get image ressource | |
* > $img = (new Squareicon($hash))->render(); | |
* | |
* The MIT License (MIT) | |
* | |
* Copyright (c) 2013-2015 Damien "Mistic" Sorel | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining | |
* a copy of this software and associated documentation files (the | |
* 'Software'), to deal in the Software without restriction, including | |
* without limitation the rights to use, copy, modify, merge, publish, | |
* distribute, sublicense, and/or sell copies of the Software, and to | |
* permit persons to whom the Software is furnished to do so, subject to | |
* the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be | |
* included in all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
* | |
*/ | |
namespace Identicon; | |
class Squareicon | |
{ | |
/** | |
* User hash | |
* @var string | |
*/ | |
public $hash = null; | |
/** | |
* Image size (between 8 and 512) | |
* @var int | |
*/ | |
public $size = 128; | |
/** | |
* Number of colors (1 or 2) | |
* @var int | |
*/ | |
public $colors = 2; | |
/** | |
* Type of symmetry (0, 1, 2 or 3) | |
* @var int | |
*/ | |
public $symmetry = 0; | |
/** | |
* Storage of the image | |
* @var resource | |
*/ | |
private $_img = null; | |
/** | |
* class constructor | |
* @param string $hash | |
* @param array $options | |
*/ | |
public function __construct($hash=null, $options=array()) | |
{ | |
$this->hash = $hash; | |
if (!$this->hash) | |
{ | |
$this->hash = md5(uniqid(time(), true)); | |
} | |
// we need 44 chars (176 bits) | |
$this->hash = str_pad($this->hash, 44, $this->hash); | |
// size is between 8 and 512 | |
if (array_key_exists('size', $options)) | |
{ | |
$this->size = max(8, min(intval($options['size']), 512)); | |
} | |
// colors is 1 or 2 | |
if (array_key_exists('colors', $options)) | |
{ | |
$this->colors = max(1, min(intval($options['colors']), 2)); | |
} | |
// symmetry is 0, 1, 2 or 3 | |
if (array_key_exists('symmetry', $options)) | |
{ | |
$this->symmetry = max(0, min(intval($options['symmetry']), 3)); | |
} | |
} | |
/** | |
* Render the squareicon and returns the image resource | |
* @return resource | |
*/ | |
public function render() | |
{ | |
if ($this->_img != null) | |
{ | |
return $this->_img; | |
} | |
## PARSE HASH | |
$c = array(); | |
// color 1 (chars 0 to 5, 24 bits) | |
$c[0] = array( | |
hexdec(substr($this->hash, 0, 2)), | |
hexdec(substr($this->hash, 2, 2)), | |
hexdec(substr($this->hash, 4, 2)), | |
); | |
// color 2 (chars 6 to 11, 24 bits) | |
if ($this->colors == 2) | |
{ | |
$c[1] = array( | |
hexdec(substr($this->hash, 6, 2)), | |
hexdec(substr($this->hash, 8, 2)), | |
hexdec(substr($this->hash, 10, 2)), | |
); | |
} | |
else | |
{ | |
$c[1] = $c[0]; | |
} | |
// white is used as transparent color | |
if (array_sum($c[0]) == 255*3) $c[0][0] = 254; | |
if (array_sum($c[1]) == 255*3) $c[1][0] = 254; | |
// squares presence (chars 12 to 27, 64 bits) | |
$sq = ''; | |
for ($i=12; $i<28; $i++) | |
{ | |
$t = substr($this->hash, $i, 1); | |
$t = decbin(hexdec($t)); | |
$t = str_pad($t, 4, '0', STR_PAD_LEFT); | |
$sq.= $t; | |
} | |
// squares color (chars 28 to 43, 64 bits) | |
$sqc = ''; | |
for ($i=28; $i<44; $i++) | |
{ | |
$t = substr($this->hash, $i, 1); | |
$t = decbin(hexdec($t)); | |
$t = str_pad($t, 4, '0', STR_PAD_LEFT); | |
$sqc.= $t; | |
} | |
if ($this->symmetry & 1) | |
{ | |
$tmp = str_split($sq, 4); | |
for ($i=0,$l=count($tmp); $i<$l; $i+=2) | |
{ | |
$tmp[$i+1] = strrev($tmp[$i]); | |
} | |
$sq = implode('', $tmp); | |
$tmp = str_split($sqc, 4); | |
for ($i=0,$l=count($tmp); $i<$l; $i+=2) | |
{ | |
$tmp[$i+1] = strrev($tmp[$i]); | |
} | |
$sqc = implode('', $tmp); | |
} | |
if ($this->symmetry & 2) | |
{ | |
$tmp = str_split($sq, 8); | |
for ($i=0,$l=count($tmp); $i<$l/2; $i++) | |
{ | |
$tmp[$l-$i-1] = $tmp[$i]; | |
} | |
$sq = implode('', $tmp); | |
$tmp = str_split($sqc, 8); | |
for ($i=0,$l=count($tmp); $i<$l/2; $i++) | |
{ | |
$tmp[$l-$i-1] = $tmp[$i]; | |
} | |
$sqc = implode('', $tmp); | |
} | |
## RENDER SQUARICON | |
$this->_img = imagecreatetruecolor($this->size, $this->size); | |
imageantialias($this->_img, true); | |
$bg = imagecolorallocate($this->_img, 255, 255, 255); | |
$c0 = imagecolorallocate($this->_img, $c[0][0], $c[0][1], $c[0][2]); | |
$c1 = imagecolorallocate($this->_img, $c[1][0], $c[1][1], $c[1][2]); | |
imagefilledrectangle($this->_img, 0, 0, $this->size, $this->size, $bg); | |
$l=0; $c=0; // line, column | |
$s = $this->size/8; // size of each square | |
for ($i=0; $i<64; $i++) | |
{ | |
if (!!($sq[$i])) | |
{ | |
imagefilledrectangle($this->_img, $c*$s, $l*$s, ($c+1)*$s-1, ($l+1)*$s-1, !!($sqc[$i]) ? $c0 : $c1); | |
} | |
$c++; | |
if ($c == 8) | |
{ | |
$c=0; | |
$l++; | |
} | |
} | |
imagecolortransparent($this->_img, $bg); | |
return $this->_img; | |
} | |
/** | |
* Display the squareicon to the browser | |
*/ | |
public function display() | |
{ | |
header('Content-Type: image/png'); | |
imagepng($this->render()); | |
} | |
/** | |
* Destroy the internal resource storage | |
*/ | |
public function destroy() | |
{ | |
if ($this->_img != null) | |
{ | |
imagedestroy($this->_img); | |
$this->_img = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment