Skip to content

Instantly share code, notes, and snippets.

@jamiebicknell
Last active March 14, 2016 00:24
Show Gist options
  • Select an option

  • Save jamiebicknell/9964033 to your computer and use it in GitHub Desktop.

Select an option

Save jamiebicknell/9964033 to your computer and use it in GitHub Desktop.

Revisions

  1. jamiebicknell revised this gist Apr 3, 2014. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  2. jamiebicknell revised this gist Apr 3, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion dash_alt.php
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,9 @@
    <?php

    function dHashAlt($file, $size = 8)
    function dHashAlt($file)
    {
    $hash = '';
    $size = 8;
    list($w, $h, $t) = getimagesize($file);
    $im = imagecreatetruecolor($size + 1, $size);
    imagefilter($im, IMG_FILTER_GRAYSCALE);
  3. jamiebicknell revised this gist Apr 3, 2014. 2 changed files with 8 additions and 8 deletions.
    8 changes: 4 additions & 4 deletions dash.php
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,10 @@
    <?php

    function dhash($file, $size = 8)
    function dHash($file, $size = 8)
    {
    $hash = '';
    list($w, $h, $t) = getimagesize($file);
    $im = imagecreatetruecolor($size+1, $size);
    $im = imagecreatetruecolor($size + 1, $size);
    imagefilter($im, IMG_FILTER_GRAYSCALE);
    switch($t) {
    case 1:
    @@ -17,13 +17,13 @@ function dhash($file, $size = 8)
    $oi = imagecreatefrompng($file);
    break;
    }
    imagecopyresampled($im, $oi, 0, 0, 0, 0, $size+1, $size, $w, $h);
    imagecopyresampled($im, $oi, 0, 0, 0, 0, $size + 1, $size, $w, $h);
    imagedestroy($oi);
    for ($y = 0; $y < $size; $y++) {
    $val = 0;
    for ($x = 0; $x < $size; $x++) {
    $curr = imagecolorat($im, $x, $y);
    $next = imagecolorat($im, $x+1, $y);
    $next = imagecolorat($im, $x + 1, $y);
    $val += ($curr > $next) ? pow(2, ($x + ($y * $size)) % 8) : 0;
    }
    $hash .= str_pad(dechex($val), 2, 0, STR_PAD_LEFT);
    8 changes: 4 additions & 4 deletions dash_alt.php
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,10 @@
    <?php

    function dhash_alt($file, $size = 8)
    function dHashAlt($file, $size = 8)
    {
    $hash = '';
    list($w, $h, $t) = getimagesize($file);
    $im = imagecreatetruecolor($size+1, $size);
    $im = imagecreatetruecolor($size + 1, $size);
    imagefilter($im, IMG_FILTER_GRAYSCALE);
    switch($t) {
    case 1:
    @@ -17,13 +17,13 @@ function dhash_alt($file, $size = 8)
    $oi = imagecreatefrompng($file);
    break;
    }
    imagecopyresampled($im, $oi, 0, 0, 0, 0, $size+1, $size, $w, $h);
    imagecopyresampled($im, $oi, 0, 0, 0, 0, $size + 1, $size, $w, $h);
    imagedestroy($oi);
    for ($y = 0; $y < $size; $y++) {
    $val = '';
    for ($x = 0; $x < $size; $x++) {
    $curr = imagecolorat($im, $x, $y);
    $next = imagecolorat($im, $x+1, $y);
    $next = imagecolorat($im, $x + 1, $y);
    $val .= ($curr > $next) ? 1 : 0;
    }
    $hash .= str_pad(dechex(bindec($val)), 2, 0, STR_PAD_LEFT);
  4. jamiebicknell revised this gist Apr 3, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion dash.php
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ function dhash($file, $size = 8)
    for ($x = 0; $x < $size; $x++) {
    $curr = imagecolorat($im, $x, $y);
    $next = imagecolorat($im, $x+1, $y);
    $val += ($curr > $next) ? pow(2, ($x+($y*$size))%8) : 0;
    $val += ($curr > $next) ? pow(2, ($x + ($y * $size)) % 8) : 0;
    }
    $hash .= str_pad(dechex($val), 2, 0, STR_PAD_LEFT);
    }
  5. jamiebicknell revised this gist Apr 3, 2014. 2 changed files with 5 additions and 5 deletions.
    4 changes: 2 additions & 2 deletions dash.php
    Original file line number Diff line number Diff line change
    @@ -19,9 +19,9 @@ function dhash($file, $size = 8)
    }
    imagecopyresampled($im, $oi, 0, 0, 0, 0, $size+1, $size, $w, $h);
    imagedestroy($oi);
    for ($y=0; $y<$size; $y++) {
    for ($y = 0; $y < $size; $y++) {
    $val = 0;
    for ($x=0; $x<$size; $x++) {
    for ($x = 0; $x < $size; $x++) {
    $curr = imagecolorat($im, $x, $y);
    $next = imagecolorat($im, $x+1, $y);
    $val += ($curr > $next) ? pow(2, ($x+($y*$size))%8) : 0;
    6 changes: 3 additions & 3 deletions dash_alt.php
    Original file line number Diff line number Diff line change
    @@ -19,9 +19,9 @@ function dhash_alt($file, $size = 8)
    }
    imagecopyresampled($im, $oi, 0, 0, 0, 0, $size+1, $size, $w, $h);
    imagedestroy($oi);
    for ($y=0; $y<$size; $y++) {
    $val = 0;
    for ($x=0; $x<$size; $x++) {
    for ($y = 0; $y < $size; $y++) {
    $val = '';
    for ($x = 0; $x < $size; $x++) {
    $curr = imagecolorat($im, $x, $y);
    $next = imagecolorat($im, $x+1, $y);
    $val .= ($curr > $next) ? 1 : 0;
  6. jamiebicknell revised this gist Apr 3, 2014. 2 changed files with 24 additions and 23 deletions.
    23 changes: 12 additions & 11 deletions dash.php
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,11 @@
    <?php

    function dhash($file, $size = 8) {
    function dhash($file, $size = 8)
    {
    $hash = '';
    list($w,$h,$t) = getimagesize($file);
    $im = imagecreatetruecolor($size+1,$size);
    imagefilter($im,IMG_FILTER_GRAYSCALE);
    list($w, $h, $t) = getimagesize($file);
    $im = imagecreatetruecolor($size+1, $size);
    imagefilter($im, IMG_FILTER_GRAYSCALE);
    switch($t) {
    case 1:
    $oi = imagecreatefromgif($file);
    @@ -16,16 +17,16 @@ function dhash($file, $size = 8) {
    $oi = imagecreatefrompng($file);
    break;
    }
    imagecopyresampled($im,$oi,0,0,0,0,$size+1,$size,$w,$h);
    imagecopyresampled($im, $oi, 0, 0, 0, 0, $size+1, $size, $w, $h);
    imagedestroy($oi);
    for($y=0;$y<$size;$y++) {
    for ($y=0; $y<$size; $y++) {
    $val = 0;
    for($x=0;$x<$size;$x++) {
    $curr = imagecolorat($im,$x,$y);
    $next = imagecolorat($im,$x+1,$y);
    $val += ($curr > $next) ? pow(2,($x+($y*$size))%8) : 0;
    for ($x=0; $x<$size; $x++) {
    $curr = imagecolorat($im, $x, $y);
    $next = imagecolorat($im, $x+1, $y);
    $val += ($curr > $next) ? pow(2, ($x+($y*$size))%8) : 0;
    }
    $hash .= str_pad(dechex($val),2,0,STR_PAD_LEFT);
    $hash .= str_pad(dechex($val), 2, 0, STR_PAD_LEFT);
    }
    imagedestroy($im);
    return $hash;
    24 changes: 12 additions & 12 deletions dash_alt.php
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    <?php

    function dhash_alt($file) {
    $size = 8; // Needs to be 8 for straight bin -> dec -> hex conversion
    function dhash_alt($file, $size = 8)
    {
    $hash = '';
    list($w,$h,$t) = getimagesize($file);
    $im = imagecreatetruecolor($size+1,$size);
    imagefilter($im,IMG_FILTER_GRAYSCALE);
    list($w, $h, $t) = getimagesize($file);
    $im = imagecreatetruecolor($size+1, $size);
    imagefilter($im, IMG_FILTER_GRAYSCALE);
    switch($t) {
    case 1:
    $oi = imagecreatefromgif($file);
    @@ -17,16 +17,16 @@ function dhash_alt($file) {
    $oi = imagecreatefrompng($file);
    break;
    }
    imagecopyresampled($im,$oi,0,0,0,0,$size+1,$size,$w,$h);
    imagecopyresampled($im, $oi, 0, 0, 0, 0, $size+1, $size, $w, $h);
    imagedestroy($oi);
    for($y=0;$y<$size;$y++) {
    $val = '';
    for($x=0;$x<$size;$x++) {
    $curr = imagecolorat($im,$x,$y);
    $next = imagecolorat($im,$x+1,$y);
    for ($y=0; $y<$size; $y++) {
    $val = 0;
    for ($x=0; $x<$size; $x++) {
    $curr = imagecolorat($im, $x, $y);
    $next = imagecolorat($im, $x+1, $y);
    $val .= ($curr > $next) ? 1 : 0;
    }
    $hash .= str_pad(dechex(bindec($val)),2,0,STR_PAD_LEFT);
    $hash .= str_pad(dechex(bindec($val)), 2, 0, STR_PAD_LEFT);
    }
    imagedestroy($im);
    return $hash;
  7. jamiebicknell revised this gist Apr 3, 2014. 2 changed files with 2 additions and 0 deletions.
    1 change: 1 addition & 0 deletions dash.php
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@ function dhash($file, $size = 8) {
    $hash = '';
    list($w,$h,$t) = getimagesize($file);
    $im = imagecreatetruecolor($size+1,$size);
    imagefilter($im,IMG_FILTER_GRAYSCALE);
    switch($t) {
    case 1:
    $oi = imagecreatefromgif($file);
    1 change: 1 addition & 0 deletions dash_alt.php
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@ function dhash_alt($file) {
    $hash = '';
    list($w,$h,$t) = getimagesize($file);
    $im = imagecreatetruecolor($size+1,$size);
    imagefilter($im,IMG_FILTER_GRAYSCALE);
    switch($t) {
    case 1:
    $oi = imagecreatefromgif($file);
  8. jamiebicknell revised this gist Apr 3, 2014. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  9. jamiebicknell revised this gist Apr 3, 2014. 2 changed files with 2 additions and 6 deletions.
    4 changes: 1 addition & 3 deletions Alternate Hash → Alternate.php
    Original file line number Diff line number Diff line change
    @@ -29,6 +29,4 @@ function dhash_alt($file) {
    }
    imagedestroy($im);
    return $hash;
    }

    ?>
    }
    4 changes: 1 addition & 3 deletions Original → Original.php
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,4 @@ function dhash($file, $size = 8) {
    }
    imagedestroy($im);
    return $hash;
    }

    ?>
    }
  10. jamiebicknell revised this gist Apr 3, 2014. 2 changed files with 6 additions and 2 deletions.
    4 changes: 3 additions & 1 deletion Alternate Hash
    Original file line number Diff line number Diff line change
    @@ -29,4 +29,6 @@ function dhash_alt($file) {
    }
    imagedestroy($im);
    return $hash;
    }
    }

    ?>
    4 changes: 3 additions & 1 deletion Original
    Original file line number Diff line number Diff line change
    @@ -28,4 +28,6 @@ function dhash($file, $size = 8) {
    }
    imagedestroy($im);
    return $hash;
    }
    }

    ?>
  11. jamiebicknell renamed this gist Apr 3, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  12. jamiebicknell created this gist Apr 3, 2014.
    32 changes: 32 additions & 0 deletions Alternate Hash
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    <?php

    function dhash_alt($file) {
    $size = 8; // Needs to be 8 for straight bin -> dec -> hex conversion
    $hash = '';
    list($w,$h,$t) = getimagesize($file);
    $im = imagecreatetruecolor($size+1,$size);
    switch($t) {
    case 1:
    $oi = imagecreatefromgif($file);
    break;
    case 2:
    $oi = imagecreatefromjpeg($file);
    break;
    case 3:
    $oi = imagecreatefrompng($file);
    break;
    }
    imagecopyresampled($im,$oi,0,0,0,0,$size+1,$size,$w,$h);
    imagedestroy($oi);
    for($y=0;$y<$size;$y++) {
    $val = '';
    for($x=0;$x<$size;$x++) {
    $curr = imagecolorat($im,$x,$y);
    $next = imagecolorat($im,$x+1,$y);
    $val .= ($curr > $next) ? 1 : 0;
    }
    $hash .= str_pad(dechex(bindec($val)),2,0,STR_PAD_LEFT);
    }
    imagedestroy($im);
    return $hash;
    }
    31 changes: 31 additions & 0 deletions PHP Port
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    <?php

    function dhash($file, $size = 8) {
    $hash = '';
    list($w,$h,$t) = getimagesize($file);
    $im = imagecreatetruecolor($size+1,$size);
    switch($t) {
    case 1:
    $oi = imagecreatefromgif($file);
    break;
    case 2:
    $oi = imagecreatefromjpeg($file);
    break;
    case 3:
    $oi = imagecreatefrompng($file);
    break;
    }
    imagecopyresampled($im,$oi,0,0,0,0,$size+1,$size,$w,$h);
    imagedestroy($oi);
    for($y=0;$y<$size;$y++) {
    $val = 0;
    for($x=0;$x<$size;$x++) {
    $curr = imagecolorat($im,$x,$y);
    $next = imagecolorat($im,$x+1,$y);
    $val += ($curr > $next) ? pow(2,($x+($y*$size))%8) : 0;
    }
    $hash .= str_pad(dechex($val),2,0,STR_PAD_LEFT);
    }
    imagedestroy($im);
    return $hash;
    }