get('media/' . $this->filename); // Open file try { $image = $manager->read($file); } catch (DecoderException) { // not an image; delete file and end job Storage::disk('local')->delete('media/' . $this->filename); return; } // Save the file publicly Storage::disk('public')->put('media/' . $this->filename, $file); // Create smaller versions if necessary if ($image->width() > 1000) { $filenameParts = explode('.', $this->filename); $extension = array_pop($filenameParts); // the following achieves this data flow // foo.bar.png => ['foo', 'bar', 'png'] => ['foo', 'bar'] => foo.bar $basename = trim(implode('.', $filenameParts), '.'); $medium = $image->resize(width: 1000); Storage::disk('public')->put('media/' . $basename . '-medium.' . $extension, (string) $medium->encode()); $small = $image->resize(width: 500); Storage::disk('public')->put('media/' . $basename . '-small.' . $extension, (string) $small->encode()); } // Now we can delete the locally saved image Storage::disk('local')->delete('media/' . $this->filename); } }