88 lines
2.7 KiB
PHP
88 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands\Bundle;
|
|
|
|
use App\Classes\AttachmentsManager;
|
|
use App\Classes\Bundle;
|
|
use App\Console\Commands\Bundle\Traits\ReadsBundles;
|
|
use App\Console\Commands\Bundle\Traits\SelectsDisks;
|
|
use App\Services\Ollama;
|
|
use App\Services\Translator;
|
|
use Illuminate\Console\Command;
|
|
use Intervention\Image\Laravel\Facades\Image;
|
|
|
|
use function Laravel\Prompts\confirm;
|
|
use function Laravel\Prompts\progress;
|
|
use function Laravel\Prompts\textarea;
|
|
|
|
class DescribeAttachments extends Command
|
|
{
|
|
use ReadsBundles;
|
|
use SelectsDisks;
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Add description to attachments';
|
|
|
|
public function __construct()
|
|
{
|
|
$this->signature = 'bundle:describe-attachments
|
|
{ --r|recursive : Also upgrade sub-bundles }
|
|
{ --source-disk= : Use specified content disk - Defaults to <info>' . env('CONTENT_DISK') . '</info> }
|
|
{ path? : Path to a specific bundle to upgrade - Default to <info>/</info> }
|
|
';
|
|
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
$this->selectDisk()
|
|
->selectBundles()
|
|
->perform();
|
|
}
|
|
|
|
private function perform()
|
|
{
|
|
progress('Updating bundles...', $this->bundles, function (Bundle $bundle, $progress) {
|
|
$this->handleBundle($bundle, $progress);
|
|
});
|
|
}
|
|
|
|
private function handleBundle(Bundle $bundle, $progress)
|
|
{
|
|
$attachmentsManager = $bundle->attachments(AttachmentsManager::Images);
|
|
|
|
foreach ($attachmentsManager->manager()->get('files') ?? [] as $ref => $data) {
|
|
if (!empty($data['alt'])) {
|
|
continue;
|
|
}
|
|
|
|
$altSelected = false;
|
|
$fullPath = $attachmentsManager->getAttachmentFullPath($ref);
|
|
$content = $this->sourceDisk->get($fullPath);
|
|
$content = Image::read($content)->toJpeg();
|
|
$base64 = base64_encode($content);
|
|
|
|
while (!$altSelected) {
|
|
$sentence = Ollama::describeImage([$base64]);
|
|
$translated = Translator::translate($sentence);
|
|
$alt = textarea(sprintf('Image description for %s', $fullPath), '', $translated);
|
|
|
|
if (confirm('Use this text as the `alt` attribute for image?', true, 'Yes', 'No', false, null, $alt)) {
|
|
$attachmentsManager->manager()->set(sprintf('files.%s.alt', $ref), $alt);
|
|
$attachmentsManager->manager()->save();
|
|
|
|
$altSelected = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|