Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Whisk's avatar
Level 9

Image path getting saved but the image isn't (Intervention Image package)

I'm trying to figure out what this bit of code does and why. This is a method to assist with an image upload process using the Intervention Image package. Whats really getting me is that the image path is getting saved to the DB but the image isn't getting saved to the file path.

Here is the part that seems to not be doing its part. I dont understand what the ->put($imageFileName, $resized->__toString(), 'public'); is doing, mostly the $resized->__toString() part.

           // Calling the intervention Image package to resize image
            $resized = Image::make($image)->resize(640, null, function ($constraint) {
                $constraint->aspectRatio();
            })->stream();
            $resized_thumb = Image::make($image)->resize(320, 213)->stream();

            $image_name = strtolower(time().Str::random(5).'-'.Str::slug($file_base_name)).'.' .     $image->getClientOriginalExtension();

            $imageFileName = 'uploads/images/'.$image_name;
            $imageThumbName = 'uploads/images/thumbs/'.$image_name;

            try{
                //Upload original image to folder
               
                $is_uploaded = current_disk()->put($imageFileName, $resized->__toString(), 'public');
                if ($is_uploaded) {
                    //Save image name into db
                    $created_img_db = Media::create(['user_id' => $user_id, 'ad_id' => $ad_id, 'media_name'=>$image_name, 'type'=>'image', 'storage' => get_option('default_storage'), 'ref'=>'ad']);

                    //upload thumb image
                    current_disk()->put($imageThumbName, $resized_thumb->__toString(), 'public');
                    $img_url = media_url($created_img_db, false);
                }
            } catch (\Exception $e){
                return redirect()->back()->withInput($request->input())->with('error', $e->getMessage()) ;
            }

Here is the current disk method

        // Set the current disk location for image uploads
        function current_disk(){
        $current_disk = \Illuminate\Support\Facades\Storage::disk('Default storage');
         return $current_disk;
         }
0 likes
1 reply
ahmeddabak's avatar

i do not think it is good to use the __toString() method to get the mage as a stream and then save it to a file, also as i remember it the stream method is meant to send the image as a response to the client, and to export the image as a string

why not use the save method in Intervention/image and feed it the storage location

Please or to participate in this conversation.