Passed
Push — master ( dec909...4437a6 )
by Chris
10:44
created

CanRecognizeImages::blob()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Meema\MediaRecognition\Traits;
4
5
use Exception;
6
use Meema\MediaRecognition\Models\MediaRecognition;
7
8
trait CanRecognizeImages
9
{
10
    /**
11
     * The input image as base64-encoded bytes.
12
     *
13
     * @var string|null
14
     */
15
    protected ?string $blob = null;
16
17
    /**
18
     * Set the base64 encoded image.
19
     *
20
     * @param string $blob
21
     *
22
     * @return $this
23
     */
24
    public function blob(string $blob)
25
    {
26
        $this->blob = $blob;
27
28
        return $this;
29
    }
30
31
    /**
32
     * Sets the image to be analyzed.
33
     *
34
     * @return void
35
     * @throws \Exception
36
     */
37
    protected function setImageSettings(): void
38
    {
39
        if (is_string($this->blob)) {
40
            $this->settings['Image'] = [
0 ignored issues
show
Bug Best Practice introduced by
The property settings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
41
                'Bytes' => $this->blob,
42
            ];
43
44
            return;
45
        }
46
47
        $disk = $this->disk ?? config('media-recognition.disk');
48
        $bucketName = config("filesystems.disks.$disk.bucket");
49
50
        if (! $bucketName) {
51
            throw new Exception('Please make sure to set a S3 bucket name.');
52
        }
53
54
        $this->settings['Image'] = [
55
            'S3Object' => [
56
                'Bucket' => $bucketName,
57
                'Name' => $this->path,
58
            ],
59
        ];
60
    }
61
62
    /**
63
     * Detects labels/objects in an image.
64
     *
65
     * @param int|null $mediaId
66
     * @param int|null $minConfidence
67
     * @param int|null $maxLabels
68
     * @return \Aws\Result
69
     * @throws \Exception
70
     */
71
    public function detectLabels($mediaId = null, $minConfidence = null, $maxLabels = null)
72
    {
73
        $this->setImageSettings();
74
75
        $this->settings['MinConfidence'] = $minConfidence ?? config('media-recognition.min_confidence');
0 ignored issues
show
Bug Best Practice introduced by
The property settings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
76
77
        if (is_int($maxLabels)) {
78
            $this->settings['MaxLabels'] = $maxLabels;
79
        }
80
81
        $results = $this->client->detectLabels($this->settings);
82
83
        if (! config('media-recognition.track_media_recognitions')) {
84
            return $results;
85
        }
86
87
        if (is_null($mediaId)) {
88
            throw new Exception('Please make sure to set a $mediaId.');
89
        }
90
91
        MediaRecognition::updateOrCreate([
92
            'model_id' => $mediaId,
93
            'model_type' => config('media-converter.media_model'),
94
        ], ['labels' => $results->toArray()]);
95
96
        return $results;
97
    }
98
99
    /**
100
     * Detects faces in an image & analyzes them.
101
     *
102
     * @param int|null $mediaId
103
     * @param array $attributes
104
     * @return \Aws\Result
105
     * @throws \Exception
106
     */
107
    public function detectFaces($mediaId = null, $attributes = ['DEFAULT'])
108
    {
109
        $this->setImageSettings();
110
111
        $this->settings['Attributes'] = $attributes;
0 ignored issues
show
Bug Best Practice introduced by
The property settings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
112
113
        $results = $this->client->detectFaces($this->settings);
114
115
        $this->updateOrCreate('faces', $mediaId, $results);
0 ignored issues
show
Bug introduced by
It seems like updateOrCreate() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
        $this->/** @scrutinizer ignore-call */ 
116
               updateOrCreate('faces', $mediaId, $results);
Loading history...
116
117
        return $results;
118
    }
119
120
    /**
121
     * Detects moderation labels in an image.
122
     * This can be useful for children-friendly images or NSFW images.
123
     *
124
     * @param int|null $mediaId
125
     * @param int|null $minConfidence
126
     * @return \Aws\Result
127
     * @throws \Exception
128
     */
129
    public function detectModeration($mediaId = null, $minConfidence = null)
130
    {
131
        $this->setImageSettings();
132
133
        $this->settings['MinConfidence'] = $minConfidence ?? config('media-recognition.min_confidence');
0 ignored issues
show
Bug Best Practice introduced by
The property settings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
134
135
        $results = $this->client->detectModerationLabels($this->settings);
136
137
        $this->updateOrCreate('moderation', $mediaId, $results);
138
139
        return $results;
140
    }
141
142
    /**
143
     * Detects text in an image (OCR).
144
     *
145
     * @param int|null $mediaId
146
     * @param int|null $minConfidence
147
     * @return \Aws\Result
148
     * @throws \Exception
149
     */
150
    public function detectText($mediaId = null, $minConfidence = null)
0 ignored issues
show
Unused Code introduced by
The parameter $minConfidence is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

150
    public function detectText($mediaId = null, /** @scrutinizer ignore-unused */ $minConfidence = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
151
    {
152
        $this->setImageSettings();
153
154
        $results = $this->client->detectText($this->settings);
155
156
        $this->updateOrCreate('ocr', $mediaId, $results);
157
158
        return $results;
159
    }
160
}
161