Lazy load all images to increase page load times (references #275)

This commit is contained in:
Bernd Bestel
2019-09-18 13:59:37 +02:00
parent d209c0bd22
commit 918f84f568
13 changed files with 48 additions and 18 deletions

View File

@@ -33,13 +33,13 @@ class FilesService extends BaseService
return $groupFolderPath . '/' . $fileName;
}
public function DownscaleImage($group, $fileName, $bestFitHeight, $bestFitWidth)
public function DownscaleImage($group, $fileName, $bestFitHeight = null, $bestFitWidth = null)
{
$filePath = $this->GetFilePath($group, $fileName);
$fileNameWithoutExtension = pathinfo($filePath, PATHINFO_FILENAME);
$fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);
$fileNameDownscaled = $fileNameWithoutExtension . '__downscaledto' . $bestFitHeight . 'x' . $bestFitWidth . '.' . $fileExtension;
$fileNameDownscaled = $fileNameWithoutExtension . '__downscaledto' . ($bestFitHeight ? $bestFitHeight : 'auto') . 'x' . ($bestFitWidth ? $bestFitWidth : 'auto') . '.' . $fileExtension;
$filePathDownscaled = $this->GetFilePath($group, $fileNameDownscaled);
try
@@ -47,7 +47,18 @@ class FilesService extends BaseService
if (!file_exists($filePathDownscaled))
{
$image = new ImageResize($filePath);
$image->resizeToBestFit($bestFitHeight, $bestFitWidth);
if ($bestFitHeight !== null && $bestFitHeight !== null)
{
$image->resizeToBestFit($bestFitWidth, $bestFitHeight);
}
else if ($bestFitHeight !== null)
{
$image->resizeToHeight($bestFitHeight);
}
else if ($bestFitWidth !== null)
{
$image->resizeToWidth($bestFitWidth);
}
$image->save($filePathDownscaled);
}
}