media = $media; } /** * Execute the job. * * @return void */ public function handle() { $media = $this->media; if (! $media) { return; } if (! Storage::exists($media->media_path) || $media->skip_optimize) { return; } $path = $media->media_path; $file = storage_path('app/'.$path); $quality = config_cache('pixelfed.image_quality'); $orientations = [ 'square' => [ 'width' => 1080, 'height' => 1080, ], 'landscape' => [ 'width' => 1920, 'height' => 1080, ], 'portrait' => [ 'width' => 1080, 'height' => 1350, ], ]; try { $imageManager = ImageDriverManager::createImageManager(); $img = $imageManager->read($file); $width = $img->width(); $height = $img->height(); $aspect = $width / $height; $orientation = $aspect === 1 ? 'square' : ($aspect > 1 ? 'landscape' : 'portrait'); $ratio = $orientations[$orientation]; $img = $img->scaleDown($ratio['width'], $ratio['height']); $extension = pathinfo($file, PATHINFO_EXTENSION); if (in_array(strtolower($extension), ['jpg', 'jpeg'])) { $encoder = new JpegEncoder($quality); } else { $encoder = new PngEncoder; } $encoded = $img->encode($encoder); file_put_contents($file, $encoded->toString()); } catch (Exception $e) { Log::error($e); } } }