mirror of
https://github.com/getgrav/grav.git
synced 2026-07-05 13:19:02 +02:00
Improved media
This commit is contained in:
@@ -11,6 +11,7 @@ namespace Grav\Common\Media\Traits;
|
||||
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Media\Interfaces\ImageMediaInterface;
|
||||
use Grav\Common\Media\Interfaces\MediaCollectionInterface;
|
||||
use Grav\Common\Page\Medium\ImageFile;
|
||||
use Grav\Common\Page\Medium\ImageMedium;
|
||||
use Grav\Common\Page\Medium\MediumFactory;
|
||||
@@ -329,15 +330,21 @@ trait ImageMediaTrait
|
||||
{
|
||||
$locator = Grav::instance()['locator'];
|
||||
|
||||
$file = $this->get('filepath');
|
||||
|
||||
// Use existing cache folder or if it doesn't exist, create it.
|
||||
$cacheDir = $locator->findResource('cache://images', true) ?: $locator->findResource('cache://images', true, true);
|
||||
|
||||
// Make sure we free previous image.
|
||||
unset($this->image);
|
||||
|
||||
$this->image = ImageFile::open($file)
|
||||
/** @var MediaCollectionInterface $media */
|
||||
$media = $this->get('media');
|
||||
if ($media && method_exists($media, 'getImageFileObject')) {
|
||||
$this->image = $media->getImageFileObject($this);
|
||||
} else {
|
||||
$this->image = ImageFile::open($this->get('filepath'));
|
||||
}
|
||||
|
||||
$this->image
|
||||
->setCacheDir($cacheDir)
|
||||
->setActualCacheDir($cacheDir)
|
||||
->setPrettyName($this->getImagePrettyName());
|
||||
|
||||
@@ -124,18 +124,19 @@ trait MediaUploadTrait
|
||||
}
|
||||
|
||||
// Calculate maximum file size (from MB).
|
||||
if ($settings['filesize']) {
|
||||
$max_filesize = $settings['filesize'] * 1048576;
|
||||
$filesize = $settings['filesize'];
|
||||
if ($filesize) {
|
||||
$max_filesize = $filesize * 1048576;
|
||||
if ($uploadedFile->getSize() > $max_filesize) {
|
||||
// TODO: use own language string
|
||||
throw new RuntimeException($this->translate('PLUGIN_ADMIN.EXCEEDED_GRAV_FILESIZE_LIMIT'), 400);
|
||||
}
|
||||
}
|
||||
|
||||
// Check size against the Grav upload limit.
|
||||
$grav_limit = Utils::getUploadLimit();
|
||||
if ($grav_limit > 0 && $uploadedFile->getSize() > $grav_limit) {
|
||||
throw new RuntimeException($this->translate('PLUGIN_ADMIN.EXCEEDED_GRAV_FILESIZE_LIMIT'), 400);
|
||||
} elseif (null === $filesize) {
|
||||
// Check size against the Grav upload limit.
|
||||
$grav_limit = Utils::getUploadLimit();
|
||||
if ($grav_limit > 0 && $uploadedFile->getSize() > $grav_limit) {
|
||||
throw new RuntimeException($this->translate('PLUGIN_ADMIN.EXCEEDED_GRAV_FILESIZE_LIMIT'), 400);
|
||||
}
|
||||
}
|
||||
|
||||
// Handle Accepted file types. Accept can only be mime types (image/png | image/*) or file extensions (.pdf | .jpg)
|
||||
|
||||
@@ -190,6 +190,15 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MediaObjectInterface $mediaObject
|
||||
* @return ImageFile
|
||||
*/
|
||||
public function getImageFileObject(MediaObjectInterface $mediaObject): ImageFile
|
||||
{
|
||||
return ImageFile::open($mediaObject->get('filepath'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Order the media based on the page's media_order
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user