-
app/Support/MediaLibrary/FileSizeOptimizedWidthCalculator.php
Open in GitHubuse Spatie\MediaLibrary\ResponsiveImages\WidthCalculator\FileSizeOptimizedWidthCalculator as BaseFileSizeOptimizedWidthCalculator; class FileSizeOptimizedWidthCalculator extends BaseFileSizeOptimizedWidthCalculator { public float $stepSize = 0.4; public function calculateWidths(int $fileSize, int $width, int $height): Collection { $targetWidths = collect(); $targetWidths->push($width); $ratio = $height / $width; $area = $height * $width; $predictedFileSize = $fileSize; $pixelPrice = $predictedFileSize / $area; while (true) { $predictedFileSize *= $this->stepSize; $newWidth = (int) floor(sqrt(($predictedFileSize / $pixelPrice) / $ratio)); if ($this->finishedCalculating((int) $predictedFileSize, $newWidth)) { return $targetWidths; } $targetWidths->push($newWidth); } } }