Skip to content

Commit

Permalink
add api for comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzyfhuba committed Jul 19, 2023
1 parent 6117b68 commit 1200095
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 30 deletions.
87 changes: 87 additions & 0 deletions app/Http/Controllers/api/CompareController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Models\Member;
use Illuminate\Http\Request;
use Validator;

class CompareController extends MemberController
{
/**
* Compare two images, upload both.
*
* @OA\Post(
* path="/compare",
* tags={"compare"},
* operationId="Compare.compare",
* @OA\RequestBody(
* required=true,
* @OA\MediaType(
* mediaType="multipart/form-data",
* @OA\Schema(
* type="object",
* @OA\Property(
* property="image1",
* description="First image",
* type="string",
* format="binary"
* ),
* @OA\Property(
* property="image2",
* description="Second image",
* type="string",
* format="binary"
* )
* )
* )
* ),
* @OA\Response(
* response=400,
* description="Invalid input"
* ),
* @OA\Response(
* response=200,
* description="Success"
* ),
* )
*/
public function compare(Request $request) {
$validator = Validator::make($request->all(), [
'image1' => 'required|mimes:png|max:2048',
'image2' => 'required|mimes:png|max:2048',
]);

if ($validator->fails()) {
return response($validator->getMessageBag(), 400);
}

[
$a_colors_r,
$a_colors_g,
$a_colors_b,
$a_width,
$a_height,
] = $this->get_rgb($request->file('image2')->getPathname());
// ] = $this->get_rgb("$this->directory_target/DSC08729.jpg");
// dd($request->file('image1'));
[
$b_colors_r,
$b_colors_g,
$b_colors_b,
$b_width,
$b_height,
] = $this->get_rgb($request->file('image1')->getPathname());

// START MAGNITUDE
$r_magnitude = $this->get_magnitude($a_colors_r, $b_colors_r);
$g_magnitude = $this->get_magnitude($a_colors_g, $b_colors_g);
$b_magnitude = $this->get_magnitude($a_colors_b, $b_colors_b);

// RESULT
$result = array_sum([$r_magnitude, $g_magnitude, $b_magnitude]) / 3;

return response($result);
}
}
16 changes: 0 additions & 16 deletions app/Http/Controllers/api/MemberController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,6 @@ function __construct()
* Display a listing of the resource.
*/

/**
* Add a new pet to the store.
*
* @OA\Post(
* path="/pet",
* tags={"pet"},
* operationId="addPet",
* @OA\Response(
* response=405,
* description="Invalid input"
* ),
* security={
* {"petstore_auth": {"write:pets", "read:pets"}}
* },
* )
*/
public function index()
{
$members = Member::orderBy('updated_at', 'desc')->get();
Expand Down
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@
Route::delete('/members/{id}', [\App\Http\Controllers\api\MemberController::class, 'destroy']);
Route::get('/members/image/{image}', [\App\Http\Controllers\api\MemberController::class, 'kyc_image']);
Route::post('/members/compare_similarity', [\App\Http\Controllers\api\MemberController::class, 'compare_similarity']);

Route::post('/compare', [\App\Http\Controllers\api\CompareController::class, 'compare']);
45 changes: 31 additions & 14 deletions storage/api-docs/api-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,43 @@
}
],
"paths": {
"/pet": {
"/compare": {
"post": {
"tags": [
"pet"
"compare"
],
"summary": "Add a new pet to the store.",
"operationId": "addPet",
"responses": {
"405": {
"description": "Invalid input"
"summary": "Compare two images, upload both.",
"operationId": "Compare.compare",
"requestBody": {
"required": true,
"content": {
"multipart/form-data": {
"schema": {
"properties": {
"image1": {
"description": "First image",
"type": "string",
"format": "binary"
},
"image2": {
"description": "Second image",
"type": "string",
"format": "binary"
}
},
"type": "object"
}
}
}
},
"security": [
{
"petstore_auth": [
"write:pets",
"read:pets"
]
"responses": {
"400": {
"description": "Invalid input"
},
"200": {
"description": "Success"
}
]
}
}
}
}
Expand Down

0 comments on commit 1200095

Please sign in to comment.