Why Storage::put() Doesn't Work With Intervention Image Streams
You are running into a classic type error caused by mixing stream-based data with string-based storage methods in Laravel. The core issue lies in this specific line of your code:
Storage::disk('local')->put('images/1/smalls'.'/'.$fileName, $img, 'public');
The method Storage::put() expects the second argument to be a string (the raw file content). However, $img is an instance of the Intervention \Intervention\Image\Image object. When you pass an object instance directly to a function expecting a string, PHP does not automatically serialize it into binary data unless explicitly instructed to do so. Consequently, Storage::put() interprets the object's internal state or its class name as the file content, resulting in a file that appears blank or contains only metadata when opened in a browser or text editor.
To fix this, you must convert the manipulated image back into a raw byte stream before saving it to the disk. The standard approach is to use the encode() method on the Intervention Image instance. This method returns a string representation of the image (typically in JPEG or PNG format) based on the resolution and quality settings defined in your constraints.
Here is how you should modify the logic:
if ($request->hasFile('photo')) {
$image = $request->file('photo');
$fileName = time() . '.' . $image->getClientOriginalExtension();
// Load the image from the uploaded file stream
$img = Image::make($image->getRealPath());
// Resize while maintaining aspect ratio
$img->resize(120, 120, function ($constraint) {
$constraint->aspectRatio();
});
// Encode the image object into a raw string (e.g., JPEG format)
$imageData = $img->encode('jpeg', 85);
// Save the raw string to storage
Storage::disk('public')->put($fileName, $imageData);
// Optional: Clean up the uploaded file if you don't need the original
$image->delete();
}
Handling File Paths and Disks
In your original snippet, you were using Storage::disk('local') but passing 'public' as the third argument. The third argument in older versions of Laravel's Storage facade was used for visibility settings, but modern Laravel (9.x and later) uses Storage::disk('name')->visibility() or simply relies on the filesystem structure.
It is best practice to use the public disk alias if your images are meant to be served directly by the web server at /public/storage. If you stick with the local disk, ensure the path points to a directory inside your project root that the web server can read. A common pitfall is forgetting to create the destination directory structure (images/1/smalls) before attempting to write to it. You should explicitly create these directories to avoid runtime exceptions:
Storage::disk('public')->makeDirectory(dirname($fileName));
The Trade-off of Intervention Image
While Intervention Image is powerful for server-side processing, it relies on GD or Imagick extensions being installed and configured correctly on your server. If those extensions are missing, Image::make() will throw a fatal error before you even get to the resizing logic. Additionally, using getRealPath() creates a temporary file in the system's temp directory just so Intervention can read the uploaded stream. In high-traffic applications, this temporary file creation can become a bottleneck compared to pure stream processing libraries like Spatie Media Library or Laravel Scout integrations.
If you find yourself needing more complex operations like generating thumbnails for multiple sizes simultaneously, consider using Image::make() with specific dimensions in one go rather than chaining multiple resize calls, which can impact performance. Always remember that encode() requires a format string (like 'jpeg', 'png') and optionally a quality integer; omitting the format defaults to PNG, which might not be what you want for photos.
Final Recommendation
The solution is straightforward: always convert your Intervention Image instance to a raw string using ->encode() before passing it to Storage::put(). This ensures that binary image data is written to the disk rather than an object reference, preventing the "blank image" issue you encountered.
