Fixed error when resizing image

This commit is contained in:
Matias Griese
2022-04-22 20:28:04 +03:00
parent f8ef9c41c5
commit a52b4cbd8e
4 changed files with 38 additions and 16 deletions

View File

@@ -49,6 +49,9 @@ class LocalMediaFactory implements MediaFactoryInterface
public function readFile(string $type, string $path): string
{
$filepath = GRAV_WEBROOT . '/' . $path;
if (!is_file($filepath)) {
throw new RuntimeException(sprintf("Reading media file failed: File '%s' not found", $path));
}
error_clear_last();
$contents = @file_get_contents($filepath);
@@ -67,6 +70,9 @@ class LocalMediaFactory implements MediaFactoryInterface
public function readStream(string $type, string $path)
{
$filepath = GRAV_WEBROOT . '/' . $path;
if (!is_file($filepath)) {
throw new RuntimeException(sprintf("Reading media file failed: File '%s' not found", $path));
}
error_clear_last();
$contents = @fopen($filepath, 'rb');

View File

@@ -10,6 +10,7 @@
namespace Grav\Common\Media\Traits;
use BadFunctionCallException;
use Exception;
use Grav\Common\Debugger;
use Grav\Common\Grav;
use Grav\Common\Media\Interfaces\ImageMediaInterface;
@@ -81,17 +82,25 @@ trait ImageMediaTrait
$mediaFactory = Grav::instance()['media_factory'];
$fileData = $mediaFactory->readFile($mediaUri);
$adapter = GdAdapter::createFromString($fileData);
try {
$fileData = $mediaFactory->readFile($mediaUri);
$adapter = GdAdapter::createFromString($fileData);
$image = Image::createFromArray($data);
$image = Image::createFromArray($data);
$image->setAdapter($adapter);
$filepath = $image->save($filepath, $format, $quality);
$image->freeAdapter();
$image->setAdapter($adapter);
$filepath = $image->save($filepath, $format, $quality);
$image->freeAdapter();
$time = filemtime($filepath);
$size = filesize($filepath);
$time = filemtime($filepath);
$size = filesize($filepath);
} catch (Exception $e) {
/** @var Debugger $debugger */
$debugger = Grav::instance()['debugger'];
$debugger->addException($e);
return null;
}
return [$filepath, $mime, $time, $size];
}
@@ -665,11 +674,16 @@ trait ImageMediaTrait
$format = $extension;
}
$image = $this->image;
$mediaUri = $this->getMedia()->getMediaUri($this->filename);
if (null === $mediaUri) {
$mediaUri = 'media-local://' . str_replace('GRAV_WEBROOT', '', $image->getFilepath());
}
$image->extra['format'] = $format;
$image->extra['quality'] = $quality;
$image->extra['media-uri'] = $this->getMedia()->getMediaUri($this->filename);
$image->extra['media-uri'] = $mediaUri;
$image->extra['mime'] = $this->mime;
$data = $image->jsonSerialize();

View File

@@ -114,14 +114,11 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac
/**
* @param string $filename
* @return string
* @return string|null
*/
public function getMediaUri(string $filename): string
public function getMediaUri(string $filename): ?string
{
$schema = 'media-' . str_replace('_', '-', $this->getName());
$path = $this->getRealPath($filename);
return "{$schema}://{$path}";
return null;
}
/**

View File

@@ -79,6 +79,11 @@ class Image implements ImageOperationsInterface, JsonSerializable
$this->orientation = isset($info['exif']['Orientation']) ? (int)$info['exif']['Orientation'] : null;
}
public function getFilepath(): string
{
return $this->filepath;
}
/**
* @return array
*/