Skip to content

Commit

Permalink
Edit controller to search for phashes [IMG-61]
Browse files Browse the repository at this point in the history
  • Loading branch information
nadineas committed Apr 29, 2022
1 parent 035d839 commit 75ffc4c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
20 changes: 18 additions & 2 deletions app/Http/Controllers/ImageSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ public function imageSearch(Request $request)
);
}

$supportedHashes = ['ahash', 'phash'];

if (!empty($request->hash_type)) {
if (!in_array($request->hash_type, $supportedHashes)){
throw new DetailedException(
'Hash type not found',
'Only ahash and phash hash types are allowed',
400,
);
}

$hashType = $request->hash_type;
} else {
$hashType = 'ahash';
}

Image::configure(['driver' => 'imagick']);

$image = Image::make($request->file);
Expand All @@ -40,7 +56,7 @@ public function imageSearch(Request $request)

$result = $shell->exec(
'python3 %s %s',
base_path('bin/ahash.py'),
base_path('bin/' . $hashType . '.py'),
$tempPath,
);

Expand All @@ -53,7 +69,7 @@ public function imageSearch(Request $request)
->range(0, 63)
->map(fn ($i) => [
'term' => [
'ahash.hash_' . $i => $hashBinaries[$i]
$hashType . '.hash_' . $i => $hashBinaries[$i]
],
])
->all();
Expand Down
2 changes: 1 addition & 1 deletion app/Transformers/Outbound/Collections/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected function getAssetFields()
return array_merge(
$imageFields,
$this->getHashField('ahash'),
// $this->getHashField('phash'),
$this->getHashField('phash'),
// $this->getHashField('whash'),
// $this->getHashField('dhash'),
);
Expand Down
5 changes: 5 additions & 0 deletions bin/phash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import imagehash
import sys
from PIL import Image

print(str(imagehash.phash(Image.open(sys.argv[1]))))

0 comments on commit 75ffc4c

Please sign in to comment.