Skip to content

Commit

Permalink
Stream image to temp file in /tmp directory [IMG-61]
Browse files Browse the repository at this point in the history
  • Loading branch information
IllyaMoskvin committed Apr 28, 2022
1 parent 2dca4b5 commit 035d839
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions app/Http/Controllers/ImageSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ public function imageSearch(Request $request)
);
}

$diskPath = 'tmp/' . uniqid() . '.jpg';
$fullPath = storage_path('app/' . $diskPath);

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

$image = Image::make($request->file);
$image->save($fullPath);
$stream = $image->stream('jpg');

$tempFile = tmpfile();
stream_copy_to_stream($stream->detach(), $tempFile);

$tempPath = stream_get_meta_data($tempFile)['uri'];

// TODO: Error out if the file is not an image
$shell = new Shell([
Expand All @@ -39,10 +41,10 @@ public function imageSearch(Request $request)
$result = $shell->exec(
'python3 %s %s',
base_path('bin/ahash.py'),
$fullPath,
$tempPath,
);

Storage::delete($diskPath);
fclose($tempFile);

$hash = array_pop($result['output']);
$hashBinaries = hexToBoolArray($hash, 64);
Expand Down

0 comments on commit 035d839

Please sign in to comment.