feat(cloud): gd image re-encode (strip payload, uuid, hash)
This commit is contained in:
parent
2f88355071
commit
7246e12c4a
2 changed files with 53 additions and 0 deletions
25
cloud/src/Ingest/ImageProcessor.php
Normal file
25
cloud/src/Ingest/ImageProcessor.php
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
<?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();
|
||||
imagedestroy($im);
|
||||
if (!$ok || $bytes === '') return ['ok' => false];
|
||||
return [
|
||||
'ok' => true,
|
||||
'bytes' => $bytes,
|
||||
'uuid' => bin2hex(random_bytes(16)),
|
||||
'hash' => hash('sha256', $bytes),
|
||||
];
|
||||
}
|
||||
}
|
||||
28
cloud/tests/ImageProcessorTest.php
Normal file
28
cloud/tests/ImageProcessorTest.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace App\Tests;
|
||||
use App\Ingest\ImageProcessor;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class ImageProcessorTest extends TestCase
|
||||
{
|
||||
private function tinyJpegBase64(): string {
|
||||
$im = imagecreatetruecolor(8, 8);
|
||||
ob_start(); imagejpeg($im); $bytes = ob_get_clean(); imagedestroy($im);
|
||||
return base64_encode($bytes);
|
||||
}
|
||||
public function testReencodeProducesJpeg(): void {
|
||||
$r = ImageProcessor::reencode($this->tinyJpegBase64());
|
||||
$this->assertTrue($r['ok']);
|
||||
$this->assertSame("\xFF\xD8", substr($r['bytes'], 0, 2)); // JPEG magic
|
||||
$this->assertSame(32, strlen($r['uuid']));
|
||||
$this->assertSame(64, strlen($r['hash']));
|
||||
}
|
||||
public function testRejectsNonImage(): void {
|
||||
$r = ImageProcessor::reencode(base64_encode("not an image <?php echo 1; ?>"));
|
||||
$this->assertFalse($r['ok']);
|
||||
}
|
||||
public function testRejectsBadBase64(): void {
|
||||
$this->assertFalse(ImageProcessor::reencode('%%%not-base64%%%')['ok']);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue