Laravel Upload Image and Resize to Digital Ocean Spaces

1.4k Views Asked by At

I try upload image and resize to digital ocean spaces, but the resize image code doesn't resize

 $image = $request->file('image');

    /**
     * save resize image
     */

    $thumb_uploads = env('CLOUD_POSTS_IMAGE_THUMB');

    $image_resize = Image::make($image->getRealPath());

    $image_resize->resize(50, 50, function ($constraint) {

        $constraint->aspectRatio();

    })->save(Storage::disk('spaces')->put($thumb_uploads .'/'.$image->hashName(), file_get_contents($image), 'public'));
2

There are 2 best solutions below

6
Ozan Kurt On BEST ANSWER

Try this, you should use the resized images content.

$image_resize = $image_resize->resize(50, 50, function ($constraint) {
    $constraint->aspectRatio();
})->encode('jpg');

Storage::disk('spaces')->put($thumb_uploads .'/'.$image->hashName(), (string) $image_resize);
2
Tovshoo eclips_GG On

I tried above the code. But not running.

{ $imgpath200 = 'mechanic/category/200/'.$imgname; $resizeImg200 = Image::make($realImg->getRealPath())->resize(200, 200, function($constraint) { $constraint->aspectRatio(); // $constraint->upsize(); })->encode('png'); \Log::info('resize image'); \Log::info((string)$resizeImg200); $result = $client->putObject([ 'Bucket' => config('filesystems.disks.do_spaces.bucket'), 'Key' => $imgpath200, 'SourceFile' => $resizeImg200->stream()->__toString(), 'ACL' => 'public-read', // 'ContentLength' => $fileSize, ]) }