disk = $disk; $this->uploadDir = $uploadDir; $this->media = $media; } public function generate(MediaPath $file, bool $update): void { $this->file = $file; $this->update = $update; if (!$file->exists()) { throw new ResizerException('File versions cannot be generated. Root file "'.$file->root().'" doesnt exist.'); } if ($this->file->compressor()->shouldGenerateVersions()) { $this->file->compressor()->start(); $this->generateVersions(); $this->file->compressor()->end(); } } private function dimensions(): Collection { [$width, $height] = $this->file->compressor()->originalSize(); return collect(compact('width', 'height')); } private function sizes(): Collection { $sizes = Setting::get('sizes'); return collect(array_column($sizes, 'aspect_ratio'))->map( fn ($ratio) => collect(array_combine(['width', 'height'], explode('x', $ratio))), ); } private function possibleSizes(): Collection { $return = collect([]); $ratios = collect(); $ratios->push($this->dimensions()); $ratios = $ratios->merge($this->sizes()); foreach (collect(Setting::get('breakpoints'))->push($this->dimensions()->get('width'))->unique() as $size) { foreach ($ratios as $ratio) { $width = $size; $height = $size * $ratio->get('height') / $ratio->get('width'); if ($height > $this->dimensions()->get('height')) { $width = $width * $this->dimensions()->get('height') / $height; $height = $this->dimensions()->get('height'); } if (ceil($width) > $this->dimensions()->get('width') || ceil($height) > $this->dimensions()->get('height')) { continue; } $return->push(collect([ 'width' => round($width), 'height' => round($height), ])); } } return $return; } private function generateVersions(): void { foreach ($this->possibleSizes() as $size) { $this->file->compressor()->resize($size, $this->update, function($media, $file) { if (!file_exists($file)) { throw new ResizerException('File versions cannot be generated. Version file "'.$file.'" of "'.$this->file->root().'" doesnt exist.'); } if (file_exists($file) || !$this->update) { $this->file->compressor()->make($file); } }); } } }