2025-03-18 00:41:04 +01:00
|
|
|
<?php
|
|
|
|
namespace Vichan\Service\Media;
|
|
|
|
|
2025-03-26 00:36:04 +01:00
|
|
|
use Vichan\Data\{Exif, MediaInstallResult, ThumbGenerationResult};
|
2025-03-25 22:41:04 +01:00
|
|
|
use Vichan\Functions\{Fs, Metadata};
|
2025-03-18 00:41:04 +01:00
|
|
|
|
|
|
|
|
2025-03-20 17:04:27 +01:00
|
|
|
/**
|
|
|
|
* Basically a fallback implementation. GD does not handle color profiles outside of webp.
|
|
|
|
*/
|
2025-03-18 11:26:30 +01:00
|
|
|
class GdMediaHandler implements MediaHandler {
|
2025-03-25 23:48:28 +01:00
|
|
|
use MediaHandlerTrait;
|
|
|
|
|
2025-03-18 10:13:09 +01:00
|
|
|
private const PHP81 = \PHP_MAJOR_VERSION >= 8 && \PHP_MINOR_VERSION >= 1;
|
2025-03-18 00:41:04 +01:00
|
|
|
|
|
|
|
|
2025-03-18 14:25:28 +01:00
|
|
|
private bool $strip_redraw;
|
2025-03-25 22:06:35 +01:00
|
|
|
private array $exif_readers;
|
2025-03-25 23:48:28 +01:00
|
|
|
private int $image_max_width;
|
|
|
|
private int $image_max_height;
|
2025-03-18 14:25:28 +01:00
|
|
|
|
|
|
|
|
2025-03-18 00:41:04 +01:00
|
|
|
private static function imageCreateFrom(string $file, string $mime): mixed {
|
|
|
|
switch ($mime) {
|
|
|
|
case 'image/jpeg':
|
2025-03-18 10:13:09 +01:00
|
|
|
return \imagecreatefromjpeg($file);
|
2025-03-18 00:41:04 +01:00
|
|
|
case 'image/png':
|
2025-03-18 10:13:09 +01:00
|
|
|
return \imagecreatefrompng($file);
|
2025-03-18 00:41:04 +01:00
|
|
|
case 'image/gif':
|
2025-03-18 10:13:09 +01:00
|
|
|
return \imagecreatefromgif($file);
|
2025-03-18 00:41:04 +01:00
|
|
|
case 'image/webp':
|
2025-03-18 10:13:09 +01:00
|
|
|
return \imagecreatefromwbmp($file);
|
2025-03-18 00:41:04 +01:00
|
|
|
case 'image/bmp':
|
2025-03-18 10:13:09 +01:00
|
|
|
return \imagecreatefrombmp($file);
|
2025-03-18 00:41:04 +01:00
|
|
|
case 'image/avif':
|
2025-03-18 10:13:09 +01:00
|
|
|
return self::PHP81 ? \imagecreatefromavif($file) : false;
|
2025-03-18 00:41:04 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2025-03-22 01:40:30 +01:00
|
|
|
private static function imageSaveTo(mixed $gd, string $file, string $mime) {
|
2025-03-18 00:41:04 +01:00
|
|
|
// Somebody should tune the quality and speed values...
|
2025-03-20 17:04:27 +01:00
|
|
|
// Won't be me.
|
2025-03-18 00:41:04 +01:00
|
|
|
switch ($mime) {
|
|
|
|
case 'image/jpeg':
|
2025-03-18 10:13:09 +01:00
|
|
|
return \imagejpeg($gd, $file, 50);
|
2025-03-18 00:41:04 +01:00
|
|
|
case 'image/png':
|
2025-03-18 10:13:09 +01:00
|
|
|
return \imagepng($gd, $file, 7);
|
2025-03-18 00:41:04 +01:00
|
|
|
case 'image/gif':
|
2025-03-18 10:13:09 +01:00
|
|
|
return \imagegif($gd, $file);
|
2025-03-18 00:41:04 +01:00
|
|
|
case 'image/webp':
|
2025-03-20 23:25:45 +01:00
|
|
|
return \imagewebp($gd, $file, 50);
|
2025-03-18 00:41:04 +01:00
|
|
|
case 'image/bmp':
|
2025-03-18 10:13:09 +01:00
|
|
|
return \imagebmp($gd, $file, true);
|
2025-03-18 00:41:04 +01:00
|
|
|
case 'image/avif':
|
2025-03-18 10:13:09 +01:00
|
|
|
return self::PHP81 ? \imageavif($gd, $file, 30, 6) : false;
|
2025-03-18 00:41:04 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2025-03-22 01:40:30 +01:00
|
|
|
private static function enableTransparency(mixed $gd, string $mime) {
|
2025-03-18 00:41:04 +01:00
|
|
|
switch ($mime) {
|
|
|
|
case 'image/png':
|
2025-03-18 10:13:09 +01:00
|
|
|
\imagecolortransparent($gd, \imagecolorallocatealpha($gd, 0, 0, 0, 0));
|
|
|
|
\imagesavealpha($gd, true);
|
|
|
|
\imagealphablending($gd, false);
|
2025-03-18 00:41:04 +01:00
|
|
|
break;
|
|
|
|
case 'image/gif':
|
|
|
|
\imagecolortransparent($gd, \imagecolorallocatealpha($gd, 0, 0, 0, 0));
|
|
|
|
\imagesavealpha($gd, true);
|
|
|
|
break;
|
|
|
|
}
|
2025-03-22 01:40:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static function createCanvas(string $mime, int $width, int $height) {
|
|
|
|
$gd = \imagecreatetruecolor($width, $height);
|
|
|
|
self::enableTransparency($gd, $mime);
|
2025-03-18 00:41:04 +01:00
|
|
|
return $gd;
|
|
|
|
}
|
|
|
|
|
2025-03-25 22:06:35 +01:00
|
|
|
private function getOrientation(string $file_path, string $file_mime): ?int {
|
|
|
|
$exif_reader = $this->exif_readers[$file_mime] ?? null;
|
|
|
|
if ($exif_reader !== null) {
|
|
|
|
return $exif_reader->getOrientation($file_path);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2025-03-18 14:25:28 +01:00
|
|
|
private function generateThumbImpl(
|
|
|
|
mixed $gd,
|
|
|
|
string $source_file_path,
|
|
|
|
string $source_file_mime,
|
|
|
|
string $source_file_kind,
|
2025-03-28 11:19:26 +01:00
|
|
|
string $preferred_out_file_basepath,
|
2025-03-18 00:41:04 +01:00
|
|
|
string $preferred_out_mime,
|
|
|
|
int $max_width,
|
|
|
|
int $max_height
|
2025-03-18 14:25:28 +01:00
|
|
|
) {
|
2025-03-18 00:41:04 +01:00
|
|
|
$width = \imagesx($gd);
|
|
|
|
$height = \imagesy($gd);
|
|
|
|
|
|
|
|
if ($width <= $max_width && $height <= $max_height) {
|
2025-03-28 11:19:26 +01:00
|
|
|
$out_path = $preferred_out_file_basepath . '.' . Metadata\mime_to_ext($source_file_mime);
|
2025-03-18 00:41:04 +01:00
|
|
|
|
2025-03-25 23:48:28 +01:00
|
|
|
$this->move_or_link_or_copy($source_file_kind, $source_file_path, $out_path);
|
2025-03-18 10:09:32 +01:00
|
|
|
|
|
|
|
return new ThumbGenerationResult(
|
|
|
|
$out_path,
|
|
|
|
$source_file_mime,
|
|
|
|
$width,
|
|
|
|
$height
|
|
|
|
);
|
2025-03-18 00:41:04 +01:00
|
|
|
} else {
|
2025-03-28 11:19:26 +01:00
|
|
|
$out_path = $preferred_out_file_basepath . '.' . Metadata\mime_to_ext($preferred_out_mime);
|
2025-03-18 00:41:04 +01:00
|
|
|
|
|
|
|
$gd_other = self::createCanvas($preferred_out_mime, $max_width, $max_height);
|
|
|
|
\imagecopyresampled($gd_other, $gd, 0, 0, 0, 0, $max_width, $max_height, $width, $height);
|
|
|
|
|
|
|
|
if (!self::imageSaveTo($gd_other, $out_path, $preferred_out_mime)) {
|
2025-03-18 14:25:28 +01:00
|
|
|
\imagedestroy($gd_other);
|
2025-03-25 23:48:28 +01:00
|
|
|
throw new MediaException("Could not create thumbnail file at '$out_path'", MediaException::ERR_IO_ERR);
|
2025-03-18 00:41:04 +01:00
|
|
|
}
|
|
|
|
|
2025-03-18 14:25:28 +01:00
|
|
|
\imagedestroy($gd_other);
|
|
|
|
|
2025-03-18 10:09:32 +01:00
|
|
|
return new ThumbGenerationResult(
|
|
|
|
$out_path,
|
|
|
|
$preferred_out_mime,
|
|
|
|
$max_width,
|
|
|
|
$max_height
|
|
|
|
);
|
2025-03-18 00:41:04 +01:00
|
|
|
}
|
|
|
|
}
|
2025-03-18 14:25:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $strip_redraw If the EXIF metadata should be stripped by redrawing it.
|
2025-03-25 21:18:10 +01:00
|
|
|
* May cause the loss of color profiles. Orientation is still handled in JPG and PNG.
|
2025-03-25 22:06:35 +01:00
|
|
|
* @param array $exif_readers A map of mime types to {@link Vichan\Data\Driver\Metadata\ExifReader} instances.
|
2025-03-18 14:25:28 +01:00
|
|
|
*/
|
2025-03-25 23:48:28 +01:00
|
|
|
public function __construct(bool $strip_redraw, array $exif_readers, int $max_width, int $max_height) {
|
2025-03-18 14:25:28 +01:00
|
|
|
$this->strip_redraw = $strip_redraw;
|
2025-03-25 22:06:35 +01:00
|
|
|
$this->exif_readers = $exif_readers;
|
2025-03-25 23:50:22 +01:00
|
|
|
$this->image_max_width = $max_width;
|
|
|
|
$this->image_max_height = $max_height;
|
2025-03-18 14:25:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function supportsMime(string $mime): bool {
|
|
|
|
$info = \gd_info();
|
|
|
|
return ($mime === 'image/jpeg' && $info['JPEG Support'])
|
|
|
|
|| ($mime === 'image/png' && $info['PNG Support'])
|
|
|
|
|| ($mime === 'image/gif' && $info['GIF Read Support'] && $info['GIF Create Support'])
|
|
|
|
|| ($mime === 'image/webp' && $info['WebP Support'])
|
|
|
|
|| $mime === 'image/bmp'
|
|
|
|
|| ($mime === 'image/avif' && self::PHP81 && $info['AVIF Support']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function openHandle(string $file_path, string $file_mime, int $file_kind): mixed {
|
2025-03-25 23:48:28 +01:00
|
|
|
try {
|
|
|
|
list($width, $height, $mime) = Metadata\sniff_image($file_path);
|
|
|
|
if ($width > $this->image_max_width || $height > $this->image_max_height) {
|
|
|
|
throw new MediaException("Image too big", MediaException::ERR_IMAGE_TOO_LARGE);
|
|
|
|
}
|
|
|
|
if ($mime !== $file_mime) {
|
|
|
|
throw new MediaException("Mime type mismatch, expected $file_mime, got $mime", MediaException::ERR_COMPUTE_ERR);
|
|
|
|
}
|
|
|
|
} catch (\RuntimeException $e) {
|
|
|
|
throw new MediaException("Could not sniff '$file_path'", MediaException::ERR_NO_OPEN, $e);
|
|
|
|
}
|
|
|
|
|
2025-03-18 14:25:28 +01:00
|
|
|
$gd = self::imageCreateFrom($file_path, $file_mime);
|
|
|
|
if ($gd === false) {
|
2025-03-25 23:48:28 +01:00
|
|
|
throw new MediaException("Could not open '$file_path'", MediaException::ERR_NO_OPEN);
|
2025-03-18 14:25:28 +01:00
|
|
|
}
|
2025-03-22 01:40:30 +01:00
|
|
|
|
|
|
|
// Fix the orientation once and for all.
|
2025-03-25 22:06:35 +01:00
|
|
|
$exif_orientation = $this->getOrientation($file_path, $file_mime);
|
|
|
|
if ($exif_orientation !== null) {
|
|
|
|
$degrees = Exif::exifOrientationDegrees($exif_orientation);
|
|
|
|
$flipped = Exif::exifOrientationIsFlipped($exif_orientation);
|
2025-03-22 01:40:30 +01:00
|
|
|
|
|
|
|
if ($degrees !== 0) {
|
|
|
|
self::enableTransparency($gd, $file_mime);
|
2025-03-25 11:02:05 +01:00
|
|
|
$gd_other = \imagerotate($gd, 360 - $degrees, \imagecolorallocatealpha($gd, 0, 0, 0, 127));
|
2025-03-22 01:40:30 +01:00
|
|
|
\imagedestroy($gd);
|
|
|
|
if ($gd_other === false) {
|
2025-03-25 23:48:28 +01:00
|
|
|
throw new MediaException("Error while correcting rotation of '$file_path'", MediaException::ERR_COMPUTE_ERR);
|
2025-03-22 01:40:30 +01:00
|
|
|
}
|
|
|
|
$gd = $gd_other;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($flipped) {
|
|
|
|
if (!\imageflip($gd, \IMG_FLIP_HORIZONTAL)) {
|
2025-03-25 23:48:28 +01:00
|
|
|
throw new MediaException("Error while correcting flipping of '$file_path'", MediaException::ERR_COMPUTE_ERR);
|
2025-03-22 01:40:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-18 14:25:28 +01:00
|
|
|
return [ $gd, $file_path, $file_mime, $file_kind ];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function closeHandle(mixed $handle) {
|
|
|
|
\imagedestroy($handle[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function installMediaAndGenerateThumb(
|
|
|
|
mixed $handle,
|
2025-03-28 11:19:26 +01:00
|
|
|
string $media_preferred_out_file_basepath,
|
|
|
|
string $thumb_preferred_out_file_basepath,
|
2025-03-18 14:25:28 +01:00
|
|
|
string $thumb_preferred_out_mime,
|
|
|
|
int $thumb_max_width,
|
|
|
|
int $thumb_max_height
|
2025-03-26 00:36:04 +01:00
|
|
|
): MediaInstallResult {
|
2025-03-18 14:25:28 +01:00
|
|
|
list($gd, $source_file_path, $source_file_mime, $source_file_kind) = $handle;
|
2025-03-28 11:19:26 +01:00
|
|
|
$out_path = $media_preferred_out_file_basepath . '.' . Metadata\mime_to_ext($source_file_mime);
|
2025-03-18 14:25:28 +01:00
|
|
|
|
|
|
|
if ($this->strip_redraw) {
|
|
|
|
if (!self::imageSaveTo($gd, $out_path, $source_file_mime)) {
|
2025-03-25 23:48:28 +01:00
|
|
|
throw new MediaException("Could not create media file at '$out_path'", MediaException::ERR_IO_ERR);
|
2025-03-18 14:25:28 +01:00
|
|
|
}
|
|
|
|
} else {
|
2025-03-25 23:48:28 +01:00
|
|
|
$this->move_or_link_or_copy($source_file_kind, $source_file_path, $out_path);
|
2025-03-18 14:25:28 +01:00
|
|
|
}
|
|
|
|
|
2025-03-26 00:36:04 +01:00
|
|
|
$thumb = $this->generateThumbImpl(
|
2025-03-18 14:25:28 +01:00
|
|
|
$gd,
|
|
|
|
$source_file_path,
|
|
|
|
$source_file_mime,
|
|
|
|
$source_file_kind,
|
2025-03-28 11:19:26 +01:00
|
|
|
$thumb_preferred_out_file_basepath,
|
2025-03-18 14:25:28 +01:00
|
|
|
$thumb_preferred_out_mime,
|
|
|
|
$thumb_max_width,
|
|
|
|
$thumb_max_height
|
|
|
|
);
|
2025-03-26 00:36:04 +01:00
|
|
|
return new MediaInstallResult($thumb, $out_path);
|
2025-03-18 14:25:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function generateThumb(
|
|
|
|
mixed $handle,
|
2025-03-28 11:19:26 +01:00
|
|
|
string $preferred_out_file_basepath,
|
2025-03-18 14:25:28 +01:00
|
|
|
string $preferred_out_mime,
|
|
|
|
int $max_width,
|
|
|
|
int $max_height
|
|
|
|
): ThumbGenerationResult {
|
|
|
|
list($gd, $source_file_path, $source_file_mime, $source_file_kind) = $handle;
|
|
|
|
|
|
|
|
return $this->generateThumbImpl(
|
|
|
|
$gd,
|
|
|
|
$source_file_path,
|
|
|
|
$source_file_mime,
|
|
|
|
$source_file_kind,
|
2025-03-28 11:19:26 +01:00
|
|
|
$preferred_out_file_basepath,
|
2025-03-18 14:25:28 +01:00
|
|
|
$preferred_out_mime,
|
|
|
|
$max_width,
|
|
|
|
$max_height
|
|
|
|
);
|
|
|
|
}
|
2025-03-18 00:41:04 +01:00
|
|
|
}
|