Media fixes

This commit is contained in:
Matias Griese
2022-02-19 16:09:16 +02:00
parent c6172cf4b2
commit e92de67c68
11 changed files with 35 additions and 14 deletions

View File

@@ -18,6 +18,7 @@ interface ImageManipulateInterface
* Allows the ability to override the image's pretty name stored in cache
*
* @param string $name
* @return void
*/
public function setImagePrettyName($name);
@@ -47,6 +48,8 @@ interface ImageManipulateInterface
/**
* Clear out the alternatives.
*
* @return void
*/
public function clearAlternatives();

View File

@@ -18,6 +18,8 @@ use Grav\Common\Data\Data;
* @property string $type
* @property string $filename
* @property string $filepath
*
* @extends ArrayAccess<string,mixed>
*/
interface MediaObjectInterface extends \Grav\Framework\Media\Interfaces\MediaObjectInterface, ArrayAccess
{
@@ -54,6 +56,7 @@ interface MediaObjectInterface extends \Grav\Framework\Media\Interfaces\MediaObj
* Add meta file for the medium.
*
* @param string $filepath
* @return void
*/
public function addMetaFile($filepath);
@@ -62,6 +65,7 @@ interface MediaObjectInterface extends \Grav\Framework\Media\Interfaces\MediaObj
*
* @param int|float $ratio
* @param MediaObjectInterface $alternative
* @return void
*/
public function addAlternative($ratio, MediaObjectInterface $alternative);

View File

@@ -96,6 +96,7 @@ trait ImageMediaTrait
* Allows the ability to override the image's pretty name stored in cache
*
* @param string $name
* @return void
*/
public function setImagePrettyName($name)
{
@@ -212,6 +213,8 @@ trait ImageMediaTrait
/**
* Clear out the alternatives.
*
* @return void
*/
public function clearAlternatives()
{

View File

@@ -104,6 +104,7 @@ trait MediaObjectTrait
* Add meta file for the medium.
*
* @param string $filepath
* @return void
*/
abstract public function addMetaFile($filepath);
@@ -112,6 +113,7 @@ trait MediaObjectTrait
*
* @param int|float $ratio
* @param MediaObjectInterface $alternative
* @return void
*/
public function addAlternative($ratio, MediaObjectInterface $alternative)
{

View File

@@ -105,6 +105,8 @@ trait MediaPlayerTrait
/**
* Reset player.
*
* @return void
*/
public function resetPlayer()
{

View File

@@ -310,7 +310,7 @@ trait MediaUploadTrait
// Force media index update.
if (method_exists($this, 'saveIndex')) {
$this->saveIndex($this->index, 0);
$this->saveIndex($this->index);
}
} catch (Exception $e) {
@@ -362,7 +362,7 @@ trait MediaUploadTrait
// Force media index update.
if (method_exists($this, 'saveIndex')) {
$this->saveIndex($this->index, 0);
$this->saveIndex($this->index);
}
// Finally clear media cache.
@@ -407,7 +407,7 @@ trait MediaUploadTrait
// Force media index update.
if (method_exists($this, 'saveIndex')) {
$this->saveIndex($this->index, 0);
$this->saveIndex($this->index);
}
// Finally clear media cache.

View File

@@ -157,7 +157,7 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac
public function offsetUnset($offset): void
{
// Hack to make Iterator trait work together with unset.
if (isset($this->iteratorUnset) && (string)$offset === (string)key($this->items)) {
if ((string)$offset === (string)key($this->items)) {
$this->iteratorUnset = true;
}
@@ -650,7 +650,7 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac
$oldChecksum = $data['checksum'] ?? null;
$newChecksum = md5(serialize($files));
if ($oldChecksum !== $newChecksum) {
$this->saveIndex($files, $now);
$this->saveIndex($files, $newChecksum, $now);
} else {
$this->touchIndex($now);
}
@@ -735,8 +735,8 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac
if (file_exists($meta_path)) {
$types['meta']['file'] = $meta_path;
} elseif ($exifReader = $this->getExifReader()) {
$meta = $exifReader->read($file_path);
if ($meta) {
try {
$meta = $exifReader->read($file_path);
$meta_data = $meta->getData();
$meta_trimmed = array_diff_key($meta_data, array_flip($this->standard_exif));
if ($meta_trimmed) {
@@ -749,6 +749,7 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac
$file->save($meta_trimmed);
$types['meta']['file'] = $meta_path;
}
} catch (RuntimeException $e) {
}
}
}
@@ -779,7 +780,7 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac
$altWidth = $altMedium['file']->get('width');
$medWidth = $medium->get('width');
if ($altWidth && $medWidth) {
$ratio = (string)($altWidth / $medWidth);
$ratio = $altWidth / $medWidth;
$medium->addAlternative($ratio, $altMedium['file']);
}
}
@@ -841,10 +842,11 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac
/**
* @param array $files
* @param string|null $checksum
* @param int|null $timestamp
* @return void
*/
protected function saveIndex(array $files, string $checksum, ?int $timestamp = null): void
protected function saveIndex(array $files, string $checksum = null, ?int $timestamp = null): void
{
$index = $this->getIndexFile();
if (!$index || !$this->exists) {
@@ -854,7 +856,7 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac
$data = [
'type' => $this->config['type'] ?? 'local',
'version' => static::VERSION,
'checksum' => $checksum,
'checksum' => $checksum ?? md5(serialize($files)),
'timestamp' => $timestamp ?? time(),
'folder' => $this->path,
'url' => $this->url,

View File

@@ -171,6 +171,8 @@ class ImageFile extends Image
/**
* Read exif rotation from file and apply it.
*
* @return $this
*/
public function fixOrientation()
{

View File

@@ -202,7 +202,9 @@ class ImageMedium extends Medium implements ImageMediaInterface, ImageManipulate
$srcset = $this->srcset($reset);
if ($srcset) {
empty($attributes['srcset']) && $attributes['srcset'] = $srcset;
if (empty($attributes['srcset'])) {
$attributes['srcset'] = $srcset;
}
$attributes['sizes'] = $this->sizes();
}

View File

@@ -187,10 +187,10 @@ abstract class LocalMedia extends AbstractMedia
// Add missing jpeg exif data.
$exifReader = $this->getExifReader();
if (null !== $exifReader && !isset($info['exif']) && $info['mime'] === 'image/jpeg') {
try {
$exif = $exifReader->read($filepath);
if ($exif) {
$info['exif'] = array_diff_key($exif->getData(), array_flip($this->standard_exif));
}
$info['exif'] = array_diff_key($exif->getData(), array_flip($this->standard_exif));
} catch (\RuntimeException $e) {
}
*/

View File

@@ -98,6 +98,7 @@ class Medium extends Data implements RenderableInterface, MediaFileInterface
* Add meta file for the medium.
*
* @param string $filepath
* @return void
*/
public function addMetaFile($filepath)
{