· 7 years ago · Jun 02, 2018, 09:04 PM
1<?php
2/**
3 * Created by PhpStorm.
4 * User: peterjaap
5 * Date: 2-6-18
6 * Time: 13:51
7 */
8
9namespace App\Model;
10
11
12use BotMan\BotMan\Messages\Attachments\Image;
13use GuzzleHttp\Client as GuzzleClient;
14
15class OpenALPR
16{
17 const COUNTRY_CODE_EU = 'eu';
18 protected $endpoint;
19 protected $parameters;
20 protected $client;
21
22 /**
23 * OpenALPR constructor.
24 */
25 public function __construct()
26 {
27 $this->endpoint = 'https://api.openalpr.com/v2/recognize';
28 $this->parameters = [
29 'recognize_vehicle' => 1,
30 'country' => self::COUNTRY_CODE_EU,
31 'secret_key' => env('OPENALPR_SECRET')
32 ];
33
34 $this->client = new GuzzleClient();
35 }
36
37 /**
38 * @param $image
39 * @return array|mixed
40 */
41 public function getPlateInfo($image)
42 {
43
44 if (is_string($image)) {
45 $imageUrl = $image;
46 } elseif (is_object($image) && $image instanceof Image) {
47 $imageUrl = $image->getUrl();
48 } else {
49 return [];
50 }
51
52 $tempImageUrl = public_path() . '/temp_'. date('U') . mt_rand(0,1000). '.jpg';
53 file_put_contents($tempImageUrl, file_get_contents($imageUrl));
54
55 $url = $this->endpoint . '?' . http_build_query($this->parameters);
56
57 $response = $this->client->request('post', $url, [
58 'multipart' => [
59 [
60 'name' => 'image',
61 'contents' => fopen($tempImageUrl, 'r')
62 ]
63 ],
64 ]);
65
66 unlink($tempImageUrl);
67
68 return json_decode($response->getBody()->getContents(), true);
69 }
70}