Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,19 @@ class Image
/** @var string */
private $binaryContent;

/** @var string */
private $urlContent;

/** @var Label[] */
private $labels = [];

/** @var Text[] */
private $texts = [];

/**
* @param string $binaryContent
*/
public function __construct(string $binaryContent)
{
$this->setBinaryContent($binaryContent);
}

/**
* @return string
*/
public function getBinaryContent(): string
public function getBinaryContent(): ?string
{
return $this->binaryContent;
}
Expand All @@ -42,6 +37,16 @@ public function setBinaryContent(string $binaryContent): void
$this->binaryContent = $binaryContent;
}

public function getUrlContent(): ?string
{
return $this->urlContent;
}

public function setUrlContent(string $urlContent): void
{
$this->urlContent = $urlContent;
}

/**
* @param int $minimumConfidence
* @return Label[]
Expand Down
72 changes: 68 additions & 4 deletions src/Service/DetectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ class DetectService
// From https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-rekognition-2016-06-27.html
const AWS_REKOGNITION_VERSION = '2016-06-27';

/**
* Detect both text and labels
* @var string
*/
const DETECT_BOTH = 'detect_both';

/**
* Detect text only
* @var string
*/
const DETECT_TEXT = 'detect_text';

/**
* Detect labels only
* @var string
*/
const DETECT_LABEL = 'detect_label';

/** @var string */
protected $detectMode;

/** @var RekognitionClient */
protected $rekognitionClient;

Expand All @@ -34,23 +55,46 @@ public function __construct(array $options = [])
{
$options = $this->buildOptions($options);

$this->detectMode = self::DETECT_BOTH;

$this->rekognitionClient = new RekognitionClient($options);
$this->setLabelService(new LabelService($this->rekognitionClient));
$this->setTextService(new TextService($this->rekognitionClient));
}

/**
* Set detection mode, default is to detect both
* labels and text.
*
* @param string $mode
*/
public function setDetectMode($mode): void
{
$this->detectMode = $mode;
}

/**
* @param string $binaryContent
* @return Image
*/
public function detect(string $binaryContent): Image
{
$rekognitionImage = new Image($binaryContent);
$rekognitionImage = new Image;
$rekognitionImage->setBinaryContent($binaryContent);

$this->labelService->detectLabels($rekognitionImage);
$this->textService->detectText($rekognitionImage);
return $this->handleDetection($rekognitionImage);
}

/**
* @param string $s3Url
* @return Image
*/
public function detectFromS3(string $s3Url): Image
{
$rekognitionImage = new Image;
$rekognitionImage->setUrlContent($s3Url);

return $rekognitionImage;
return $this->handleDetection($rekognitionImage);
}

/**
Expand All @@ -60,6 +104,9 @@ public function detect(string $binaryContent): Image
*/
public function detectFromUrl(string $url): Image
{
if (strpos($url, 's3.amazonaws.com') !== false) {
return $this->detectFromS3($url);
}
$binaryContent = @file_get_contents($url);

if ($binaryContent === false) {
Expand Down Expand Up @@ -146,4 +193,21 @@ protected function getEnvironmentOptions(): array

return $environmentOptions;
}

/**
* Run detection, respecting mode
* @param Image $image
* @return Image
*/
private function handleDetection(Image $image): Image
{
if ($this->detectMode == self::DETECT_BOTH || $this->detectMode == self::DETECT_LABEL) {
$this->labelService->detectLabels($image);
}
if ($this->detectMode == self::DETECT_BOTH || $this->detectMode == self::DETECT_TEXT) {
$this->textService->detectText($image);
}

return $image;
}
}
25 changes: 24 additions & 1 deletion src/Service/LabelService.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,33 @@ public function detectLabels(Image $rekognitionImage): void
*/
protected function getResult(Image $rekognitionImage)
{
if ($rekognitionImage->getBinaryContent()) {
return $this->rekognitionClient->detectLabels(
[
'Image' => [
'Bytes' => $rekognitionImage->getBinaryContent(),
],
'Attributes' => ['ALL']
]
);
}

$url = $rekognitionImage->getUrlContent();
$parts = explode('s3.amazonaws.com', $url);
if (!count($parts)) {
throw new \Exception('Unable to parse S3 url');
}

$bucket = trim(substr($parts[1], 0, strpos($parts[1], '/', 1)), '/');
$name = substr($parts[1], (strpos($parts[1], '/', 1) + 1));

return $this->rekognitionClient->detectLabels(
[
'Image' => [
'Bytes' => $rekognitionImage->getBinaryContent(),
'S3Object' => [
'Bucket' => $bucket,
'Name' => $name
]
],
'Attributes' => ['ALL']
]
Expand Down
27 changes: 25 additions & 2 deletions src/Service/TextService.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,33 @@ public function detectText(Image $rekognitionImage): void
*/
protected function getResult(Image $rekognitionImage)
{
return $this->rekognitionClient->detectText(
if ($rekognitionImage->getBinaryContent()) {
return $this->rekognitionClient->detectText(
[
'Image' => [
'Bytes' => $rekognitionImage->getBinaryContent(),
],
'Attributes' => ['ALL']
]
);
}

$url = $rekognitionImage->getUrlContent();
$parts = explode('s3.amazonaws.com', $url);
if (!count($parts)) {
throw new \Exception('Unable to parse S3 url');
}

$bucket = trim(substr($parts[1], 0, strpos($parts[1], '/', 1)), '/');
$name = substr($parts[1], (strpos($parts[1], '/', 1) + 1));

return $this->rekognitionClient->detectLabels(
[
'Image' => [
'Bytes' => $rekognitionImage->getBinaryContent(),
'S3Object' => [
'Bucket' => $bucket,
'Name' => $name
]
],
'Attributes' => ['ALL']
]
Expand Down