move_detect/cloud/src/Ingest/ImageProcessor.php

24 lines
728 B
PHP

<?php
declare(strict_types=1);
namespace App\Ingest;
final class ImageProcessor
{
public static function reencode(string $base64, int $quality = 80): array
{
$raw = base64_decode($base64, true);
if ($raw === false || $raw === '') return ['ok' => false];
$im = @imagecreatefromstring($raw);
if ($im === false) return ['ok' => false];
ob_start();
$ok = imagejpeg($im, null, $quality);
$bytes = (string)ob_get_clean();
if (!$ok || $bytes === '') return ['ok' => false];
return [
'ok' => true,
'bytes' => $bytes,
'uuid' => bin2hex(random_bytes(16)),
'hash' => hash('sha256', $bytes),
];
}
}