Improved image size support

This commit is contained in:
Matias Griese
2022-02-16 14:12:05 +02:00
parent 70ef4efdcb
commit c984e0a78e
2 changed files with 48 additions and 35 deletions

View File

@@ -369,32 +369,19 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac
unset($info['dirname'], $info['filename']);
if (null !== $cached) {
$filepath = $this->getPath($filename);
$existing = $cached[$filename] ?? null;
if ($existing && $existing['size'] === $info['size'] && $existing['modified'] === $info['modified']) {
// Append cached data.
$info += $existing;
} elseif ($type === 'image') {
// Cached data cannot be used, load the image from the filesystem and read the image size.
$image_info = $this->readImageSize($filepath);
if ($image_info) {
[$width, $height] = $image_info;
$info += [
'width' => $width,
'height' => $height
];
try {
$filepath = $this->getPath($filename);
$existing = $cached[$filename] ?? null;
if ($existing && $existing['size'] === $info['size'] && $existing['modified'] === $info['modified']) {
// Append cached data.
$info += $existing;
} else if ($type === 'image') {
$info += $this->readImageSize($filepath);
} elseif ($type === 'vector') {
$info += $this->readVectorSize($filepath);
}
// TODO: This is going to be slow without any indexing!
/*
// Add missing jpeg exif data.
if (null !== $exifReader && !isset($info['exif']) && $info['mime'] === 'image/jpeg') {
$exif = $exifReader->read($filepath);
if ($exif) {
$info['exif'] = array_diff_key($exif->getData(), array_flip($this->standard_exif));
}
}
*/
} catch (\RuntimeException $e) {
// TODO: Maybe we want to handle this..?
}
}

View File

@@ -17,6 +17,7 @@ use Grav\Common\Utils;
use Grav\Framework\File\Formatter\JsonFormatter;
use Grav\Framework\File\JsonFile;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
use RuntimeException;
use function count;
use function is_array;
@@ -129,19 +130,46 @@ abstract class LocalMedia extends AbstractMedia
return file_exists("{$destination}/{$filename}");
}
/**
* @param string $filepath
* @return string
*/
protected function readFileContents(string $filepath): string
{
return file_get_contents($filepath);
}
/**
* @param string $filepath
* @return array
*/
protected function readImageSize(string $filepath): array
{
if (str_ends_with($filepath, '.svg')) {
// Make sure that getting image size is supported.
if (!\extension_loaded('simplexml')) {
return [0, 0, 'mime' => 'image/svg+xml'];
}
$info = getimagesize($filepath);
if (!$info) {
throw new RuntimeException('Cannot read image size');
}
$xml = simplexml_load_string(file_get_contents($filepath));
// TODO: This is going to be slow without any indexing!
/*
// Add missing jpeg exif data.
if (null !== $exifReader && !isset($info['exif']) && $info['mime'] === 'image/jpeg') {
$exif = $exifReader->read($filepath);
if ($exif) {
$info['exif'] = array_diff_key($exif->getData(), array_flip($this->standard_exif));
}
}
*/
return ['width' => $info[0], 'height' => $info[0], 'mime' => $info['mime']];
}
protected function readVectorSize(string $filepath): array
{
// Make sure that getting image size is supported.
if (\extension_loaded('simplexml')) {
$data = $this->readFileContents($filepath);
$xml = simplexml_load_string($data);
$attr = $xml ? $xml->attributes() : null;
if ($attr instanceof \SimpleXMLElement) {
// Get the size from svg image.
@@ -153,14 +181,12 @@ abstract class LocalMedia extends AbstractMedia
}
if ($width && $height) {
return [(int)$width, (int)$height, 'mime' => 'image/svg+xml'];
return ['width' => (int)$width, 'height' => (int)$height, 'mime' => 'image/svg+xml'];
}
}
return [0, 0, 'mime' => 'application/octet-stream'];
}
return getimagesize($filepath);
throw new RuntimeException('Cannot read image size');
}
/**