1. Go to this page and download the library: Download gowelle/google-moderator library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gowelle / google-moderator example snippets
// config/google-moderator.php
'auth' => [
// Option 1: Path to service account JSON file
'credentials_path' => env('GOOGLE_APPLICATION_CREDENTIALS'),
// Option 2: Inline JSON (for serverless environments like Vapor)
'credentials_json' => env('GOOGLE_CREDENTIALS_JSON'),
// Option 3: Project ID for Application Default Credentials
'project_id' => env('GOOGLE_CLOUD_PROJECT'),
],
'engines' => [
'text' => 'natural_language', // or 'gemini'
'image' => 'vision', // or 'gemini'
],
use Gowelle\GoogleModerator\Facades\Moderation;
$result = Moderation::text(
text: 'This is some content to moderate',
language: 'en'
);
if ($result->isSafe()) {
// Content is safe
} else {
// Content was flagged
foreach ($result->flags() as $flag) {
echo "{$flag->category}: {$flag->severity}";
}
}
use Gowelle\GoogleModerator\Facades\Moderation;
// From file path
$result = Moderation::image('/path/to/image.jpg');
// From URL
$result = Moderation::image('https://example.com/image.jpg');
if ($result->isUnsafe()) {
// Handle unsafe image
}
use Gowelle\GoogleModerator\Rules\ModeratedText;
$request->validate([
'bio' => ['xt('Please be polite.')],
]);
use Gowelle\GoogleModerator\Rules\ModeratedImage;
$request->validate([
'avatar' => ['
$result = Moderation::text($content, 'sw');
// Safety checks
$result->isSafe(); // bool
$result->isUnsafe(); // bool
$result->confidence(); // float|null
// Flag access
$result->flags(); // array<FlaggedTerm>
$result->apiFlags(); // Flags from Google API only
$result->blocklistFlags(); // Flags from blocklist only
$result->highSeverityFlags(); // High severity flags only
$result->hasHighSeverityFlags(); // bool
// Metadata
$result->provider(); // 'google' or 'blocklist'
$result->engine(); // 'natural_language', 'vision', 'gemini'
// Grouping
$result->flagsByCategory(); // array<string, array<FlaggedTerm>>
// Serialization
$result->toArray();
json_encode($result);
'blocklists' => [
'enabled' => true,
'storage' => 'database', // or 'file'
'languages' => ['en', 'sw', 'fr'], // any languages
],
use Gowelle\GoogleModerator\Events\ContentFlagged;
use Illuminate\Support\Facades\Event;
// In your EventServiceProvider or listener
Event::listen(ContentFlagged::class, function (ContentFlagged $event) {
Log::warning('Unsafe content detected', [
'type' => $event->type, // 'text' or 'image'
'categories' => $event->categories(),
'is_high_severity' => $event->isHighSeverity(),
'flags' => $event->result->flags(),
]);
// Take action: notify moderators, block submission, etc.
});
$event->result; // ModerationResult DTO
$event->type; // 'text' or 'image'
$event->content; // Original text or image path
$event->language; // Language code (for text)
$event->metadata; // Additional context
// Helper methods
$event->isText(); // bool
$event->isImage(); // bool
$event->categories(); // array of flagged categories
$event->isHighSeverity(); // bool
use Gowelle\GoogleModerator\Facades\Moderation;
use Gowelle\GoogleModerator\DTOs\ModerationResult;
Moderation::shouldReceive('text')
->with('test content', 'en')
->andReturn(ModerationResult::safe('google', 'natural_language'));