mirror of
https://github.com/grocy/grocy.git
synced 2025-08-20 12:20:22 +00:00
Automatically downscale pictures to reduce page loading times (closes #275)
This commit is contained in:
@@ -2,8 +2,12 @@
|
||||
|
||||
namespace Grocy\Services;
|
||||
|
||||
use \Gumlet\ImageResize;
|
||||
|
||||
class FilesService extends BaseService
|
||||
{
|
||||
const FILE_SERVE_TYPE_PICTURE = 'picture';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
@@ -28,4 +32,30 @@ class FilesService extends BaseService
|
||||
|
||||
return $groupFolderPath . '/' . $fileName;
|
||||
}
|
||||
|
||||
public function DownscaleImage($group, $fileName, $bestFitHeight, $bestFitWidth)
|
||||
{
|
||||
$filePath = $this->GetFilePath($group, $fileName);
|
||||
$fileNameWithoutExtension = pathinfo($filePath, PATHINFO_FILENAME);
|
||||
$fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);
|
||||
|
||||
$fileNameDownscaled = $fileNameWithoutExtension . '__downscaledto' . $bestFitHeight . 'x' . $bestFitWidth . '.' . $fileExtension;
|
||||
$filePathDownscaled = $this->GetFilePath($group, $fileNameDownscaled);
|
||||
|
||||
try
|
||||
{
|
||||
if (!file_exists($filePathDownscaled))
|
||||
{
|
||||
$image = new ImageResize($filePath);
|
||||
$image->resizeToBestFit($bestFitHeight, $bestFitWidth);
|
||||
$image->save($filePathDownscaled);
|
||||
}
|
||||
}
|
||||
catch (ImageResizeException $ex)
|
||||
{
|
||||
return $filePath;
|
||||
}
|
||||
|
||||
return $filePathDownscaled;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user