clickandmortar/rekognition-php
Simple PHP library for AWS Rekognition. Detect labels (objects, scenes, concepts) and text in JPEG/PNG images. Works with image URLs, raw file bytes, or base64, returning easy-to-filter results by confidence.
Installation
composer require clickandmortar/rekognition-php
Ensure your AWS credentials are configured via:
AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION)~/.aws/credentials file.First Use Case: Detect Labels in an Image
use ClickAndMortar\Rekognition\Rekognition;
$rekognition = new Rekognition();
$result = $rekognition->detectLabels('path/to/image.jpg');
print_r($result);
Where to Look First
src/ClickAndMortar/Rekognition/Rekognition.php (core class methods).tests/ directory (for usage examples and edge cases).Image Analysis Pipeline
$rekognition = new Rekognition();
$labels = $rekognition->detectLabels($imagePath);
$faces = $rekognition->detectFaces($imagePath);
$text = $rekognition->detectText($imagePath);
// Filter labels by confidence threshold
$filteredLabels = array_filter($labels, fn($label) => $label['Confidence'] > 80);
Batch Processing
$rekognition = new Rekognition();
$images = ['image1.jpg', 'image2.jpg', 'image3.jpg'];
$results = [];
foreach ($images as $image) {
$results[$image] = $rekognition->detectLabels($image);
}
Integration with Laravel
$rekognition = new Rekognition();
$labels = $rekognition->detectLabels($request->file('image')->path());
Analysis::create([
'user_id' => auth()->id(),
'labels' => json_encode($labels),
]);
public function handle($request, Closure $next) {
$response = $rekognition->detectLabels($request->image);
if (empty($response)) {
abort(400, 'No labels detected.');
}
return $next($request);
}
Async Processing with Queues
// Dispatch a job
DetectLabelsJob::dispatch($imagePath, $userId);
// Job class
public function handle() {
$rekognition = new Rekognition();
$labels = $rekognition->detectLabels(storage_path($this->imagePath));
Analysis::create([...]);
}
AWS Credentials
rekognition:Detect* permissions.AccessDeniedException → Verify IAM policies and credentials.Image Format/Size
DetectLabels, 5MB for DetectText).InvalidS3ObjectException → Check file size/type before upload.Rate Limits
ThrottlingException).Deprecated Methods
Enable AWS SDK Debugging
use Aws\Common\Aws;
Aws::setDebug(true); // Logs requests/responses to `storage/logs/aws.log`
Validate Responses
$result = $rekognition->detectLabels($imagePath);
if (!isset($result['Labels'])) {
throw new \RuntimeException('Invalid Rekognition response');
}
Handle Exceptions
try {
$result = $rekognition->detectLabels($imagePath);
} catch (\Aws\Rekognition\Exception\RekognitionException $e) {
Log::error('Rekognition Error: ' . $e->getMessage());
abort(500, 'Image analysis failed.');
}
Custom Response Mapping
$rekognition = new Rekognition();
$rawResult = $rekognition->detectLabels($imagePath);
// Transform raw AWS response
$formatted = array_map(function($label) {
return [
'name' => $label['Name'],
'confidence' => $label['Confidence'] / 100, // Convert to decimal
'parents' => $label['Parents'] ?? [],
];
}, $rawResult['Labels']);
Add New Features
Rekognition class to support unsupported APIs (e.g., CompareFaces):
class ExtendedRekognition extends Rekognition {
public function compareFaces($sourceImage, $targetImage) {
return $this->client->compareFaces([
'SourceImage' => ['Bytes' => file_get_contents($sourceImage)],
'TargetImage' => ['Bytes' => file_get_contents($targetImage)],
]);
}
}
Mocking for Tests
$rekognition = Mockery::mock(ClickAndMortar\Rekognition\Rekognition::class);
$rekognition->shouldReceive('detectLabels')
->once()
->andReturn(['Labels' => [['Name' => 'Car', 'Confidence' => 99]]]);
How can I help you explore Laravel packages today?