Tabellen und Bilder gehören zur festen Auswahl von FormattedText. Ein Bild ist ein Verweis auf eine freigegebene Bildfassung und kommt per Picker aus dem Medienbestand; der Stand schreibt die Bildfassung mit fest, die Rechte entscheidet DecideAccess. Der PDF-Satz (PdfFlow) ist für Übung und kommende Dokumente gemeinsam; PdfDocument kann zusätzlich Bilder, Flächen, Verweise und Lesezeichen, Belege bleiben byte-gleich. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
57 lines
1.9 KiB
PHP
57 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Media;
|
|
|
|
/**
|
|
* Bereitet ein Bild für das PDF auf: Es wird mit GD gelesen, auf eine druckbare Größe verkleinert, auf Weiß gesetzt
|
|
* (Transparenz gibt es im PDF-Schreiber nicht) und als JPEG neu kodiert. So braucht das PDF keine Bildbibliothek und
|
|
* keine Rücksicht auf das Ausgangsformat. Was GD nicht lesen kann (zum Beispiel WebP in einem Image ohne WebP-Unterstützung),
|
|
* liefert null; die Ausgabe zeigt dann einen Hinweis statt des Bildes.
|
|
*/
|
|
final class PdfImage
|
|
{
|
|
/** Längste Kante in Pixel; 1400 Pixel genügen für die Spaltenbreite einer A4-Seite bei guter Druckqualität. */
|
|
public const MAX_EDGE = 1400;
|
|
|
|
/**
|
|
* @return array{jpeg: string, width: int, height: int}|null
|
|
*/
|
|
public static function fromBytes(string $raw): ?array
|
|
{
|
|
if ($raw === '' || ! function_exists('imagecreatefromstring')) {
|
|
return null;
|
|
}
|
|
|
|
$source = @imagecreatefromstring($raw);
|
|
|
|
if ($source === false) {
|
|
return null;
|
|
}
|
|
|
|
$width = imagesx($source);
|
|
$height = imagesy($source);
|
|
|
|
if ($width < 1 || $height < 1) {
|
|
return null;
|
|
}
|
|
|
|
$scale = min(1.0, self::MAX_EDGE / max($width, $height));
|
|
$targetWidth = max(1, (int) round($width * $scale));
|
|
$targetHeight = max(1, (int) round($height * $scale));
|
|
$canvas = imagecreatetruecolor($targetWidth, $targetHeight);
|
|
|
|
if ($canvas === false) {
|
|
return null;
|
|
}
|
|
|
|
imagefill($canvas, 0, 0, (int) imagecolorallocate($canvas, 255, 255, 255));
|
|
imagecopyresampled($canvas, $source, 0, 0, 0, 0, $targetWidth, $targetHeight, $width, $height);
|
|
|
|
ob_start();
|
|
$written = imagejpeg($canvas, null, 82);
|
|
$jpeg = (string) ob_get_clean();
|
|
|
|
return $written && $jpeg !== '' ? ['jpeg' => $jpeg, 'width' => $targetWidth, 'height' => $targetHeight] : null;
|
|
}
|
|
}
|